vastorbit.machine_learning.vast.preprocessing.Scaler¶
- class vastorbit.machine_learning.vast.preprocessing.Scaler(name: str = None, overwrite_model: bool = False, method: Literal['zscore', 'robust_zscore', 'minmax'] = 'zscore')¶
Creates a VAST Scaler object.
Attributes
Many attributes are created during the fitting phase.
For StandardScaler:
mean_: numpy.arrayModel’s features means.
std_: numpy.arrayModel’s features standard deviation.
For MinMaxScaler:
min_: numpy.arrayModel’s features minimums.
max_: numpy.arrayModel’s features maximums.
For RobustScaler:
median_: numpy.arrayModel’s features medians.
mad_: numpy.arrayModel’s features median absolute deviations.
Note
All attributes can be accessed using the
get_attributes()method.Note
Several other attributes can be accessed by using the
get_attributes()method.- Parameters:
name (str, optional) – Name of the model.
overwrite_model (bool, optional) – If set to
True, training a model with the same name as an existing model overwrites the existing model.method (str, optional) –
Method used to scale the data.
zscore:
Scaling using the Z-Score
\[Z_score = (x - avg) / std\]robust_zscore:
Scaling using the Robust Z-Score.
\[Z_rscore = (x - median) / (1.4826 * mad)\]minmax:
Normalization using the Min & Max.
\[Z_minmax = (x - min) / (max - min)\]
Examples
The following examples provide a basic understanding of usage. For more detailed examples, please refer to the Machine Learning or the Examples section on the website.
Load data for machine learning¶
We import
vastorbit:import vastorbit as vo
Hint
By assigning an alias to
vastorbit, we mitigate the risk of code collisions with other libraries. This precaution is necessary because vastorbit uses commonly known function names like “average” and “median”, which can potentially lead to naming conflicts. The use of an alias ensures that the functions fromvastorbitare used as intended without interfering with functions from other libraries.For this example, we will use a dummy dataset.
data = vo.VastFrame( { "values": [1, 1.01, 1.02, 1.05, 1.024], } )
Note
vastorbit offers a wide range of sample datasets that are ideal for training and testing purposes. You can explore the full list of available datasets in the Datasets, which provides detailed information on each dataset and how to use them effectively. These datasets are invaluable resources for honing your data analysis and machine learning skills within the vastorbit environment.
Model Initialization¶
First we import the
Scalermodel:from vastorbit.machine_learning.vast import Scaler
Then we can create the model:
model = Scaler(method = "zscore")
Important
The model name is crucial for the model management system and versioning. It’s highly recommended to provide a name if you plan to reuse the model later.
Model Fitting¶
We can now fit the model:
model.fit(data)
Important
To fit a model, you can directly use the
VastFrameor the name of the relation stored in the database.Model Parameters¶
To fetch the model parameter (mean) you can use:
model.mean_
Similarly for standard deviation:
model.std_
Conversion/Transformation¶
To get the scaled dataset, we can use the
transformmethod. Let us transform the data:model.transform(data)
123valuesDecimal(38, 10)1 -0.642493021 2 1.7371107604 3 -0.0475920756 4 0.1903683025 5 -1.2373939663 Rows: 1-5 | Column: values | Type: decimal(38, 10)Please refer to
transform()for more details on transforming aVastFrame.Similarly, you can perform the inverse transform to get the original features using:
model.inverse_transform(data_transformed)
The variable
data_transformedis the scaled dataset.Model Exporting¶
To Memmodel
model.to_memmodel()
Note
MemModelobjects serve as in-memory representations of machine learning models. They can be used for both in-database and in-memory prediction tasks. These objects can be pickled in the same way that you would pickle ascikit-learnmodel.The preceding methods for exporting the model use
MemModel, and it is recommended to useMemModeldirectly.SQL
To get the SQL query use below:
model.to_sql()
To Python
To obtain the prediction function in Python syntax, use the following code:
X = [[1]] model.to_python()(X)
Hint
The
to_python()method is used to scale the data. For specific details on how to use this method for different model types, refer to the relevant documentation for each model.See also
StandardScaler: Scalar with method set aszscore.RobustScaler: Scalar with method set asrobust_zscore.MinMaxScaler: Scalar with method set asminmax.- __init__(name: str = None, overwrite_model: bool = False, method: Literal['zscore', 'robust_zscore', 'minmax'] = 'zscore') None¶
Methods
__init__([name, overwrite_model, method])contour([nbins, chart])Draws the model's contour plot.
deployInverseSQL([key_columns, ...])Returns the SQL code needed to deploy the inverse model.
deploySQL([X, key_columns, exclude_columns])Returns the SQL code needed to deploy the model.
drop()Drops the model from the VAST DataBase.
export_models(name, path[, kind])Exports machine learning models.
fit(input_relation[, X, return_report])Trains the model.
get_attributes([attr_name])Returns the model attributes.
get_match_index(x, col_list[, str_check])Returns the matching index.
Returns the parameters of the model.
get_plotting_lib([class_name, chart, ...])Returns the first available library (Plotly, Matplotlib) to draw a specific graphic.
import_models(path[, schema, kind])Imports machine learning models.
inverse_transform(vdf[, X])Applies the Inverse Model on a
VastFrame.set_params([parameters])Sets the parameters of the model.
Summarizes the model.
to_binary(path)Exports the model to the VAST Binary format.
Converts the model to an InMemory object that can be used for different types of predictions.
to_python([return_proba, ...])Returns the Python function needed for in-memory scoring without using built-in VAST functions.
to_sql([X, return_proba, ...])Returns the SQL code needed to deploy the model without using built-in VAST functions.
transform([vdf, X])Applies the model on a
VastFrame.Attributes