日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

机器学习基础-支持向量机 SVM-17

發(fā)布時(shí)間:2024/9/15 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 机器学习基础-支持向量机 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降維

# 100個(gè)維度 n_components = 100pca = PCA(n_components=n_components, whiten=True).fit(lfw_people.data)x_train_pca = pca.transform(x_train) x_test_pca = pca.transform(x_test) x_train_pca.shape

model = SVC(kernel='rbf', class_weight='balanced') model.fit(x_train_pca, y_train)

predictions = model.predict(x_test_pca) print(classification_report(y_test, predictions, target_names=target_names))


調(diào)參

param_grid = {'C': [0.1, 1, 5, 10, 100],'gamma': [0.0005, 0.001, 0.005, 0.01], } model = GridSearchCV(SVC(kernel='rbf', class_weight='balanced'), param_grid) model.fit(x_train_pca, y_train) print(model.best_estimator_)

predictions = model.predict(x_test_pca) print(classification_report(y_test, predictions, target_names=target_names))

param_grid = {'C': [0.1, 0.6, 1, 2, 3],'gamma': [0.003, 0.004, 0.005, 0.006, 0.007], } model = GridSearchCV(SVC(kernel='rbf', class_weight='balanced'), param_grid) model.fit(x_train_pca, y_train) print(model.best_estimator_)

predictions = model.predict(x_test_pca) print(classification_report(y_test, predictions, target_names=target_names))


畫圖

# 畫圖,3行4列 def plot_gallery(images, titles, h, w, n_row=3, n_col=5):plt.figure(figsize=(1.8 * n_col, 2.4 * n_row))plt.subplots_adjust(bottom=0, left=.01, right=.99, top=.90, hspace=.35)for i in range(n_row * n_col):plt.subplot(n_row, n_col, i + 1)plt.imshow(images[i].reshape((h, w)), cmap=plt.cm.gray)plt.title(titles[i], size=12)plt.xticks(())plt.yticks(())# 獲取一張圖片title def title(predictions, y_test, target_names, i):pred_name = target_names[predictions[i]].split(' ')[-1]true_name = target_names[y_test[i]].split(' ')[-1]return 'predicted: %s\ntrue: %s' % (pred_name, true_name)# 獲取所有圖片title prediction_titles = [title(predictions, y_test, target_names, i) for i in range(len(predictions))]# 畫圖 plot_gallery(x_test, prediction_titles, h, w)plt.show()

總結(jié)

以上是生活随笔為你收集整理的机器学习基础-支持向量机 SVM-17的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。