- 金錢
- 46
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 556
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 46
- 威望
- 3183
- 主題
- 0
|
import numpy as np
* s" a% k8 I9 Z. e5 [5 o/ gimport matplotlib.pyplot as plt1 v4 j4 i0 Z! c" x5 K
/ }+ t! K& D- u" Z% b& T: @
import utilities
6 `+ {4 t( Z1 x; E& N# k( O" ^5 M, n
# Load input data
. M+ C9 S* w$ X- C9 uinput_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'" u+ e1 n4 \' v9 ~/ S4 I
X, y = utilities.load_data(input_file)
% c3 Y% I4 Y, U B* @" Z8 G D# M& c4 o8 @& i V; M
###############################################
/ g, B) {; Q0 Y: P5 y, P" @# Separate the data into classes based on 'y'+ W. i0 C5 [7 j r, J
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
5 N, G, V* B4 Q5 P. ^5 U4 Yclass_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])/ S4 f P! v. K8 Q2 v
$ e; b9 Q2 t6 p9 a, Q* d# Plot the input data7 z, u, f* t: d
plt.figure()' n5 Q/ Q& T9 [5 Y2 g* ]6 h2 r
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s'). j( v# k! M* p% `
plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')2 ~0 i0 ?+ R) O1 S5 M' K/ t7 Z6 v
plt.title('Input data')7 _4 v W. P' A) i A5 \
8 S0 v# f' d& A8 T/ d- _
###############################################
, K. r1 G" Q* g* T' t" j, _# Train test split and SVM training$ h& y2 r3 [7 P4 U) t, q$ M/ W l
from sklearn import cross_validation
5 _8 f6 I% d, d# ?from sklearn.svm import SVC0 t' y2 \. T2 q! K2 s
8 U* r0 G j5 r2 {
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)- Y1 e# w0 ~1 P7 k6 J# k& A& b7 ?
6 Y$ f+ x- e! J* s
#params = {'kernel': 'linear'}6 b, l$ W |) T# h$ s
#params = {'kernel': 'poly', 'degree': 3}1 y0 g1 y/ V4 j: R3 I
params = {'kernel': 'rbf'}
7 N5 ]4 ]/ j7 `* @% S" e2 I8 Wclassifier = SVC(**params)
; j* X9 e' [: |9 F& l! Sclassifier.fit(X_train, y_train)7 J5 y& s$ `# A( o7 R
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')3 a1 _* b/ ~! |' b- O, b6 P7 ]
. p2 L5 i a2 l: U+ {1 o4 T: h4 I' @3 ey_test_pred = classifier.predict(X_test)) w( T& A' @- M# n/ @4 _% a
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')
' X& P8 U7 O' O7 s4 A7 r! }; t, z" I: h4 h2 L% G6 e
###############################################
9 U* Z: c' D6 i/ m( ?" T4 _# Evaluate classifier performance
( c2 }* w5 s* r* ]! J) \; A4 b6 n n- h( \' u1 G, d
from sklearn.metrics import classification_report5 M% w' v" F3 B
; @7 f9 L4 A" [/ b. j% {
target_names = ['Class-' + str(int(i)) for i in set(y)]
3 Q! C2 S+ R' xprint "\n" + "#"*30
7 p- M# Z" ~0 G; U: K9 Pprint "\nClassifier performance on training dataset\n"
- m7 S1 ?7 k! T( q2 c1 Z2 |( g& tprint classification_report(y_train, classifier.predict(X_train), target_names=target_names), w- K+ }' g; }( @
print "#"*30 + "\n"
" M8 \% D4 `; ^/ w& L2 {* \6 g: V+ O$ W" a) }% B0 X
print "#"*30
9 Z! U4 C! R3 G) P5 h/ y& Sprint "\nClassification report on test dataset\n"# N. R3 J5 v+ y5 E/ J& ]9 f
print classification_report(y_test, y_test_pred, target_names=target_names)
1 y; i& d; F5 O% mprint "#"*30 + "\n"' e- s8 d: i- \9 s( E. V
7 C M- ~& R! L& W3 W" y
|
|