- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np) O0 [8 ]4 ^( ~. w f4 m& E9 V$ ^ @
import matplotlib.pyplot as plt1 r# o3 p2 q! k! R
3 m) h- A* b. @. \) D4 a* s$ g5 A& i6 Limport utilities : i; h3 ^) A. J4 X4 m
5 w) }& ]4 H2 i. V- A2 q
# Load input data$ G. f" H& |3 J1 H( _' z
input_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
9 o. e! C' l9 g, xX, y = utilities.load_data(input_file)
- D" A1 G+ D6 e1 u6 ]3 V7 G9 C0 ]4 M. q# \) v
###############################################
! @$ H0 q% g, r' K. z# Separate the data into classes based on 'y'
0 W' q- U+ J* E/ J4 j# fclass_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
! O+ s3 r' m! O) zclass_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])6 d, P1 S" }& q9 L8 Y
. ~* I5 O. P3 n% S( e4 n i
# Plot the input data) S/ R1 T$ U' t0 r
plt.figure() u: E+ v: ]; u) ~: V7 c1 L1 a. {- \
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s') f- u2 j' I# h8 ^% ^; b- X
plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')& K: D1 W' G, |* x8 A3 v
plt.title('Input data')
5 a) H* A- L8 a$ U( u: ]
; B7 L" H" y6 x: N8 q################################################ p' Q! S5 [4 _
# Train test split and SVM training' i* I0 G& O/ M$ h5 s4 ?# i
from sklearn import cross_validation
5 U! T+ P. V' u0 [* \from sklearn.svm import SVC% b9 W9 ~5 M: u
. K+ v& o6 |( J. G! X; dX_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)7 S6 _" V- _. U8 j: R6 J {
3 W. Z5 n! {) n. k8 @
#params = {'kernel': 'linear'}. ^% V+ x+ ~" q5 w( Y7 _+ z
#params = {'kernel': 'poly', 'degree': 3}- ^% j( j8 s8 b9 e
params = {'kernel': 'rbf'}* {4 u; s' x9 c2 Q9 a9 O) ?! c0 B
classifier = SVC(**params)4 w& U5 }5 H! H, T$ f" ], s
classifier.fit(X_train, y_train)2 X- @3 A4 B$ G9 u- J! ]
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')
( m7 t* D1 \ u( t8 O
7 S, J3 P& }& h- [9 I5 U" T% c! }y_test_pred = classifier.predict(X_test), M! s0 p2 r. y7 y7 M
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')8 @2 I1 s2 f. w7 K T
9 p3 h1 `# E2 O, k' o Q
###############################################
7 n8 V7 [& z& J2 e) i# |# Evaluate classifier performance s/ l3 @+ B& N9 m: |: _! ?
- B# p V9 s3 r9 H( T
from sklearn.metrics import classification_report
6 l& v" u2 V5 Z8 E' l9 r: r+ ^5 g$ Z/ l5 | C$ {; j
target_names = ['Class-' + str(int(i)) for i in set(y)]
% _ o7 x& }" ^print "\n" + "#"*307 F! y9 Z2 m( B( V6 B N
print "\nClassifier performance on training dataset\n"5 ?; W/ ]/ {2 ]2 c8 n! z
print classification_report(y_train, classifier.predict(X_train), target_names=target_names), o. a6 Q* N8 r7 ?* C1 `
print "#"*30 + "\n"
7 D$ O6 m$ ?) `: A% m7 ] k/ l9 k$ n4 C+ ^8 P8 y4 i( n/ Z
print "#"*302 Y" T* ?' f& v- `; b9 B
print "\nClassification report on test dataset\n"( ?3 i) U( S8 }. ]: Q
print classification_report(y_test, y_test_pred, target_names=target_names)
: Y" `* j- D; U/ G' cprint "#"*30 + "\n"- h8 `) {: g% o
' F* ~1 F# e5 P' N/ H
|
|