马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
SVM是一个应用非常广泛的机器学习模型,支持线性和非线性分类,回归分类,边界检查等,特别适用于复杂数据的分类。首先是SVM里的线性SVC模型:import numpy as np
from sklearn import datasets
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import LinearSVC
iris=datasets.load_iris()
X=iris["data"][:,(2,3)]
y=(iris["target"]==2).astype(np.float64)
svm_clf=Pipeline([
("scaler",StandardScaler()),
("linear_svc",LinearSVC(C=1,loss="hinge")),
])
svm_clf.fit(X,y)
还是用上次的鸢尾花数据制作模型,然后随便预测下:svm_clf.predict([[5.5,1.7]])
输出为:array([ 1.])。然后是多项式特征模型:from sklearn.datasets import make_moons
from sklearn.preprocessing import PolynomialFeatures
polynomial_svm_clf=Pipeline([
("poly_features",PolynomialFeatures(degree=3)),
("scaler",StandardScaler()),
("svm_clf",LinearSVC(C=10,loss="hinge"))
])
polynomial_svm_clf.fit(X,y)
这里就不演示预测了,都一样。然后是支撑向量分类模型:from sklearn.svm import SVC
poly_kernel_svm_clf=Pipeline([
("scaler",StandardScaler()),
("svm_clf",SVC(kernel="poly",degree=3,coef0=1,C=5))
])
poly_kernel_svm_clf.fit(X,y)
然后是高斯RBF神经网络模型:rbf_kernel_svm_clf=Pipeline([
("scaler",StandardScaler()),
("svm_clf",SVC(kernel="rbf",gamma=5,C=0.001))
])
poly_kernel_svm_clf.fit(X,y)
支持向量回归模型:from sklearn.svm import SVR
svm_poly_reg=SVR(kernel="poly",degree=2,C=100,epsilon=0.1)
svm_poly_reg.fit(X,y)
|