memModel¶
memModel(model_type: str,
attributes: dict,)
Creates platform-independent machine learning models that you can export as SQL or Python code for deployment in other environments.
Parameters¶
Name | Type | Optional | Description |
---|---|---|---|
model_type | str | ❌ | The model type, one of the following: 'BinaryTreeClassifier', 'BinaryTreeRegressor', 'BisectingKMeans', 'CHAID', 'KMeans', 'LinearSVC', 'LinearSVR', 'LinearRegression', 'LogisticRegression', 'NaiveBayes', 'NearestCentroid', 'Normalizer', 'OneHotEncoder', 'PCA', 'RandomForestClassifier', 'RandomForestRegressor', 'SVD', 'XGBoostClassifier', 'XGBoostRegressor'. |
attributes | dict | ❌ | Dictionary which includes all the model's attributes.
|
Attributes¶
After the object is created, the following parameters become attributes of the model.
Name | Type | Description |
---|---|---|
attributes_ | dict | Model's attributes. |
model_type_ | str | Model's type. |
represent_ | str | Model's summary. |
Methods¶
Name | Description |
---|---|
get_attributes | Returns model's attributes. |
plot_tree | Draws the input tree. Requires the graphviz module. |
predict | Predicts using the model's attributes. |
predict_proba | Predicts probabilities using the model's attributes. |
predict_proba_sql | Returns the SQL code needed to deploy the probabilities model. |
predict_sql | Returns the SQL code needed to deploy the model. |
rotate | Performs a Oblimin (Varimax, Quartimax) rotation on the the model's PCA matrix. |
set_attributes | Sets new model's attributes. |
to_graphviz | Returns the code for a Graphviz tree. |
transform | Transforms the data using the model's attributes. |
transform_sql | Returns the SQL code needed to deploy the model. |
Example¶
Building a memModel
memModels are defined entirely by their attributes. For example, the following attributes define a linear regression model:
coefficients = [0.5, 1.2]
intercept = 2.0
To build a linear regression model from its memModel, specify the model type and its attributes:
from verticapy.learn.memmodel import memModel
model = memModel(model_type = "LinearRegression",
attributes = {"coefficients": coefficients,
"intercept": intercept})
model
Making predictions
The predict method returns the model's predictions on a given set of data:
model.predict([[1.0, 0.3],
[2.0, -0.6]])
The predict_sql method generates the SQL code for deploying the model in Veritca.
model.predict_sql(["x_1", "x_2"])
Computing class probabilities
Multiclass classifiers calculate the probabilities for each class. The BinaryTreeClassifier is one such classifier:
model = memModel("BinaryTreeClassifier", {"children_left": [1, 3, None, None, None],
"children_right": [2, 4, None, None, None],
"feature": [0, 1, None, None, None],
"threshold": ['female', 30, None, None, None],
"value": [None, None, [0.8, 0.1, 0.1], [0.1, 0.8, 0.1], [0.2, 0.2, 0.6]],
"classes": ['a', 'b', 'c',]})
model.predict_proba([['female', 500.0],
['male', 300.0],
['female', 6.0],])
The predict_proba_sql method generates the SQL code for deploying the BinaryTreeClassifier model:
model.predict_proba_sql(["sex", "fare"])
Preprocessing new data
Transformer models have the 'transform' method to preprocess new data in-memory:
model = memModel("Normalizer", {"values": [(0.4, 0.5), (0.3, 0.2),],
"method": "minmax"})
model.transform([[1.0, 0.3],
[2.0, -0.6]])
To deploy a transformer model with SQL, use 'transform_sql':
model.transform_sql(["x_1", "x_2"])
Special methods
PCA memModels have the special 'rotate' method:
model = memModel("PCA", {"principal_components": [[0.4, 0.5], [0.3, 0.2],],
"mean": [0.1, 0.3]})
model.rotate()
model.get_attributes()
Trees have special methods 'to_graphviz' and 'plot_tree':
model = memModel("BinaryTreeClassifier", {"children_left": [1, 3, None, None, None],
"children_right": [2, 4, None, None, None],
"feature": [0, 1, None, None, None],
"threshold": ['female', 30, None, None, None],
"value": [None, None, [0.8, 0.1, 0.1], [0.1, 0.8, 0.1], [0.2, 0.2, 0.6]],
"classes": ['a', 'b', 'c',]})
model.to_graphviz()
model.plot_tree()