- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np
+ Y) H$ y* z/ y- g! Fimport matplotlib.pyplot as plt
" m" g/ O% R2 O3 x! p2 r* p
: V# h' s1 Y5 q& w* simport utilities 9 Z5 x' R. ~; K; u2 Y
# U/ k2 i& b0 x
# Load input data
: v, P( S& y% y2 y9 J, P! ]4 zinput_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
: }7 B9 h! H6 ?0 S4 v5 x+ wX, y = utilities.load_data(input_file)
! X9 z0 J3 X& M
/ y9 ] H H$ ~7 p8 f( q0 @###############################################
$ B; O( T, F1 r( i7 B# Separate the data into classes based on 'y'
3 t3 S1 E: f) ~8 w! j/ d$ T6 }- eclass_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
7 Y1 p; }! ?2 T/ V/ o& X9 Jclass_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
. ?* |% Y2 b4 ?: ~/ ~3 A# }$ C# V8 Z# d2 U7 r# F: A
# Plot the input data% P4 f) ~1 m% u$ s! E1 V4 V- L( ~& \( V# ?
plt.figure()* ^1 ?, Q" a" i& Z C
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
0 c7 F9 _+ Z) s" V" Y" z* E* hplt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')9 i! y5 X' q; a% V
plt.title('Input data')# V* ]" j; F7 b- C d7 u
& l' E0 m5 e. n% i Y###############################################
" `9 a/ I8 X* K/ ]# Train test split and SVM training9 i' q+ X5 o$ O) r8 c
from sklearn import cross_validation, q, n& n- A$ J* `% h# G; L! l1 P
from sklearn.svm import SVC
4 t$ d, }7 m* X7 Y' a z9 ?
) R* `* M. ~$ K- r3 l' q3 rX_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
6 Z5 Q( s8 y) F; ^( u
1 f8 s, R' {$ B c& j#params = {'kernel': 'linear'}; E' {% d" E- E9 r" v! v
#params = {'kernel': 'poly', 'degree': 3}5 V: d) ] }4 m$ p% r3 X
params = {'kernel': 'rbf'}" {& }# q) |1 Y0 c# l- ?# r% q+ m
classifier = SVC(**params)6 s, c- A, ~. s' j5 x a
classifier.fit(X_train, y_train)
* m; [2 p! B; e" x3 B/ z: Uutilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')
! Y. C. c; V! b4 q/ V x' ], E4 T' u
y_test_pred = classifier.predict(X_test)& L9 \& Z$ j% Z4 v M, x& _
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')
6 i$ B. r: W' ?
) ]& D) K* t7 ?; i' T% e9 g###############################################. Z; K1 p, _" V" d
# Evaluate classifier performance' F3 i0 p. f) g4 H) t& f+ z
7 c7 z0 ~7 }1 v$ N- A4 m
from sklearn.metrics import classification_report
+ M# R2 m2 n9 l1 ~3 r. Y; t
9 Y5 \, |- ~4 a6 k$ b" g utarget_names = ['Class-' + str(int(i)) for i in set(y)]4 J* X4 O$ `; R6 `" T1 u# r
print "\n" + "#"*300 K& P# j- w$ i6 v. c% F/ @. B
print "\nClassifier performance on training dataset\n"8 z: N; s3 F3 b" D! K7 e' y5 o
print classification_report(y_train, classifier.predict(X_train), target_names=target_names)
( x& k" O" z6 Pprint "#"*30 + "\n"
* r( q* F, W. h; z0 z! l
0 G B3 r' a0 }print "#"*30' ~& v5 v, t* b3 Q7 G# w
print "\nClassification report on test dataset\n"
" ^- Z& y$ f/ ~1 s, _# o& s) Vprint classification_report(y_test, y_test_pred, target_names=target_names), z/ u ]" q# G* J0 b& t/ V. z% u
print "#"*30 + "\n"" g$ ], l% t8 U. o/ F/ q) E
- ]0 G6 P0 d8 s+ F1 Q) X. d5 v2 e+ j2 Q6 n
|
|