- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np* O6 C B" f( x {2 _( b+ Y
import matplotlib.pyplot as plt
$ `8 B& ?! J a g `4 w
3 O' F" ^( k1 J* P$ F8 @ _" Zimport utilities
) ^! `+ q! k7 s2 C
* P; Y! ]5 S9 k0 c- [8 P6 F# Load input data5 s. G5 m2 [1 O( F) q* E- ?) g
input_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'. o, d- m- y, S" K4 u& C% e( W
X, y = utilities.load_data(input_file)
! z7 R# V/ B0 p1 o8 x( Y0 s0 @9 N, D% t3 C! W/ F/ Q! F
###############################################
& u* K* N% a- f# Separate the data into classes based on 'y'7 [, M# ~8 a3 ^+ k
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
2 }4 a; ?8 I# Xclass_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
% x& ], K6 }( G2 F3 g" A# z# z2 s l' o h) ~
# Plot the input data8 g( w) p( d, d1 B
plt.figure(). L: ]9 r8 A: z
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
- Z O3 d" i6 \ ]3 ?4 z& tplt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')" }" V0 D& F$ Q) D
plt.title('Input data')% [5 v: i0 m4 |$ R! h1 a2 S
" h# `, m, w* R6 u1 Z* p
###############################################
+ v, a }- _. X7 j# Train test split and SVM training
1 C2 j" M9 w/ M* @* M2 y/ Z4 Sfrom sklearn import cross_validation7 ]- B' I" K8 ~& h" a* A0 Z9 b
from sklearn.svm import SVC: g2 ~4 l! r0 P' B4 P
, q' |' j- l) S- m+ n0 B$ d# \' zX_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)8 n/ u$ u2 u) z8 w7 H
2 _( [% o4 u5 a- a: s
#params = {'kernel': 'linear'}9 `' S3 @9 U* k: I, f! q4 w
#params = {'kernel': 'poly', 'degree': 3}" s& m5 l. L% V) f+ ]9 k5 k- z, F" }; P
params = {'kernel': 'rbf'}$ [" g( U. @* X/ R3 e% \1 m
classifier = SVC(**params)
% H( z1 Y. a5 S9 K; L+ fclassifier.fit(X_train, y_train)+ P7 a! U, Z0 g5 A. u5 s- e! Q
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')8 G3 o! X% J3 x/ i8 X
! f: W. V- o8 b6 Q8 ?y_test_pred = classifier.predict(X_test)
# e4 b- o, v) x2 O5 n* e/ qutilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')
+ }$ Y, g8 Z- j8 ]! Y; O/ F- r1 ]7 L
# q' V. ]; l3 ^2 C. J- n+ u###############################################: m7 ]/ S! T9 C
# Evaluate classifier performance6 X5 a. y, f: K; k% i2 J8 H
0 s% E# C9 C, Y& V+ A
from sklearn.metrics import classification_report
+ ]9 e5 v- _ p& U: M2 p( j& p* `, k& f0 K8 j
target_names = ['Class-' + str(int(i)) for i in set(y)]6 W% |1 u2 g# s% i
print "\n" + "#"*30" g- t* c, A D9 T5 i! K2 h
print "\nClassifier performance on training dataset\n"
; Q4 |9 r5 W# O5 l( m3 cprint classification_report(y_train, classifier.predict(X_train), target_names=target_names)1 K: h5 k& y! P: c M* i. I+ n4 h
print "#"*30 + "\n"3 Y8 o" b- F2 m* ~! w) x- O
: b& G$ q. Q4 g9 t3 ? W4 D
print "#"*30
) I0 ~8 n4 x9 C5 P# J, u+ Q" Gprint "\nClassification report on test dataset\n"
& v! F. i7 A* _* _print classification_report(y_test, y_test_pred, target_names=target_names)
) [# E) i" [ k% ]; K z. g) lprint "#"*30 + "\n"
4 G& c' E' R' K$ K! S4 v2 l4 {) s8 V' S& R' E- A7 u$ w1 H1 }& t
|
|