After preparing the data for a classification model, you can train the model.
Review and complete all the tasks to Prepare Data for a Classification Model.
Train the model with the
ML_TRAIN routine and use the
training_data table previously created.
Before training the model, it is good practice to define the
model handle instead of automatically creating one. See
Define Model Handle.
-
Optionally, set the value of the session variable, which sets the model handle to this same value.
mysql> SET @variable = 'model_handle';Replace
@variableandmodel_handlewith your own definitions. For example:mysql> SET @model='classification_use_case';The model handle is set to
classification_use_case. -
Run the
ML_TRAINroutine.mysql> CALL sys.ML_TRAIN('table_name', 'target_column_name', JSON_OBJECT('task', 'task_name'), model_handle);Replace
table_name,target_column_name,task_name, andmodel_handlewith your own values.The following example runs
ML_TRAINon the training dataset previously created.mysql> CALL sys.ML_TRAIN('classification_data.Loan_Training', 'Approved', JSON_OBJECT('task', 'classification'), @model);Where:
classification_data.Loan_Trainingis the fully qualified name of the table that contains the training dataset (database_name.table_name).Approvedis the name of the target column, which contains ground truth values.JSON_OBJECT('task', 'classification')specifies the machine learning task type.@modelis the session variable previously set that defines the model handle to the name defined by the user:classification_use_case. If you do not define the model handle before training the model, the model handle is automatically generated, and the session variable only stores the model handle for the duration of the connection. User variables are written as@. Any valid name for a user-defined variable is permitted. See Work with Model Handles to learn more.var_name
-
When the training operation finishes, the model handle is assigned to the
@modelsession variable, and the model is stored in the model catalog. View the entry in the model catalog with the following query. Replaceuser1with your MySQL account name.mysql> SELECT model_id, model_handle, train_table_name FROM ML_SCHEMA_user1.MODEL_CATALOG WHERE model_handle = 'classification_use_case'; +----------+----------------------------------------------+-------------------------------------+ | model_id | model_handle | train_table_name | +----------+----------------------------------------------+-------------------------------------+ | 1 | classification_use_case | classification_data.Loan_Training | +----------+----------------------------------------------+-------------------------------------+
Learn how to Generate Predictions for a Classification Model.