C12.1
线性SVM与非线性SVM分类器(支持向量机)
教材页 第12章,12.5节,第323页,支持向量机分类
数据

数据集1:线性可分数据集
JLinearTwoClass.txt,(附JLinearTwoClass.rar压缩包格式)下载后存储到D:\下,如果是其它路径,则需要修改程序中文件的路径。
数据各属性含义:类别, x1属性, x2属性
分类问题:预测样例类别。(二分类问题)。可以绘制散点图,观察两类样本在空间中的分布。
说明:本数据来自《数据分析与数据挖掘建模与工具》,版权所有。这里仅用于教学工作。

数据集2:
鸢尾花数据集

任务 建立支持向量机分类模型。
Python

import numpy as np;
from sklearn import svm;
ds = np.loadtxt("D:\\JLinearTwoClass.txt", delimiter = "\t");
X = ds[:,1:3];
y = ds[:,0];
#print (X);
print(y);
m = svm.LinearSVC(C = 1.0, multi_class = "ovr", fit_intercept=True, max_iter = 100000);
m.fit(X, y);
pred = m.predict(X);
print(pred);
w, b = m.coef_, m.intercept_;
print(w, b);

Python

鸢尾花

from sklearn import datasets
from sklearn import svm
from sklearn.model_selection import train_test_split
import numpy as np
iris = datasets.load_iris()
print(type(iris),dir(iris))
y = iris.target
X = iris.data[:,:2]
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size = 0.3, random_state = 0)
lin_svc = svm.SVC(decision_function_shape='ovo', kernel = 'rbf', gamma = 'auto') #径向基核函数
lin_svc.fit(X_train,y_train)
rbf_svc = svm.SVC(decision_function_shape='ovo',kernel='linear')
rbf_svc.fit(X_train, y_train)
poly_svc = svm.SVC(decision_function_shape = 'ovo', kernel = 'poly',degree = 3) #多项式核函数
poly_svc.fit(X_train, y_train)
acc_lin_svc = lin_svc.score(X_test,y_test)
acc_rbf_svc = rbf_svc.score(X_test,y_test)
acc_poly_svc = poly_svc.score(X_test,y_test)
lin_svc_pre = lin_svc.predict(X_test)
rbf_svc_pre = rbf_svc.predict(X_test)
poly_svc_pre = poly_svc.predict(X_test)
print('acc_lin_svc: ',acc_lin_svc)
print('acc_lin_predicted: ',lin_svc_pre)
print('acc_rbf_svc: ',acc_rbf_svc)
print('acc_rbf_predicted: ',rbf_svc_pre)
print('acc_poly_svc: ',acc_poly_svc)
print('acc_poly_predicted: ',poly_svc_pre)
C++ #include "orsci.h"
#include "orsci_dm.h"
using namespace orsci;
using namespace dm;

mdouble X = dmt::dataset::JLinearTwoClass::JLinearTwoClass_X();
vint y = dmt::dataset::JLinearTwoClass::JLinearTwoClass_y();
//cout << "真实标记..." << endl;
cout << y << endl;

dm::TSVM_Linear_TwoClass m;
const double mC = 0;
m.train(y, X, mC, 0, false);
cout << "预测标记..." << endl;
cout << m.predict(X).T() << endl;
cout << m.predict_value(X).T() << endl;

cout << dm::TSVM_Linear_TwoClass::kCloseTest(y, X, mC) << endl;
cout << dm::TSVM_Linear_TwoClass::kFold(y, X, mC, 10, true, false, 29104612) << endl;

输出 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[[2.18052988 1.05881974]] [-1.5678957]
书籍 姜维.《数据分析与数据挖掘》、《数据分析与数据挖掘建模与工具》,电子工业出版社, 2023,2024。
软件 Python,C++(附加orsci包)。