PyML(四)——用sklearn训练SVM模型

SVM可以看作是感知机的扩展。在感知机算法中,我们最小化错误分类误差。在SVM中我们的优化目标是最大化间隔。间隔定义为两个分隔超平面(决策界)的距离。

1.用松弛变量解决线性可分的情况(软间隔)

需要调节的主要参数:C

使用变量C,我们可以控制错分类的惩罚量。和logistics不同的是,C越大,对于错分类的惩罚越大。可以通过C控制间隔的宽度,在bias-variance之间找到某种平衡。

# -*- coding: utf-8 -*-
# @Time    : 2018/7/23 13:33
# @Author  : Alan
# @Email   : [email protected]
# @File    : svm_sk1.py
# @Software: PyCharm

from sklearn.svm import SVC
from sklearn import datasets
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import StandardScaler
from matplotlib.colors import ListedColormap

iris = datasets.load_iris()
X = iris.data[:,[2,3]]
y = iris.target
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.3,random_state = 0)
sc = StandardScaler()
sc.fit(X_train)
X_train_std = sc.transform(X_train)
X_test_std = sc.transform(X_test)
X_combined_std = np.vstack((X_train_std,X_test_std))
y_combined_std = np.hstack((y_train,y_test))
svm = SVC(kernel='linear',C=1.0,random_state= 0)
svm.fit(X_train_std,y_train)
def plot_decision_regions(X, y, classifier,test_idx=None, resolution=0.02):
    # setup marker generator and color map
    markers = ('s', 'x', 'o', '^', 'v')
    colors = ('red', 'blue', 'lightgreen', 'cyan','gray')
    cmap = ListedColormap(colors[:len(np.unique(y))])
    # plot the decision surface
    x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1
    x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1
    xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution),
                           np.arange(x2_min, x2_max, resolution))
    Z = classifier.predict(np.array([xx1.ravel(), xx2.ravel()]).T)
    Z = Z.reshape(xx1.shape)
    plt.contourf(xx1, xx2, Z, alpha=0.4, cmap=cmap)
    plt.xlim(xx1.min(), xx1.max())
    plt.ylim(xx2.min(), xx2.max())
    # plot all samples
    #X_test, y_test = X[test_idx, :], y[test_idx]
    for idx, cl in enumerate(np.unique(y)):
        plt.scatter(x=X[y == cl, 0], y=X[y == cl, 1],
                    alpha=0.8, c=cmap(idx),
                    marker=markers[idx], label=cl)
    # highlight test samples
    if test_idx:
        X_test, y_test = X[test_idx, :], y[test_idx]
        plt.scatter(X_test[:,0], X_test[:,1], c='',
                alpha=1.0, linewidth=1, marker='o',
                s=55, label='test set')

plot_decision_regions(X_combined_std,y_combined_std, classifier=svm,test_idx=range(105,150))
plt.xlabel('petal length [standardized]')
plt.ylabel('petal width [standardized]')
plt.legend(loc='upper left')
plt.show()

图片:

2.创建数据集,使用svm解决非线性问题。

引入了核函数的概念,其实就是将低维空间映射到高维空间里去解决线性不可分的问题。西瓜书(周志华,机器学习)里的核函数有讲到:如果原始空间是有限维,即属性数有限,那么一定存在一个高维特征空间使样本可分。简而言之,高维空间中的线性决策界实际上是低维空间的非线性决策界。

kernel=‘rbf’,rbf是高斯核函数,除此之外还有很多核函数。

# -*- coding: utf-8 -*-
# @Time    : 2018/7/23 13:47
# @Author  : Alan
# @Email   : [email protected]
# @File    : svm_sk2.py
# @Software: PyCharm

from sklearn.svm import SVC
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap

''''
创建一个简单的数据集,其中有100个正例,100个负例,用高斯核函数的svm来分类
'''
np.random.seed(0)
#np.random.seed(0)作用:使得随机数据可预测。
X_xor = np.random.randn(200,2)
#np.random.randn 产生正态分布(平均值为0,标准差为1)的样本值
Y_xor = np.logical_xor(X_xor[:,0] > 0, X_xor[:, 1] > 0)
#np.logical_xor 逻辑运算符,返回true或者false
Y_xor = np.where(Y_xor,1,-1)
plt.scatter(X_xor[Y_xor==1, 0], X_xor[Y_xor==1, 1],c='b', marker='x', label='1')
plt.scatter(X_xor[Y_xor==-1, 0], X_xor[Y_xor==-1, 1],c='r', marker='s', label='-1')
plt.ylim(-3.0)
plt.legend()
plt.show()

