标签:磁盘 pca icc style near 绘制 sep etc axis
1 2 7、代码测试 3 4 #!/usr/bin/python 5 # encoding: utf-8 6 7 8 """ 9 @author : 杜函敏 10 @contact : duhanmin@foxmail.com 11 @File : SVMImplementation.py 12 @time : 2017/7/24 23:14 13 14 """ 15 import numpy as np 16 import pylab as pl 17 from sklearn import svm 18 19 20 np.random.seed(0) 21 X = np.r_[np.random.randn(20, 2) - [2, 2], np.random.randn(20, 2) + [2, 2]] 22 Y = [0] * 20 + [1] * 20 23 24 25 clf = svm.SVC(kernel=‘linear‘) 26 clf.fit(X, Y) 27 28 #得到的是几何形式,我们要转化为点斜式方程 29 w = clf.coef_[0] 30 a = -w[0] / w[1] 31 xx = np.linspace(-5, 5) 32 yy = a * xx - (clf.intercept_[0]) / w[1] 33 34 b = clf.support_vectors_[0] 35 yy_down = a * xx + (b[1] - a * b[0]) 36 b = clf.support_vectors_[-1] 37 yy_up = a * xx + (b[1] - a * b[0]) 38 39 40 print ("w: ", w) 41 print ("a: ", a) 42 print ("support_vectors_: ", clf.support_vectors_) 43 print ("clf.coef_: ", clf.coef_) 44 45 pl.plot(xx, yy, ‘k-‘) 46 pl.plot(xx, yy_down, ‘k--‘) 47 pl.plot(xx, yy_up, ‘k--‘) 48 49 pl.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1], 50 s=80, facecolors=‘none‘) 51 pl.scatter(X[:, 0], X[:, 1], c=Y, cmap=pl.cm.Paired) 52 53 pl.axis(‘tight‘) 54 pl.show()
1 #!/usr/bin/python 2 # encoding: utf-8 3 4 5 """ 6 @author : 杜函敏 7 @contact : duhanmin@foxmail.com 8 @File : SVMImplementation.py 9 @time : 2017/7/24 23:14 10 """ 11 12 from __future__ import print_function 13 14 from time import time 15 import logging 16 import matplotlib.pyplot as plt 17 18 from sklearn.model_selection import train_test_split 19 from sklearn.datasets import fetch_lfw_people 20 from sklearn.model_selection import GridSearchCV 21 from sklearn.metrics import classification_report 22 from sklearn.metrics import confusion_matrix 23 from sklearn.decomposition import PCA 24 from sklearn.svm import SVC 25 26 plt.rcParams[‘font.sans-serif‘] = [‘SimHei‘] #指定默认字体 27 plt.rcParams[‘axes.unicode_minus‘] = False #解决保存图像是负号‘-‘显示为方块的问题 28 29 30 #在控制台上显示日志 31 logging.basicConfig(level=logging.INFO, format=‘%(asctime)s %(message)s‘) 32 33 ############################################################################### 34 # 下载数据,如果尚未在磁盘上,并将其作为numpy数组加载 35 36 lfw_people = fetch_lfw_people(min_faces_per_person=70, resize=0.4) 37 38 # shape求图片矩阵的维度大小 39 # n_samples图数量 h,w图的大小 40 n_samples, h, w = lfw_people.images.shape 41 42 # data中为每个图片矩阵的特征向量(列) 43 X = lfw_people.data 44 n_features = X.shape[1] 45 46 # 提取不同人的身份标记 47 y = lfw_people.target 48 49 # 提取人的名字 50 target_names = lfw_people.target_names 51 n_classes = target_names.shape[0] 52 53 print("*************总数据集大小**********************") 54 print("**图数量: %d" % n_samples) 55 print("**特征向量数: %d" % n_features) 56 print("**人数: %d" % n_classes) 57 print("*************总数据集大小**********************") 58 59 60 ############################################################################### 61 # 分训练和测试集 62 X_train, X_test, y_train, y_test = train_test_split( 63 X, y, test_size=0.25) 64 ############################################################################### 65 # 特征提取/降维 66 n_components = 150 67 print("从 %d 个维度中提取到 %d 维度" % (X_train.shape[0],n_components)) 68 #主成分分析建模 69 pca = PCA(n_components=n_components, whiten=True).fit(X_train) 70 eigenfaces = pca.components_.reshape((n_components, h, w)) 71 print("根据主成分进行降维开始") 72 X_train_pca = pca.transform(X_train) 73 X_test_pca = pca.transform(X_test) 74 print("降维结束") 75 ############################################################################### 76 # 训练SVM 77 print("训练SVM分类模型开始") 78 t0 = time() 79 #构建归类精确度5x6=30 80 param_grid = {‘C‘: [1e3, 5e3, 1e4, 5e4, 1e5], 81 ‘gamma‘: [0.0001, 0.0005, 0.001, 0.005, 0.01, 0.1], } 82 #图片用rbf核函数,权重自动选取 83 clf = GridSearchCV(SVC(kernel=‘rbf‘, class_weight=‘balanced‘), param_grid) 84 clf = clf.fit(X_train_pca, y_train) 85 print("SVM训练结束,结果如下:" "SVM训练用时 %0.3fs" % (time() - t0)) 86 print(clf.best_estimator_) 87 88 # ############################################################################### 89 print("测试集SVM分类模型开始") 90 91 t0 = time() 92 y_pred = clf.predict(X_test_pca) 93 print("测试集用时 %0.3fs" % (time() - t0)) 94 95 print("误差衡量") 96 # 数据中1的个数为a,预测1的次数为b,预测1命中的次数为c 97 # 准确率 precision = c / b 98 # 召回率 recall = c / a 99 # f1_score = 2 * precision * recall / (precision + recall) 100 101 print(classification_report(y_test, y_pred, target_names=target_names)) 102 print("预测值和实际值对角矩阵") 103 print(confusion_matrix(y_test, y_pred, labels=range(n_classes))) 104 105 106 # ############################################################################### 107 # 画图 108 def plot_gallery(images, titles, h, w, n_row=3, n_col=4): 109 plt.figure(figsize=(1.8 * n_col, 2.4 * n_row)) 110 plt.subplots_adjust(bottom=0, left=.01, right=.99, top=.90, hspace=.35) 111 for i in range(n_row * n_col): 112 plt.subplot(n_row, n_col, i + 1) 113 plt.imshow(images[i].reshape((h, w)), cmap=plt.cm.gray) 114 plt.title(titles[i], size=12) 115 plt.xticks(()) 116 plt.yticks(()) 117 118 # 绘制一部分测试集上的预测结果 119 def title(y_pred, y_test, target_names, i): 120 ##以空格为分隔符,把y_pred分成一个list。分割的次数1。[-1]取最后一个 121 pred_name = target_names[y_pred[i]].rsplit(‘ ‘, 1)[-1] 122 true_name = target_names[y_test[i]].rsplit(‘ ‘, 1)[-1] 123 return ‘预测值: %s\n 实际值: %s‘ % (pred_name, true_name) 124 125 prediction_titles = [title(y_pred, y_test, target_names, i) 126 for i in range(y_pred.shape[0])] 127 128 plot_gallery(X_test, prediction_titles, h, w) 129 130 # 画人脸咯,eigenfaces主成分特征脸 131 eigenface_titles = ["特征脸 %d" % i for i in range(eigenfaces.shape[0])] 132 plot_gallery(eigenfaces, eigenface_titles, h, w) 133 134 plt.show()
标签:磁盘 pca icc style near 绘制 sep etc axis
原文地址:http://www.cnblogs.com/duhanmin/p/7247799.html