1.项目介绍
机器学习(Machine Learning)正在广泛应用于各种领域,不断学习,充实自己,才能跟上步伐。
在Gihub发现了一个项目,作者写了自己的学习过程,图文并茂,由浅入深,有完整的Python代码,非常值得借鉴。但英文读起来不方便,因此,我和小伙伴一起,把它翻译成了中文,截至目前,包含以下内容:
-
数据预处理
-
简单线性回归
-
多元线性回归
-
逻辑回归
-
k近邻法(k-NN)
-
支持向量机(SVM)
-
决策树
-
随机森林
项目地址:
https://github.com/MachineLearning100/100-Days-Of-ML-Code
2.例子
支持向量机(support vector machine):
Python代码:
1 #Day13: Support Vector Machine (SVM) 2 3 4 5 #Importing the libraries 6 7 import numpy as np 8 9 import matplotlib.pyplot as plt 10 11 import pandas as pd 12 13 14 15 #Importing the dataset 16 17 dataset = pd.read_csv('../datasets/Social_Network_Ads.csv') 18 19 X = dataset.iloc[:, [2, 3]].values 20 21 y = dataset.iloc[:, 4].values 22 23 24 25 #Splitting the dataset into the Training set and Test set 26 27 from sklearn.cross_validation import train_test_split 28 29 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0) 30 31 32 33 #Feature Scaling 34 35 from sklearn.preprocessing import StandardScaler 36 37 sc = StandardScaler() 38 39 X_train = sc.fit_transform(X_train) 40 41 X_test = sc.transform(X_test) 42 43 44 45 #Fitting SVM to the Training set 46 47 from sklearn.svm import SVC 48 49 classifier = SVC(kernel = 'linear', random_state = 0) 50 51 classifier.fit(X_train, y_train) 52 53 54 55 #Predicting the Test set results 56 57 y_pred = classifier.predict(X_test) 58 59 60 61 #Making the Confusion Matrix 62 63 from sklearn.metrics import confusion_matrix 64 65 cm = confusion_matrix(y_test, y_pred) 66 67 68 69 #Visualising the Training set results 70 71 from matplotlib.colors import ListedColormap 72 73 X_set, y_set = X_train, y_train 74 75 X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01), 76 77 np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01)) 78 79 plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape), 80 81 alpha = 0.75, cmap = ListedColormap(('red', 'green'))) 82 83 plt.xlim(X1.min(), X1.max()) 84 85 plt.ylim(X2.min(), X2.max()) 86 87 for i, j in enumerate(np.unique(y_set)): 88 89 plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1], 90 91 c = ListedColormap(('red', 'green'))(i), label = j) 92 93 plt.title('SVM (Training set)') 94 95 plt.xlabel('Age') 96 97 plt.ylabel('Estimated Salary') 98 99 plt.legend() 100 101 plt.show() 102 103 104 105 #Visualising the Test set results 106 107 from matplotlib.colors import ListedColormap 108 109 X_set, y_set = X_test, y_test 110 111 X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01), 112 113 np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01)) 114 115 plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape), 116 117 alpha = 0.75, cmap = ListedColormap(('red', 'green'))) 118 119 plt.xlim(X1.min(), X1.max()) 120 121 plt.ylim(X2.min(), X2.max()) 122 123 for i, j in enumerate(np.unique(y_set)): 124 125 plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1], 126 127 c = ListedColormap(('red', 'green'))(i), label = j) 128 129 plt.title('SVM (Test set)') 130 131 plt.xlabel('Age') 132 133 plt.ylabel('Estimated Salary') 134 135 plt.legend() 136 137 plt.show()
输出结果: