机器学习实战-SVM算法-27
生活随笔
收集整理的這篇文章主要介紹了
机器学习实战-SVM算法-27
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
SVM算法-線性分類
import numpy as np import matplotlib.pyplot as plt from sklearn import svm # 創建40個點 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 # 載入數據 data = np.genfromtxt("LR-testSet2.txt", delimiter=",") x_data = data[:,:-1] y_data = data[:,-1]def plot():x0 = []x1 = []y0 = []y1 = []# 切分不同類別的數據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) # 獲取數據值所在的范圍 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# 生成網格矩陣 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類似,多維數據轉一維。flatten不會改變原始數據,ravel會改變原始數據 z = z.reshape(xx.shape)# 等高線圖 cs = plt.contourf(xx, yy, z) plot() plt.show()SVM-人臉識別
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 # 載入數據lfw_people = fetch_lfw_people(min_faces_per_person=70, resize=0.4) plt.imshow(lfw_people.images[6],cmap='gray') plt.show() # 照片的數據格式 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降維
調參
畫圖
總結
以上是生活随笔為你收集整理的机器学习实战-SVM算法-27的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 机器学习实战-PCA算法-26
- 下一篇: 从零开始数据科学与机器学习算法-数据预处