r"""Apply special functions to the original features. Parameters ---------- model : alphapy.Model Model specifications indicating any transforms. X : pandas.DataFrame Combined train and test data, or just prediction data. Returns ------- all_features : p
(model, X)
| 153 | # |
| 154 | |
| 155 | def apply_transforms(model, X): |
| 156 | r"""Apply special functions to the original features. |
| 157 | |
| 158 | Parameters |
| 159 | ---------- |
| 160 | model : alphapy.Model |
| 161 | Model specifications indicating any transforms. |
| 162 | X : pandas.DataFrame |
| 163 | Combined train and test data, or just prediction data. |
| 164 | |
| 165 | Returns |
| 166 | ------- |
| 167 | all_features : pandas.DataFrame |
| 168 | All features, including transforms. |
| 169 | |
| 170 | Raises |
| 171 | ------ |
| 172 | IndexError |
| 173 | The number of transform rows must match the number of |
| 174 | rows in ``X``. |
| 175 | |
| 176 | """ |
| 177 | |
| 178 | # Extract model parameters |
| 179 | transforms = model.specs['transforms'] |
| 180 | |
| 181 | # Log input parameters |
| 182 | |
| 183 | logger.info("Original Features : %s", X.columns) |
| 184 | logger.info("Feature Count : %d", X.shape[1]) |
| 185 | |
| 186 | # Iterate through columns, dispatching and transforming each feature. |
| 187 | |
| 188 | logger.info("Applying transforms") |
| 189 | all_features = X |
| 190 | |
| 191 | if transforms: |
| 192 | for fname in transforms: |
| 193 | # find feature series |
| 194 | fcols = [] |
| 195 | for col in X.columns: |
| 196 | if col.split(LOFF)[0] == fname: |
| 197 | fcols.append(col) |
| 198 | # get lag values |
| 199 | lag_values = [] |
| 200 | for item in fcols: |
| 201 | _, _, _, lag = vparse(item) |
| 202 | lag_values.append(lag) |
| 203 | # apply transform to the most recent value |
| 204 | if lag_values: |
| 205 | f_latest = fcols[lag_values.index(min(lag_values))] |
| 206 | features = apply_transform(f_latest, X, transforms[fname]) |
| 207 | if features is not None: |
| 208 | if features.shape[0] == X.shape[0]: |
| 209 | all_features = pd.concat([all_features, features], axis=1) |
| 210 | else: |
| 211 | raise IndexError("The number of transform rows [%d] must match X [%d]" % |
| 212 | (features.shape[0], X.shape[0])) |
no test coverage detected