gramex.ml_api
¶
search_modelclass(mclass)
¶
Search for an ML algorithm or estimator by its name within supported modules.
Each estimator also comes with a wrapper class through which MLHandler can use it. Wrapper classes are: 1. SklearnModel (for subclasses of sklearn.base.{ClassifierMixin, RegressorMixin}) 2. SklearnTransformer (for subclasses of sklearn.base.TransformerMixin) 3. StatsModel (for statsmodels)
Parameters¶
str
Name of a model / estimator / algorithm
Returns¶
tuple of class, wrapper
Raises¶
ImportError If the required class is not found anywhere in the supported modules.
Example¶
klass, wrapper = search_modelclass(‘LogisticRegression’) print(klass)
print(wrapper)
Source code in gramex\ml_api.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
|
coerce_model_params(mclass, params)
¶
Coerce a dictionary of parameters into the types expected by a given class constructor.
This is typically used when hyperparamters are set through HTTP request bodies.
Parameters¶
str
Name of a model / estimator / algorithm
dict
A dictionary containing named parameters and their values used to instantiate mlclass
.
Returns¶
dict
A copy of params
, with values typecasted into the types expected by mlclass
.
Example¶
params = {“C”: “1.0”, max_iter: “100”} # Values are strings coerce_model_params(‘LogisticRegression’, params) {“C”: 1.0, max_iter: 100} # Values are numbers
Source code in gramex\ml_api.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
|
assemble_pipeline(data, target_col, model, nums=None, cats=None, kwargs)
¶
Create an sklearn pipeline to preprocess features.
Parameters¶
pd.DataFrame
The training data.
str
The column name of the target, must be present in data
.
sklearn.base.BaseEstimator
The sklearn estimator which is fitted at the end of the pipeline, after the preprocessing.
list
Numerical columns in data
, to be StandardScaled with the pipeline.
list
Categorical columns in data
, to be OneHotEncoded with the pipeline.
kwargs : Additional parameters for the model constructor.
Returns¶
sklearn.pipeline.Pipleline
An sklearn pipeline containing two steps.
1. A sklearn.compose.ColumnTransformer
step that one-hot encodes categorical variables,
and StandardScales numerical ones.
2. An estimator
Example¶
df = pd.read_csv(‘superstore-sales.csv’, usecols=[‘region’, ‘discount’, ‘profit’]) assemble_pipeline(df, ‘profit’, ‘LogisticRegression’, nums=[‘discount’], cats=[‘region’]) Pipeline(steps=[(‘transform’, ColumnTransformer(transformers=[(‘ohe’, OneHotEncoder(sparse=False), [‘region’]), (‘scaler’, StandardScaler(), [‘discount’])])), (‘LogisticRegression’, LogisticRegression())])
Source code in gramex\ml_api.py
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
|
ModelStore(path, args, kwargs)
¶
A hybrid version of keystore that stores models, data and parameters.
Source code in gramex\ml_api.py
199 200 201 202 203 204 |
|
AbstractModel
¶
Abstract base class for all models supported by MLHandler. MLHandler will assume ONLY this interface.
fit(args, kwargs)
abstractmethod
¶
Fit the model.
Ensure that all variations like partial_fit, or fit called without a target, etc, are sufficiently handled by the concrete implementations.
Source code in gramex\ml_api.py
248 249 250 251 252 253 254 |
|
predict(args, kwargs)
abstractmethod
¶
Get a prediction as a pandas Series.
Source code in gramex\ml_api.py
256 257 258 |
|
get_params(args, kwargs)
abstractmethod
¶
Get the (hyper)parameters of the model.
Source code in gramex\ml_api.py
260 261 262 |
|
score(args, kwargs)
abstractmethod
¶
Score the model against some y_true.
Source code in gramex\ml_api.py
264 265 266 |
|
get_attributes(args, kwargs)
abstractmethod
¶
Get the learned attributes of the model.
Source code in gramex\ml_api.py
268 269 270 |
|
SklearnModel(model, data=None, target_col=None, nums=None, cats=None, params=None, kwargs)
¶
SklearnModel.
Source code in gramex\ml_api.py
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
|
fit(X, y, model_path='', name='', kwargs)
¶
Fit the model.
Parameters¶
array-like
Training features.
array-like
Training labels
str, optional
If specified, the model is saved at this path.
str, optional
Name of the handler instance calling this method.
kwargs : Additional parameters for model.fit
Source code in gramex\ml_api.py
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
|
predict(X, target_col='', kwargs)
¶
Get a prediction.
Parameters¶
array-like
Input features
str, optional
If specified, predictions are added as a column to X
, with this as the column name.
kwargs : Additionnal parameters for model.predict
Source code in gramex\ml_api.py
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
|
SklearnTransformer
¶
SklearnTransformer.