8.2.4 SVC分析红酒数据
代码语言:javascript复制#SVC分析红酒数据
def SVC_for_load_wine():
warnings.filterwarnings("ignore")
myutil = util()
X,y = datasets.load_wine().data,datasets.load_wine().target
X1 = datasets.load_wine().data[:,:2]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
for kernel in ['linear','rbf','sigmoid','poly']:
for gamma in ['scale', 'auto',0.1,0.01,0.001]:
clf = SVC(max_iter=100000,kernel=kernel,gamma=gamma)
clf.fit(X_train, y_train)
title = "SVC_红酒数据,kernel=" kernel ",gamma=" str(gamma)
myutil.print_scores(clf,X_train,y_train,X_test,y_test,title)
myutil.plot_learning_curve(SVC(max_iter=100000,kernel=kernel,gamma=gamma),X,y,title)
myutil.show_pic(title)
clf = SVC(max_iter=100000,kernel=kernel,gamma=gamma).fit(X1,y)
myutil.draw_scatter_for_clf(X1,y,clf,title)
输出
代码语言:javascript复制*SVC_红酒数据,kernel=linear,gamma=scale:
100.00%
SVC_红酒数据,kernel=linear,gamma=scale:
91.67%
*SVC_红酒数据,kernel=linear,gamma=auto:
100.00%
SVC_红酒数据,kernel=linear,gamma=auto:
91.67%
*SVC_红酒数据,kernel=linear,gamma=0.1:
100.00%
SVC_红酒数据,kernel=linear,gamma=0.1:
91.67%
*SVC_红酒数据,kernel=linear,gamma=0.01:
100.00%
SVC_红酒数据,kernel=linear,gamma=0.01:
91.67%
*SVC_红酒数据,kernel=linear,gamma=0.001:
100.00%
SVC_红酒数据,kernel=linear,gamma=0.001:
91.67%
SVC_红酒数据,kernel=rbf,gamma=scale:
71.13%
SVC_红酒数据,kernel=rbf,gamma=scale:
66.67%
*SVC_红酒数据,kernel=rbf,gamma=auto:
100.00%
SVC_红酒数据,kernel=rbf,gamma=auto:
50.00%
*SVC_红酒数据,kernel=rbf,gamma=0.1:
100.00%
SVC_红酒数据,kernel=rbf,gamma=0.1:
50.00%
*SVC_红酒数据,kernel=rbf,gamma=0.01:
99.30%
SVC_红酒数据,kernel=rbf,gamma=0.01:
69.44%
SVC_红酒数据,kernel=rbf,gamma=0.001:
83.80%
SVC_红酒数据,kernel=rbf,gamma=0.001:
72.22%
SVC_红酒数据,kernel=sigmoid,gamma=scale:
18.31%
SVC_红酒数据,kernel=sigmoid,gamma=scale:
22.22%
SVC_红酒数据,kernel=sigmoid,gamma=auto:
39.44%
SVC_红酒数据,kernel=sigmoid,gamma=auto:
41.67%
SVC_红酒数据,kernel=sigmoid,gamma=0.1:
39.44%
SVC_红酒数据,kernel=sigmoid,gamma=0.1:
41.67%
SVC_红酒数据,kernel=sigmoid,gamma=0.01:
39.44%
SVC_红酒数据,kernel=sigmoid,gamma=0.01:
41.67%
SVC_红酒数据,kernel=sigmoid,gamma=0.001:
39.44%
SVC_红酒数据,kernel=sigmoid,gamma=0.001:
41.67%
SVC_红酒数据,kernel=poly,gamma=scale:
65.49%
SVC_红酒数据,kernel=poly,gamma=scale:
80.56%
*SVC_红酒数据,kernel=poly,gamma=auto:
100.00%
SVC_红酒数据,kernel=poly,gamma=auto:
91.67%
*SVC_红酒数据,kernel=poly,gamma=0.1:
100.00%
SVC_红酒数据,kernel=poly,gamma=0.1:
91.67%
*SVC_红酒数据,kernel=poly,gamma=0.01:
100.00%
SVC_红酒数据,kernel=poly,gamma=0.01:
91.67%
*SVC_红酒数据,kernel=poly,gamma=0.001:
100.00%
SVC_红酒数据,kernel=poly,gamma=0.001:
91.67%
图(省略)
8.2.4 SVC分析乳腺癌数据
代码语言:javascript复制#SVC分析乳腺癌数据
def SVC_for_load_breast_cancer():
warnings.filterwarnings("ignore")
myutil = util()
X,y = datasets.load_breast_cancer().data,datasets.load_breast_cancer().target
X1 = datasets.load_breast_cancer().data[:,:2]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
for kernel in ['linear','rbf','sigmoid','poly']:
for gamma in ['scale', 'auto',0.1,0.01,0.001]:
clf = SVC(max_iter=100000,kernel=kernel,gamma=gamma)
clf.fit(X_train, y_train)
title = "SVC_乳腺癌数据,kernel=" kernel ",gamma=" str(gamma)
myutil.print_scores(clf,X_train,y_train,X_test,y_test,title)
myutil.plot_learning_curve(SVC(max_iter=100000,kernel=kernel,gamma=gamma),X,y,title)
myutil.show_pic(title)
clf = SVC(max_iter=100000,kernel=kernel,gamma=gamma).fit(X1,y)
myutil.draw_scatter_for_clf(X1,y,clf,title)
输出
代码语言:javascript复制SVC_乳腺癌数据,kernel=linear,gamma=scale:
94.51%
SVC_乳腺癌数据,kernel=linear,gamma=scale:
91.23%
SVC_乳腺癌数据,kernel=linear,gamma=auto:
94.51%
SVC_乳腺癌数据,kernel=linear,gamma=auto:
91.23%
SVC_乳腺癌数据,kernel=linear,gamma=0.1:
94.51%
SVC_乳腺癌数据,kernel=linear,gamma=0.1:
91.23%
SVC_乳腺癌数据,kernel=linear,gamma=0.01:
94.51%
SVC_乳腺癌数据,kernel=linear,gamma=0.01:
91.23%
SVC_乳腺癌数据,kernel=linear,gamma=0.001:
94.51%
SVC_乳腺癌数据,kernel=linear,gamma=0.001:
91.23%
SVC_乳腺癌数据,kernel=rbf,gamma=scale:
91.87%
SVC_乳腺癌数据,kernel=rbf,gamma=scale:
91.23%
*SVC_乳腺癌数据,kernel=rbf,gamma=auto:
100.00%
SVC_乳腺癌数据,kernel=rbf,gamma=auto:
56.14%
SVC_乳腺癌数据,kernel=rbf,gamma=0.1:
100.00%
SVC_乳腺癌数据,kernel=rbf,gamma=0.1:
56.14%
*SVC_乳腺癌数据,kernel=rbf,gamma=0.01:
100.00%
SVC_乳腺癌数据,kernel=rbf,gamma=0.01:
55.26%
SVC_乳腺癌数据,kernel=rbf,gamma=0.001:
98.02%
SVC_乳腺癌数据,kernel=rbf,gamma=0.001:
89.47%
SVC_乳腺癌数据,kernel=sigmoid,gamma=scale:
48.79%
SVC_乳腺癌数据,kernel=sigmoid,gamma=scale:
38.60%
SVC_乳腺癌数据,kernel=sigmoid,gamma=auto:
64.40%
SVC_乳腺癌数据,kernel=sigmoid,gamma=auto:
56.14%
SVC_乳腺癌数据,kernel=sigmoid,gamma=0.1:
64.40%
SVC_乳腺癌数据,kernel=sigmoid,gamma=0.1:
56.14%
SVC_乳腺癌数据,kernel=sigmoid,gamma=0.01:
64.40%
SVC_乳腺癌数据,kernel=sigmoid,gamma=0.01:
56.14%
SVC_乳腺癌数据,kernel=sigmoid,gamma=0.001:
64.40%
SVC_乳腺癌数据,kernel=sigmoid,gamma=0.001:
56.14%
SVC_乳腺癌数据,kernel=poly,gamma=scale:
90.77%
SVC_乳腺癌数据,kernel=poly,gamma=scale:
91.23%
SVC_乳腺癌数据,kernel=poly,gamma=auto:
48.35%
SVC_乳腺癌数据,kernel=poly,gamma=auto:
51.75%
SVC_乳腺癌数据,kernel=poly,gamma=0.1:
35.38%
SVC_乳腺癌数据,kernel=poly,gamma=0.1:
34.21%
SVC_乳腺癌数据,kernel=poly,gamma=0.01:
76.70%
SVC_乳腺癌数据,kernel=poly,gamma=0.01:
79.82%
SVC_乳腺癌数据,kernel=poly,gamma=0.001:
40.44%
SVC_乳腺癌数据,kernel=poly,gamma=0.001:
42.11%
图(省略)
8.3 LinearSVC
8.3.1 LinearSVC类参数、属性和方法
类
class sklearn.svm.LinearSVC(penalty='l2', loss='squared_hinge', *, dual=True, tol=0.0001, C=1.0, multi_class='ovr', fit_intercept=True, intercept_scaling=1, class_weight=None, verbose=0, random_state=None, max_iter=1000)
参数
参数 | 类型 | 解释 |
---|---|---|
C | float, default=1.0 | 正则化参数。正则化的强度与C成反比,必须是严格正的。 |
属性
属性 | 类型 |
---|---|
coef_ | ndarray of shape (1, n_features) if n_classes == 2 else (n_classes, n_features)分配给特征的权重(原始问题中的系数)。这仅在线性内核的情况下可用。 |
intercept_ | ndarray of shape (1,) if n_classes == 2 else (n_classes,)coef_是从遵循liblinear的内部内存布局的raw_coef_派生的只读属性。 |
classes_ | ndarray of shape (n_classes,)决策函数中的常数。唯一类标签。 |
n_iter_ | int在所有类中运行的最大迭代次数 |
方法
decision_function(X) | 计算X中样本的决策函数。 |
---|---|
densify() | 将系数矩阵转换为密集数组格式。 |
decision_function(X) | 预测样本的置信度得分。 |
fit(X, y[, sample_weight]) | 根据给定的训练数据拟合SVM模型。 |
get_params([deep]) | 获取此估计器的参数。 |
predict(X) | 对X中的样品进行分类。 |
score(X, y[, sample_weight]) | 返回给定测试数据和标签的平均精度。 |
set_params(**params) | 设置此估计器的参数。 |
sparsify() | 将系数矩阵转换为稀疏格式。 |
8.3.2 LinearSVC原理(分析make_blobs数据)
代码语言:javascript复制def LinearSVC_Theory():
myutil = util()
X , y = make_blobs(n_samples=50,random_state=6,centers=2)
clf = LinearSVC()
clf.fit(X,y)
plt.scatter(X[:,0],X[:,1],c=y,cmap=plt.cm.Paired,s=30)
ax = plt.gca()
xlim = ax.get_xlim()
ylim = ax.get_ylim()
xx = np.linspace(xlim[0],xlim[1],30)
yy = np.linspace(ylim[0],ylim[1],30)
YY , XX = np.meshgrid(yy,xx)
xy = np.vstack([XX.ravel(),YY.ravel()]).T
Z = clf.decision_function(xy). reshape(XX.shape)
ax.contour(XX,YY,Z,colors='k',levels=[-1,0,1],alpha=0.5,linestyles=['--','-','--']) #绘制等高线
title= u"LinearSVC原理"
myutil.show_pic(title)
8.3.3 LinearSVC分析鸢尾花数据
代码语言:javascript复制#LinearSVC分析鸢尾花数据
def LinearSVC_for_load_iris():
warnings.filterwarnings("ignore")
myutil = util()
X,y = datasets.load_iris().data,datasets.load_iris().target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
clf = LinearSVC()
clf.fit(X_train, y_train)
title = "LinearSVC_鸢尾花数据"
myutil.print_scores(clf,X_train,y_train,X_test,y_test,title)
myutil.plot_learning_curve(LinearSVC(),X,y,title)
myutil.show_pic(title)
输出
代码语言:javascript复制LinearSVC_鸢尾花数据:
95.83%
LinearSVC_鸢尾花数据:
96.67%
8.3.4 LinearSVC分析红酒数据
代码语言:javascript复制#LinearSVC分析红酒数据
def LinearSVC_for_load_wine():
warnings.filterwarnings("ignore")
myutil = util()
X,y = datasets.load_wine().data,datasets.load_wine().target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
clf = LinearSVC()
clf.fit(X_train, y_train)
title = "LinearSVC_红酒数据"
myutil.print_scores(clf,X_train,y_train,X_test,y_test,title)
myutil.plot_learning_curve(LinearSVC(),X,y,title)
myutil.show_pic(title)
X = datasets.load_wine().data[:,:2]
clf = LinearSVC().fit(X,y)
myutil.draw_scatter_for_clf(X,y,clf,title)
输出
代码语言:javascript复制LinearSVC_红酒数据:
90.14%
LinearSVC_红酒数据:
86.11%
8.3.5 LinearSVC分析乳腺癌数据
代码语言:javascript复制#LinearSVC分析乳腺癌数据
def LinearSVC_for_load_breast_cancer():
warnings.filterwarnings("ignore")
myutil = util()
X,y = datasets.load_breast_cancer().data,datasets.load_breast_cancer().target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
clf = LinearSVC()
clf.fit(X_train, y_train)
title = "LinearSVC_乳腺癌数据"
myutil.print_scores(clf,X_train,y_train,X_test,y_test,title)
myutil.plot_learning_curve(LinearSVC(),X,y,title)
myutil.show_pic(title)
X = datasets.load_breast_cancer().data[:,:2]
clf = LinearSVC().fit(X,y)
myutil.draw_scatter_for_clf(X,y,clf,title)
输出
代码语言:javascript复制LinearSVC_乳腺癌数据:
93.19%
LinearSVC_乳腺癌数据:
92.11%
Linear Kernel与LinearSVC差不多,边界都是线性的;
RBF和polynomial Kernel 边界都不是线性的