def plot_decision_regions(X, y, classifier,test_idx=None, resolution=0.02):
    # setup marker generator and color map
    markers = ('s', 'x', 'o', '^', 'v')
    colors = ('red', 'blue', 'lightgreen', 'cyan','gray')
    cmap = ListedColormap(colors[:len(np.unique(y))])
    # plot the decision surface
    x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1
    x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1
    xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution),
                           np.arange(x2_min, x2_max, resolution))
    Z = classifier.predict(np.array([xx1.ravel(), xx2.ravel()]).T)
    Z = Z.reshape(xx1.shape)
    plt.contourf(xx1, xx2, Z, alpha=0.4, cmap=cmap)
    plt.xlim(xx1.min(), xx1.max())
    plt.ylim(xx2.min(), xx2.max())
    # plot all samples
    #X_test, y_test = X[test_idx, :], y[test_idx]
    for idx, cl in enumerate(np.unique(y)):
        plt.scatter(x=X[y == cl, 0], y=X[y == cl, 1],
                    alpha=0.8, c=cmap(idx),
                    marker=markers[idx], label=cl)
    # highlight test samples
    if test_idx:
        X_test, y_test = X[test_idx, :], y[test_idx]
        plt.scatter(X_test[:,0], X_test[:,1], c='',
                alpha=1.0, linewidth=1, marker='o',
                s=55, label='test set')

svm = SVC(kernel='rbf',random_state=0,gamma=0.10,C=10.0)
svm.fit(X_xor,Y_xor)
plot_decision_regions(X_xor,Y_xor,classifier=svm)
plt.legend(loc='upper left')
plt.show()

图片:

3.用高斯核函数对iris数据集训练

用高斯核函数的svm来训练iris数据集,其中参数gamma可以被理解为高斯球面的阶段参数,gamma越大,产生更加
柔和的决策界,但是值得注意的地方是,gamma值较大的模型对训练集效果很大,但其泛化能力一般很差,因此选择
合适的gamma值有助于避免过拟合。
# -*- coding: utf-8 -*-
# @Time    : 2018/7/23 14:56
# @Author  : Alan
# @Email   : [email protected]
# @File    : svm_3.py
# @Software: PyCharm

from sklearn.svm import SVC
from sklearn import datasets
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import StandardScaler
from matplotlib.colors import ListedColormap


'''
用高斯核函数的svm来训练iris数据集,其中参数gamma可以被理解为高斯球面的阶段参数,gamma越大,产生更加
柔和的决策界,但是值得注意的地方是,gamma值较大的模型对训练集效果很大,但其泛化能力一般很差,因此选择
合适的gamma值有助于避免过拟合
'''
iris = datasets.load_iris()
X = iris.data[:,[2,3]]
y = iris.target
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.3,random_state = 0)
sc = StandardScaler()
sc.fit(X_train)
X_train_std = sc.transform(X_train)
X_test_std = sc.transform(X_test)
X_combined_std = np.vstack((X_train_std,X_test_std))
y_combined_std = np.hstack((y_train,y_test))

def plot_decision_regions(X, y, classifier,test_idx=None, resolution=0.02):
    # setup marker generator and color map
    markers = ('s', 'x', 'o', '^', 'v')
    colors = ('red', 'blue', 'lightgreen', 'cyan','gray')
    cmap = ListedColormap(colors[:len(np.unique(y))])
    # plot the decision surface
    x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1
    x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1
    xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution),
                           np.arange(x2_min, x2_max, resolution))
    Z = classifier.predict(np.array([xx1.ravel(), xx2.ravel()]).T)
    Z = Z.reshape(xx1.shape)
    plt.contourf(xx1, xx2, Z, alpha=0.4, cmap=cmap)
    plt.xlim(xx1.min(), xx1.max())
    plt.ylim(xx2.min(), xx2.max())
    # plot all samples
    #X_test, y_test = X[test_idx, :], y[test_idx]
    for idx, cl in enumerate(np.unique(y)):
        plt.scatter(x=X[y == cl, 0], y=X[y == cl, 1],
                    alpha=0.8, c=cmap(idx),
                    marker=markers[idx], label=cl)
    # highlight test samples
    if test_idx:
        X_test, y_test = X[test_idx, :], y[test_idx]
        plt.scatter(X_test[:,0], X_test[:,1], c='',
                alpha=1.0, linewidth=1, marker='o',
                s=55, label='test set')

svm = SVC(kernel='rbf',C=1.0,random_state= 0,gamma=0.2)
svm.fit(X_train_std,y_train)
plot_decision_regions(X_combined_std,y_combined_std, classifier=svm,test_idx=range(105,150))
plt.xlabel('petal length [standardized]')
plt.ylabel('petal width [standardized]')
plt.legend(loc='upper left')
plt.show()

图片:

reference:

《python machine learning》

猜你喜欢

转载自blog.csdn.net/qq_38150441/article/details/81168000