- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np
% ?6 P! I/ F9 F) y. pimport matplotlib.pyplot as plt& n" H5 N; [# N
0 m; [, v( r; W0 k
import utilities
. H: ?+ x7 ?7 Z2 m& t" I3 A
2 P( Q# f2 t! D* ~6 o; B/ ?! g$ r$ }5 l# Load input data' e, H/ U' v6 j( p0 g W! {9 r6 Y
input_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'. R. @3 R4 ^& k
X, y = utilities.load_data(input_file)7 }1 i% P( V/ L) o% B
; S& P1 P" x% j! q9 D+ ?- {4 R* `###############################################
& _ ~0 s) X5 d$ l/ G: m: ^! ~# ?( [# Separate the data into classes based on 'y'% U1 A: R6 B- A7 E
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
* F/ ?$ X2 w) t4 o& z% i; ^class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
2 r+ O: |# i' y& o& i9 x! m( n4 |) Q7 O6 X% Y; @& x
# Plot the input data
1 z- B, g( s/ j9 ^9 uplt.figure()! x4 x- k5 {! p' E
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
! M6 }/ n/ t. H6 Z' dplt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')
4 t/ V4 {/ {( f2 g9 g8 j. M7 eplt.title('Input data')9 [. K7 j& @9 |0 a4 P' z2 \
6 ?+ u& \' o4 _% l! l###############################################
$ V% v+ C7 z4 }- m# Train test split and SVM training' ^0 d3 F& x0 |# S* i# f/ y
from sklearn import cross_validation8 |3 F- s& W+ L# Q0 q! D: G
from sklearn.svm import SVC
5 `/ f6 `, x# ^" V: P6 v8 d& s' ^/ t$ X' {" b
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
. Y! |5 ?3 M, s0 s; ]! |5 m2 O6 S" N* Y+ }% E9 s0 V
#params = {'kernel': 'linear'}
j; b$ o" a, G& r# T#params = {'kernel': 'poly', 'degree': 3}
8 a5 {) h3 d# s' r- jparams = {'kernel': 'rbf'}
^. y2 S0 [9 ~classifier = SVC(**params)( O. R/ y" f$ U. w, D+ E" F4 U( c4 o
classifier.fit(X_train, y_train)
4 T [ o0 e" M9 s7 l- l4 vutilities.plot_classifier(classifier, X_train, y_train, 'Training dataset'). X! w: L8 \& Q. l
1 S$ M1 Y9 w, f- o! Dy_test_pred = classifier.predict(X_test)! ], `- `: M) h6 ^& `
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')( c$ u% K: v# }# f/ f+ w
7 [* V4 l0 D7 a' l5 b) U
###############################################' P3 E- j' V. v" \
# Evaluate classifier performance
5 W; w6 Q X ~5 d" q8 b
4 s5 P8 ?$ d; l- m8 V2 b' K! ]/ ~from sklearn.metrics import classification_report
+ k) C% b! p+ I& |2 Z4 S. Q3 d; m, s; p$ Y% j2 F$ B$ N$ |
target_names = ['Class-' + str(int(i)) for i in set(y)]
6 c4 c; I, h' _* Qprint "\n" + "#"*30 W( m1 m$ P. w& J! d) n
print "\nClassifier performance on training dataset\n"
/ `' V H4 C, eprint classification_report(y_train, classifier.predict(X_train), target_names=target_names)
; [/ t$ V8 j1 _7 O! Lprint "#"*30 + "\n"6 Y1 J2 o7 p: u9 U* k% q8 i' X
( W2 h5 `$ B5 eprint "#"*30
$ d( |. K% O% @9 J2 F) Cprint "\nClassification report on test dataset\n", l" q, N# ?3 j8 v) w/ F" U
print classification_report(y_test, y_test_pred, target_names=target_names)
/ O$ O4 ~3 n4 i F: M k0 Aprint "#"*30 + "\n"7 a/ J+ M. `4 L" H
" K$ B/ c/ L: ^' b
|
|