吳恩達 machine learning 編程作業 python實現 ex6

# -*- coding: utf-8 -*-
"""
Created on Thu Jul  2 20:13:57 2020

@author: cheetah023
"""
import numpy as np
import scipy.io as sci
import matplotlib.pyplot as plt
from sklearn import svm

#函數定義
def plotData(X, y):
    pos = np.where(y == 1)
    neg = np.where(y == 0)
    plt.scatter(X[pos[0],0],X[pos[0],1],marker='o',c='g')
    plt.scatter(X[neg[0],0],X[neg[0],1],marker='x',c='r')
def visualizeBoundary(X,model):
    x1_max = np.max(X[:,0])
    x1_min = np.min(X[:,0])
    x2_max = np.max(X[:,1])
    x2_min = np.min(X[:,1])
    x1_t = np.linspace(x1_min, x1_max, 1000)
    x2_t = np.linspace(x2_min, x2_max, 1000)
    #生成網格型數據,可以接受兩個一維數組生成兩個二維矩陣,對應兩個數組中所有的(x,y)對
    x1,x2 = np.meshgrid(x1_t, x2_t)
    p = model.predict(np.c_[x1.flatten(), x2.flatten()])
    p = p.reshape(x1.shape)
    plt.contour(x1, x2, p)
def gaussianKernel(x1, x2, sigma):
    x1 = np.reshape(x1,[-1,1])
    x2 = np.reshape(x2,[-1,1])
    sim = np.exp(-np.sum((x1-x2) ** 2) / (2 * sigma * sigma))
    return sim
def dataset3Params(X, y, Xval, yval):
    list_vec = [0.01,0.03,0.1,0.3,1,3,10,30]
    C = 0.01
    sigma = 0.01
    score = 0
    for C_t in list_vec:
        for sigma_t in list_vec:
            gamma_t = 1 / (2 * sigma_t * sigma_t)
            model = svm.SVC(C = C_t,kernel='rbf',gamma=gamma_t)
            model.fit(X, y.flatten())
            score_t = model.score(Xval,yval.flatten())
            if score_t > score:
                score = score_t
                sigma = sigma_t
                C = C_t
    return C, sigma
#Part 1: Loading and Visualizing Data
data = sci.loadmat('ex6data1.mat')
#print(data.keys())
X = data['X']
y = data['y']
print('X1:',X.shape)
print('y1:',y.shape)
plt.figure(0)
plotData(X, y)

#Part 2: Training Linear SVM
C_t = 1
#使用的svm裏面自己的核函數
model = svm.SVC(C = C_t,kernel='linear')
model.fit(X, y.flatten())
visualizeBoundary(X,model)
plt.title('SVM Decision Boundary data1  C = 1')

#Part 3: Implementing Gaussian Kernel
x1 = [1, 2, 1]
x2 = [0, 4, -1]
sigma = 2
sim = gaussianKernel(x1, x2, sigma)
print('Gaussian Kernel:',sim)
print('(for sigma = 2, this value should be about 0.324652)')

#Part 4: Visualizing Dataset 2
data = sci.loadmat('ex6data2.mat')
#print(data.keys())
X = data['X']
y = data['y']
print('X2:',X.shape)
print('y2:',y.shape)
plt.figure(1)
plotData(X, y)

#Part 5: Training SVM with RBF Kernel (Dataset 2)
C_t = 1
sigma = 0.1
gamma_t = 1 / (2 * sigma * sigma)
#使用svm自帶的高斯核函數
model = svm.SVC(C = C_t,kernel='rbf',gamma=gamma_t)
model.fit(X, y.flatten())
visualizeBoundary(X,model)
plt.title('SVM Decision Boundary data2  C = 1 sigma = 0.1')

#Part 6: Visualizing Dataset 3
data = sci.loadmat('ex6data3.mat')
#print(data.keys())
X = data['X']
y = data['y']
Xval = data['Xval']
yval = data['yval']
print('X3:',X.shape)
print('y3:',y.shape)
plt.figure(2)
plotData(X, y)

#Part 7: Training SVM with RBF Kernel (Dataset 3)
[C_t, sigma] = dataset3Params(X, y, Xval, yval)
gamma_t = 1 / (2 * sigma * sigma)
model = svm.SVC(C = C_t,kernel='rbf',gamma=gamma_t)
model.fit(X, y.flatten())
visualizeBoundary(X,model)
plt.title('SVM Decision Boundary data3  C = {} sigma = {}'.format(C_t,sigma))

運行結果:

X1: (51, 2)
y1: (51, 1)
Gaussian Kernel: 0.32465246735834974
(for sigma = 2, this value should be about 0.324652)
X2: (863, 2)
y2: (863, 1)
X3: (211, 2)
y3: (211, 1)

 

參考資料:

https://blog.csdn.net/weixin_44027820/article/details/104592429

總結:

1、主要是學習了sklearn 裏面svm自帶的核函數的用法

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章