使用 SVM(支持向量机)对数据进行分类
该 SVM 示例使用名为 mtcars 的小数据集。该示例展示了如何使用 SVM_CLASSIFIER 函数训练模型以使用 PREDICT_SVM_CLASSIFIER 函数预测 am
的值(挡位类型,其中 0 = 自动挡,1 = 手动挡)。
-
使用
mtcars_train
训练数据创建名为svm_class
的 SVM 模型。=> SELECT SVM_CLASSIFIER('svm_class', 'mtcars_train', 'am', 'cyl, mpg, wt, hp, gear' USING PARAMETERS exclude_columns='gear');
SVM_CLASSIFIER
---------------------------------------------------------------- Finished in 12 iterations. Accepted Rows: 20 Rejected Rows: 0 (1 row)
-
查看
svm_class
的摘要输出。=> SELECT GET_MODEL_SUMMARY(USING PARAMETERS model_name='svm_class'); ------------------------------------------------------------------------ ======= details ======= predictor|coefficient ---------+----------- Intercept| -0.02006 cyl | 0.15367 mpg | 0.15698 wt | -1.78157 hp | 0.00957 =========== call_string =========== SELECT svm_classifier('public.svm_class', 'mtcars_train', '"am"', 'cyl, mpg, wt, hp, gear' USING PARAMETERS exclude_columns='gear', C=1, max_iterations=100, epsilon=0.001); =============== Additional Info =============== Name |Value ------------------+----- accepted_row_count| 20 rejected_row_count| 0 iteration_count | 12 (1 row)
-
创建名为
svm_mtcars_predict
的新表。使用通过在测试数据上运行PREDICT_SVM_CLASSIFIER
函数获得的预测结果来填充表。=> CREATE TABLE svm_mtcars_predict AS (SELECT car_model, am, PREDICT_SVM_CLASSIFIER(cyl, mpg, wt, hp USING PARAMETERS model_name='svm_class') AS Prediction FROM mtcars_test); CREATE TABLE
-
在
svm_mtcars_predict
表中查看结果。=> SELECT * FROM svm_mtcars_predict; car_model | am | Prediction ------------- +----+------------
Toyota Corona | 0 | 1 Camaro Z28 | 0 | 0 Datsun 710 | 1 | 1 Valiant | 0 | 0 Volvo 142E | 1 | 1 AMC Javelin | 0 | 0 Honda Civic | 1 | 1 Hornet 4 Drive| 0 | 0 Maserati Bora | 1 | 1 Merc 280 | 0 | 0 Merc 450SL | 0 | 0 Porsche 914-2 | 1 | 1 (12 rows)
-
使用
PREDICT_SVM_CLASSIFIER
评估函数来评估 CONFUSION_MATRIX 函数的准确性。=> SELECT CONFUSION_MATRIX(obs::int, pred::int USING PARAMETERS num_classes=2) OVER() FROM (SELECT am AS obs, Prediction AS pred FROM svm_mtcars_predict) AS prediction_output; class | 0 | 1 | comment -------+---+---+--------------------------------------------- 0 | 6 | 1 | 1 | 0 | 5 | Of 12 rows, 12 were used and 0 were ignored (2 rows)
在本例中,
PREDICT_SVM_CLASSIFIER
正确预测在am
列中值为1
的汽车具有1
值。没有汽车被错误分类。在0
列中值为am
的七辆汽车中,正确预测了六辆具有值0
。将一辆汽车错误地分类为具有值1
。