- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np
+ z/ [; i0 m8 Y0 B( ?import matplotlib.pyplot as plt# q* S; Y; }! ?2 W
8 T' W# W: L0 q8 k2 W
import utilities
5 t+ b$ V3 p" U1 {
" \6 o: D+ i$ z7 e& W8 ~5 _# Load input data2 r' _8 h4 d' B2 Q K. c4 l4 v3 n
input_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
7 t, N8 S6 M/ y7 z' ~X, y = utilities.load_data(input_file)1 i! P! y) _$ T; K$ V, u6 U
( w5 U# ~) f# O5 P4 r1 c! n###############################################8 {+ ^' J& ]0 F& Q# Q
# Separate the data into classes based on 'y'
: K& J3 C5 d( o" s( n% b" N- kclass_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
/ R# {' s' u0 A9 o$ eclass_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])7 v6 U6 O. _+ r" G+ c/ d
* V6 |6 L# l) o6 B |2 `" m+ v# Plot the input data
4 F5 D: a/ Y3 w. J7 g- s Nplt.figure()9 w$ x+ a% o! [: A
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')) O2 V1 Q6 T h. u
plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')4 |1 b3 f, Q- G2 g; `6 ?
plt.title('Input data'); D9 R9 @7 \7 \ @* Q3 i# ]2 a
8 X! y2 j) l+ W' |
###############################################/ D$ Q1 a# M1 |; n4 k6 q
# Train test split and SVM training, k; [/ a7 j' R8 C9 H7 ^6 [& ]
from sklearn import cross_validation
- d/ m. q) { M( R8 R. ^, kfrom sklearn.svm import SVC
5 Q5 W ^. [& b0 U" \* _. d, T$ e2 C: _8 B# d% @
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
' M+ i* v. Z" I$ z: I! K) k& i( j& ~" j
#params = {'kernel': 'linear'}& s# I$ S( U3 d9 K+ y+ A$ T+ }) d q) K
#params = {'kernel': 'poly', 'degree': 3}1 l9 {6 ]# I$ Z8 e/ @2 T$ Q( B
params = {'kernel': 'rbf'}) \! X4 S. ^1 U2 R
classifier = SVC(**params)# Y5 Z8 |2 |, {; @* ?& s
classifier.fit(X_train, y_train)
2 E! }% f* j X2 n) D$ {utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')
/ l3 {% S/ M9 E$ L* ]. b; ~( D+ y- E- s7 a
y_test_pred = classifier.predict(X_test)2 L; l- y9 e2 ~$ g5 e4 {+ W; t: ^
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')
# L: G; _# W: [" B" X
9 c$ f% @" A- l# j( R, ?: a _. ?###############################################
8 l9 s/ p1 U4 s, ?3 `# Evaluate classifier performance" b7 u) Q7 k$ ]5 _* e' V# |
7 k2 y5 A& q; Z6 A3 Rfrom sklearn.metrics import classification_report8 e# L1 o* \3 P
2 u J, b% z) |. {1 Q8 X5 T0 d0 @
target_names = ['Class-' + str(int(i)) for i in set(y)]1 v; l! G( J5 o/ F5 l; J
print "\n" + "#"*30
5 r( l# d1 n& K7 zprint "\nClassifier performance on training dataset\n" D' x/ | ~% t0 f6 w0 {
print classification_report(y_train, classifier.predict(X_train), target_names=target_names)
% p: E; Q5 e6 s: S" ~print "#"*30 + "\n"7 t* x7 n3 ]; A$ a# [1 ~
% w$ } t$ o- w+ Z% `
print "#"*30- N- M# U+ o: x* u
print "\nClassification report on test dataset\n"7 }5 P* B/ @' W+ g- w
print classification_report(y_test, y_test_pred, target_names=target_names)' ]7 S" z7 w$ m( P8 R+ U
print "#"*30 + "\n"
6 U4 [8 B/ t8 }; e# ]: J) D4 \/ j/ g3 q g8 o0 g
|
|