机器学习基础-支持向量机 SVM-17
生活随笔
收集整理的這篇文章主要介紹了
机器学习基础-支持向量机 SVM-17
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
支持向量機(jī)SVM(Support Vector Machines)
SVM簡單例子
from sklearn import svm x = [[3, 3], [4, 3], [1, 1]] y = [1, 1, -1]model = svm.SVC(kernel='linear') model.fit(x, y) # 打印支持向量 print(model.support_vectors_) # 第2和第0個(gè)點(diǎn)是支持向量 print(model.support_)
SVM-低維映射高維
import matplotlib.pyplot as plt from sklearn import datasets from mpl_toolkits.mplot3d import Axes3D x_data, y_data = datasets.make_circles(n_samples=500, factor=.3, noise=.10) plt.scatter(x_data[:,0], x_data[:,1], c=y_data) plt.show() z_data = x_data[:,0]**2 + x_data[:,1]**2 ax = plt.figure().add_subplot(111, projection = '3d') ax.scatter(x_data[:,0], x_data[:,1], z_data, c = y_data, s = 10) #點(diǎn)為紅色三角形 #顯示圖像 plt.show()
SVM-線性分類
import numpy as np import matplotlib.pyplot as plt from sklearn import svm # 創(chuàng)建40個(gè)點(diǎn) x_data = np.r_[np.random.randn(20, 2) - [2, 2], np.random.randn(20, 2) + [2, 2]] y_data = [0]*20 +[1]*20plt.scatter(x_data[:,0],x_data[:,1],c=y_data) plt.show() #fit the model model = svm.SVC(kernel='linear') model.fit(x_data, y_data) model.coef_ model.intercept_ # 獲取分離平面 plt.scatter(x_data[:,0],x_data[:,1],c=y_data) x_test = np.array([[-5],[5]]) d = -model.intercept_/model.coef_[0][1] k = -model.coef_[0][0]/model.coef_[0][1] y_test = d + k*x_test plt.plot(x_test, y_test, 'k') plt.show() model.support_vectors_ # 畫出通過支持向量的分界線 b1 = model.support_vectors_[0] y_down = k*x_test + (b1[1] - k*b1[0]) b2 = model.support_vectors_[-1] y_up = k*x_test + (b2[1] - k*b2[0]) plt.scatter(x_data[:,0],x_data[:,1],c=y_data) x_test = np.array([[-5],[5]]) d = -model.intercept_/model.coef_[0][1] k = -model.coef_[0][0]/model.coef_[0][1] y_test = d + k*x_test plt.plot(x_test, y_test, 'k') plt.plot(x_test, y_down, 'r--') plt.plot(x_test, y_up, 'b--') plt.show()SVM-非線性分類
import matplotlib.pyplot as plt import numpy as np from sklearn.metrics import classification_report from sklearn import svm # 載入數(shù)據(jù) data = np.genfromtxt("LR-testSet2.txt", delimiter=",") x_data = data[:,:-1] y_data = data[:,-1]def plot():x0 = []x1 = []y0 = []y1 = []# 切分不同類別的數(shù)據(jù)for i in range(len(x_data)):if y_data[i]==0:x0.append(x_data[i,0])y0.append(x_data[i,1])else:x1.append(x_data[i,0])y1.append(x_data[i,1])# 畫圖scatter0 = plt.scatter(x0, y0, c='b', marker='o')scatter1 = plt.scatter(x1, y1, c='r', marker='x')#畫圖例plt.legend(handles=[scatter0,scatter1],labels=['label0','label1'],loc='best')plot() plt.show() # fit the model # C和gamma # 'linear', 'poly', 'rbf', 'sigmoid' model = svm.SVC(kernel='rbf', C=2, gamma=1) model.fit(x_data, y_data) model.score(x_data,y_data) # 獲取數(shù)據(jù)值所在的范圍 x_min, x_max = x_data[:, 0].min() - 1, x_data[:, 0].max() + 1 y_min, y_max = x_data[:, 1].min() - 1, x_data[:, 1].max() + 1# 生成網(wǎng)格矩陣 xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.02),np.arange(y_min, y_max, 0.02))z = model.predict(np.c_[xx.ravel(), yy.ravel()])# ravel與flatten類似,多維數(shù)據(jù)轉(zhuǎn)一維。flatten不會(huì)改變?cè)紨?shù)據(jù),ravel會(huì)改變?cè)紨?shù)據(jù) z = z.reshape(xx.shape)# 等高線圖 cs = plt.contourf(xx, yy, z) plot() plt.show()LFW人臉數(shù)據(jù)集
import matplotlib.pyplot as plt from sklearn.model_selection import train_test_split from sklearn.datasets import fetch_lfw_people from sklearn.model_selection import GridSearchCV from sklearn.metrics import classification_report from sklearn.svm import SVC from sklearn.decomposition import PCA # 載入數(shù)據(jù)lfw_people = fetch_lfw_people(min_faces_per_person=70, resize=0.4) plt.imshow(lfw_people.images[6],cmap='gray') plt.show() # 照片的數(shù)據(jù)格式 n_samples, h, w = lfw_people.images.shape print(n_samples) print(h) print(w) lfw_people.data.shape lfw_people.target target_names = lfw_people.target_names target_names n_classes = lfw_people.target_names.shape[0] x_train, x_test, y_train, y_test = train_test_split(lfw_people.data, lfw_people.target) model = SVC(kernel='rbf', class_weight='balanced') model.fit(x_train, y_train) predictions = model.predict(x_test) print(classification_report(y_test, predictions, target_names=lfw_people.target_names))
PCA降維
調(diào)參
畫圖
總結(jié)
以上是生活随笔為你收集整理的机器学习基础-支持向量机 SVM-17的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 机器学习基础-主成分分析PCA-16
- 下一篇: 机器学习实战-回归算法-18