- 金錢
- 46
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 556
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 46
- 威望
- 3183
- 主題
- 0
|
import numpy as np. a# ]' f% }1 l' S* {, Y7 a: h$ T
import matplotlib.pyplot as plt
5 C. j; {7 U9 v }3 j+ F* l: W8 j
3 y Z6 W5 I1 ?; r! U, G2 aimport utilities
6 s8 Q/ ^5 H4 A4 T; d3 q5 k+ [+ P. k9 D$ S; {7 M4 z) y
# Load input data
- ]- ^) L- U. [" p1 [input_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'; T% L, m0 g2 ~9 y( e9 H
X, y = utilities.load_data(input_file)
: T4 X7 h& Z5 g3 u s& T- x) P# S2 T% S+ A% {' W
###############################################' d& u$ G) z& [" ?( ^, {1 I
# Separate the data into classes based on 'y'! \, W2 s6 u- b6 v. @6 e# a/ d
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
, J O* J8 R& w1 K7 jclass_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
2 W5 |3 E8 K- h9 G4 h: r2 H1 q' T! r7 C6 G. h
# Plot the input data! P4 Q4 K. o8 Z- W0 E
plt.figure()
* h B) T) g/ e! Z( X! I2 j4 f) p- Cplt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
+ k4 I9 n1 y' L0 V# Hplt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')5 g: Q) q! L) X0 W/ G1 s
plt.title('Input data')
/ e6 ~- p2 k, e. ]: C9 X2 O7 K
7 O/ A$ [9 o" m/ y% r7 i# A###############################################! k L/ }: J2 n7 [5 r
# Train test split and SVM training& ^( x* L5 |/ H7 u+ z( Z
from sklearn import cross_validation
+ X+ S5 ~, T. L* `6 U6 Q* O$ Ufrom sklearn.svm import SVC# z: ]6 \" ^4 Z5 I, I( u
) ]1 ~; G' L6 l; }X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
. g& v# ^4 F% ?5 \' O0 b, Z, t
|; J- t. E. e6 _& i% j$ F' f#params = {'kernel': 'linear'}1 E; k: o: G" E4 i$ e& B i7 ^+ B
#params = {'kernel': 'poly', 'degree': 3}
; ^8 _; t t h1 ^) eparams = {'kernel': 'rbf'}0 T. t) X+ }1 @
classifier = SVC(**params)$ ^& l, ?' N2 \1 N, i
classifier.fit(X_train, y_train)$ b4 K) m$ R$ y0 \7 K) T# @
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')
" k4 H, k- R8 M/ J' e6 i
6 J( w' v% e5 E5 i- [/ wy_test_pred = classifier.predict(X_test)7 E3 f+ p, Y, N* a( s
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')
( b- {0 m A; L1 y; o
$ C5 z. f8 _, U###############################################
3 g, P" V- h8 s9 ]" N$ u. |# Evaluate classifier performance2 `1 y/ G# ? Z# x8 S; W. `3 c
3 b9 Q0 O7 D5 r1 [from sklearn.metrics import classification_report3 e* @( ^: O# B1 d5 s7 t
8 M% p% |$ o0 L) Y+ Starget_names = ['Class-' + str(int(i)) for i in set(y)]& }+ z4 `$ C4 [9 }0 b8 `2 t5 O
print "\n" + "#"*30" n! q8 N0 V0 t! [+ U
print "\nClassifier performance on training dataset\n"
& w+ G; c. G- _/ @& Bprint classification_report(y_train, classifier.predict(X_train), target_names=target_names)% J+ V l5 h! ~6 t7 y, b
print "#"*30 + "\n"
( x: L+ B \4 X+ X1 ` W2 J1 L
& k7 ^% h7 f V5 E/ t$ Rprint "#"*30
7 P5 A# R7 |- v/ a Yprint "\nClassification report on test dataset\n"8 z8 b5 L; B' ]( w" c
print classification_report(y_test, y_test_pred, target_names=target_names)# {* q) z9 J; E; X7 }* y2 a
print "#"*30 + "\n"
7 w4 Q0 z. K" e* Y
6 p+ v0 n$ ]9 d |
|