資源簡介
此文檔使用python語言實現SVM經典機器學習算法,可以幫助初學者更好的掌握。

代碼片段和文件信息
#?-*-?coding:?utf-8?-*-
import?numpy?as?np
import?matplotlib.pyplot?as?plt
from?sklearn?import?svm?datasets
from?load_data?import?dataload
def?make_meshgrid(x?y?h=.02):
????“““Create?a?mesh?of?points?to?plot?in
????Parameters
????----------
????x:?data?to?base?x-axis?meshgrid?on
????y:?data?to?base?y-axis?meshgrid?on
????h:?stepsize?for?meshgrid?optional
????Returns
????-------
????xx?yy?:?ndarray
????“““
????x_min?x_max?=?x.min()?-?1?x.max()?+?1
????y_min?y_max?=?y.min()?-?1?y.max()?+?1
????xx?yy?=?np.meshgrid(np.arange(x_min?x_max?h)
?????????????????????????np.arange(y_min?y_max?h))
????return?xx?yy
def?plot_contours(ax?clf?xx?yy?**params):
????“““Plot?the?decision?boundaries?for?a?classifier.
????Parameters
????----------
????ax:?matplotlib?axes?object
????clf:?a?classifier
????xx:?meshgrid?ndarray
????yy:?meshgrid?ndarray
????params:?dictionary?of?params?to?pass?to?contourf?optional
????“““
????Z?=?clf.predict(np.c_[xx.ravel()?yy.ravel()])
????Z?=?Z.reshape(xx.shape)
????out?=?ax.contourf(xx?yy?Z?**params)
????return?out
#?import?some?data?to?play?with
iris?=?datasets.load_iris()
#?Take?the?first?two?features.?We?could?avoid?this?by?using?a?two-dim?dataset
X?=?iris.data[:?:2]
y?=?iris.target
#?we?create?an?instance?of?SVM?and?fit?out?data.?We?do?not?scale?our
#?data?since?we?want?to?plot?the?support?vectors
C?=?1.0??#?SVM?regularization?parameter
models?=?(svm.SVC(kernel=‘linear‘?C=C)
??????????svm.LinearSVC(C=C)
??????????svm.SVC(kernel=‘rbf‘?gamma=0.7?C=C)
??????????svm.SVC(kernel=‘poly‘?degree=3?C=C))
models?=?(clf.fit(X?y)?for?clf?in?models)
#?title?for?the?plots
titles?=?(‘SVC?with?linear?kernel‘
??????????‘LinearSVC?(linear?kernel)‘
??????????‘SVC?with?RBF?kernel‘
??????????‘SVC?with?polynomial?(degree?3)?kernel‘)
#?Set-up?2x2?grid?for?plotting.
fig?sub?=?plt.subplots(2?2)
plt.subplots_adjust(wspace=0.4?hspace=0.4)
X0?X1?=?X[:?0]?X[:?1]
xx?yy?=?make_meshgrid(X0?X1)
for?clf?title?ax?in?zip(models?titles?sub.flatten()):
????plot_contours(ax?clf?xx?yy
??????????????????cmap=plt.cm.coolwarm?alpha=0.8)
????ax.scatter(X0?X1?c=y?cmap=plt.cm.coolwarm?s=20?edgecolors=‘k‘)
????ax.set_xlim(xx.min()?xx.max())
????ax.set_ylim(yy.min()?yy.max())
????ax.set_xlabel(‘Sepal?length‘)
????ax.set_ylabel(‘Sepal?width‘)
????ax.set_xticks(())
????ax.set_yticks(())
????ax.set_title(title)
plt.show()
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????????62??2018-09-08?20:58??SVM\svm\.spyproject\codest
?????文件?????????64??2018-09-08?20:58??SVM\svm\.spyproject\encoding.ini
?????文件?????????92??2018-09-08?20:58??SVM\svm\.spyproject\vcs.ini
?????文件????????281??2019-03-28?11:52??SVM\svm\.spyproject\workspace.ini
?????文件?????196048??2018-10-25?19:07??SVM\svm\archive.txt
?????文件???????4700??2018-08-28?17:49??SVM\svm\DATA.txt
?????文件???????2490??2018-09-09?15:45??SVM\svm\iris_dataset.py
?????文件???????2703??2018-11-02?15:12??SVM\svm\load_data.py
?????文件??????29322??2018-11-02?16:03??SVM\svm\TestingData.txt
?????文件????????321??2018-09-09?15:45??SVM\svm\train.py
?????文件??????88248??2018-11-02?16:03??SVM\svm\TrainingData.txt
?????文件???????1475??2018-11-02?16:03??SVM\svm\__pycache__\load_data.cpython-36.pyc
?????文件???????4688??2018-09-29?20:07??SVM\svm\人臉識別.py
?????目錄??????????0??2018-09-08?20:58??SVM\svm\.spyproject
?????目錄??????????0??2018-11-02?16:03??SVM\svm\__pycache__
?????目錄??????????0??2018-11-02?15:12??SVM\svm
?????目錄??????????0??2018-09-08?20:58??SVM
-----------?---------??----------?-----??----
???????????????330494????????????????????17
評論
共有 條評論