- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np
9 S9 O( ]% }; ximport matplotlib.pyplot as plt
, K) g% _+ S4 [! e
& k+ B, f3 U s& P. aimport utilities ! O. F F: k. @$ y! y1 |
: Y) b, N; N# Q: O
# Load input data
. |8 W# A2 u. iinput_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
8 Q3 s, [6 a& k4 b; U! A# H5 w8 qX, y = utilities.load_data(input_file)& P: j& w. t5 A( \/ V6 w
( J6 d( I$ M* F6 y6 i###############################################4 R) F9 a! J5 y4 X& z
# Separate the data into classes based on 'y'/ i0 a8 g: a8 B; t
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])* k% p0 O, ?9 z3 C0 L
class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])6 V+ f7 R% y7 b& a f
9 S7 [ T- i/ s) N2 i# o: F
# Plot the input data
$ E# Q, s6 ?3 ~* y1 Zplt.figure()
3 F# ]( B/ i7 _- Splt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')" f$ z P" a4 n
plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')4 L0 S r. Q- v1 b9 O
plt.title('Input data'), `4 R. ~8 |+ Q: I/ x* I
; c: R6 S( c- Y! ^( Q# l! o( U5 H###############################################
+ D+ D: p, Q& K9 d' x# Train test split and SVM training0 e" E: @4 [5 s6 P' C4 ^& S
from sklearn import cross_validation
7 i1 ], X9 F& P1 w% ifrom sklearn.svm import SVC& X+ k, E$ N# h, H
; C- @( n( w2 n6 d+ h$ q" lX_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)8 r* v) L: ?( M; ~
* k/ V( O' c; e1 |! [( z$ t
#params = {'kernel': 'linear'}
5 x- h. A' K' n#params = {'kernel': 'poly', 'degree': 3}
1 |0 y3 V$ ~, f- ?* A2 |params = {'kernel': 'rbf'}+ H9 Y0 x7 G8 T1 I8 J: y2 T7 @$ o
classifier = SVC(**params)* y: z, i. ?5 n" a; o
classifier.fit(X_train, y_train)/ d l8 j" _/ ?7 ~1 b
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')
4 N$ ?* K8 I3 \5 |' g: T
3 J5 W4 J' \+ gy_test_pred = classifier.predict(X_test)
6 S U% h: k$ Y+ J5 cutilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')
8 P- y0 E$ y. O
: h1 u6 K G6 `1 T, V###############################################
2 t% \; V1 W5 L- J7 i9 [; k3 z# Evaluate classifier performance
9 h y0 ~& w# ]7 B' d& c
% d1 j( a) ?$ o, m1 ]8 B& Y9 y1 ffrom sklearn.metrics import classification_report
2 x, V8 N! K4 ~
# P+ a# M) x: ~% J L7 H% l( ktarget_names = ['Class-' + str(int(i)) for i in set(y)]
M/ e$ l) w. ~9 \# A7 ?8 Wprint "\n" + "#"*30
2 p. s# |- a# Dprint "\nClassifier performance on training dataset\n") S8 m/ z9 H4 ]1 x* U& x
print classification_report(y_train, classifier.predict(X_train), target_names=target_names)" Y5 _1 k( c: I* _ d$ X' q( ^
print "#"*30 + "\n"
: x4 l R I4 i4 z& U0 d5 k+ n6 h" S3 {* i1 F" a5 A) R
print "#"*30( s. o; N+ @, d3 j! U
print "\nClassification report on test dataset\n"
. H$ R' h# V9 r0 V; ]6 ~6 jprint classification_report(y_test, y_test_pred, target_names=target_names)
7 m" P# J' ~9 z; V6 dprint "#"*30 + "\n"' _ l' D5 m$ ~. Q
& |( K# Z9 T. K R/ L6 c |
|