- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np, b; C- `* S: d2 s- U# w" t
import matplotlib.pyplot as plt
6 X6 v+ F" p2 }; J$ l' T) E* ~8 i' f& z5 k. x3 D8 _3 t4 S
import utilities , Z" f6 B: j# X- d& T K8 p: l
% n! Y0 g6 e' m0 a- J3 {
# Load input data
/ X/ y- Q/ j$ X9 O" f! a$ l. d) iinput_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'4 O* ?( S/ e7 y4 V5 N
X, y = utilities.load_data(input_file)
4 R/ p. ?! d2 Y* j2 G# f1 t4 g9 l: [
###############################################
7 u6 C; p. P6 l. {( k# Separate the data into classes based on 'y'
3 q: F4 Q3 `3 f. r% wclass_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
. o! a/ Y8 T; T1 U5 T- E) uclass_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
% G8 b- h O) g$ C4 j
" O& Y |$ `% @4 U; \# Plot the input data
3 U- ~1 Q V+ n* _0 P, t! o L6 Jplt.figure()
& N& {( E+ H, z( zplt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
( n% w+ X A# X; f2 mplt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s'); C( \+ _+ y+ K. x4 Q, e9 h3 q
plt.title('Input data')
- p1 ?8 G* j( `4 n$ o1 N; p, p
- U, j3 G4 ?7 ^2 g6 g###############################################1 |8 @ ~6 U2 v; P U$ F0 t
# Train test split and SVM training) Y: w- P$ a1 \8 M7 k
from sklearn import cross_validation
0 u' B( P) L' E" M# t$ lfrom sklearn.svm import SVC& _& U5 A' s0 r; z# H
; c& Y8 p3 X3 f7 `! x* h9 DX_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
9 H6 A: R1 M$ V$ k' B2 f. h$ U" q# u9 U( T* u' o' l- n0 J
#params = {'kernel': 'linear'}3 t3 N" P. ]+ C# p6 {
#params = {'kernel': 'poly', 'degree': 3}1 M5 j6 @& N0 H. s# g: K9 o. X
params = {'kernel': 'rbf'}' Y# \% A* |- R; k
classifier = SVC(**params)/ e4 H$ V: i9 G
classifier.fit(X_train, y_train)
: W+ q+ h9 N6 [* @ m9 Tutilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')) U R+ {1 ?, ]) q
; o( x, j! p# cy_test_pred = classifier.predict(X_test)% d m% G0 n. A5 J
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')5 h, ?9 O R) H: G K% Q* N
6 U4 l2 n* [ T# R& y8 l y/ n3 v###############################################
! w4 s/ N) c$ R8 w# F D# Evaluate classifier performance' q* h( W6 ^/ G1 ?/ D9 a; ]0 E
# @% f. J/ o! kfrom sklearn.metrics import classification_report! q" N- f- ~8 M9 }2 n
! Y5 R* u3 x# O$ p6 c& s5 t
target_names = ['Class-' + str(int(i)) for i in set(y)]/ Q; M- x7 |2 K: g; Q) Y* m+ T3 g" p
print "\n" + "#"*30
0 v |/ C6 X6 G" ?/ `- P7 Gprint "\nClassifier performance on training dataset\n"
- d+ [. v1 P$ B1 N. j+ }! |print classification_report(y_train, classifier.predict(X_train), target_names=target_names)- ?7 E5 r( `3 o& W
print "#"*30 + "\n"
: F; L, h; @! I0 A
( ~! B% c* k6 w& m7 S: W6 F! u* p, Hprint "#"*30 Z* @2 ]# J r7 S, l8 u0 I5 J' z! e
print "\nClassification report on test dataset\n"3 _$ U9 a& B$ j
print classification_report(y_test, y_test_pred, target_names=target_names)
* I( S6 |' R8 ~+ |print "#"*30 + "\n"
/ Z3 j4 ?3 e0 G" ^* d0 x5 T
+ d5 [- R; C: D: O! I |
|