- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np k8 W2 M: {4 z0 [
import matplotlib.pyplot as plt$ T7 u3 L$ P2 {: r U6 S$ u& |
! m9 j' C& Z5 \% o9 \3 Limport utilities
8 i& v) F$ a, h) ^) @7 h- {$ G6 @8 \: P$ O) b8 [
# Load input data
- U P5 M- T3 Q* Kinput_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'3 r9 c& K0 Y5 B+ W- `& F- B5 z6 Z, B
X, y = utilities.load_data(input_file)3 h, Z6 E" E" T2 A6 `- |
% U; p6 N5 Z! H& ]$ I" P }- A###############################################
+ e2 e5 }) e e' H' @# Separate the data into classes based on 'y'; A" I& F+ O9 y" D% v) f& x/ ~
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
1 |: k' M9 ^2 Y G1 u) Dclass_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
( v7 r2 Z8 K7 v; X6 t6 r
+ K: L2 \$ ^8 x; m2 F0 D# Plot the input data
]! w& H O2 Dplt.figure(); }- q' ]* M/ n/ N# |3 H8 r& c5 ^
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
) V0 l; D7 y, K+ X2 S; X% P4 Tplt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')
; x& V$ r/ t0 X/ Y+ Wplt.title('Input data')
/ D: u$ D& L/ d c& y" g; {4 j' l: X& n s7 u" e$ g+ W: t
###############################################3 k% X3 I5 J! X. i
# Train test split and SVM training
5 V" P' T1 r% Vfrom sklearn import cross_validation
3 A1 y( K& L1 G: hfrom sklearn.svm import SVC2 C1 \6 M! b; V' ~" l
8 D3 Q. M4 i$ c% A# J* ]5 D: |
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)6 c0 U0 `5 R6 f" t
. h7 B6 E# H) a, K" m; \#params = {'kernel': 'linear'}
* ^& N5 u2 U" m#params = {'kernel': 'poly', 'degree': 3}
. V6 c& W: |3 X, v' [params = {'kernel': 'rbf'}
6 }. G% N$ m( \9 u* g1 c$ R# B- gclassifier = SVC(**params)3 X4 s: [5 F% o( F2 d
classifier.fit(X_train, y_train)
}6 {% Y2 b) |5 Mutilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')
6 w& @5 o9 }" F4 _' n7 A8 ^0 R4 F3 D. e+ A7 c1 s8 Y: ^1 r
y_test_pred = classifier.predict(X_test)! `. b U4 x, o9 C2 I
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')
9 Z2 P, t8 l9 i- g
$ g$ H5 ]' l6 S5 W. s###############################################
, k- e/ c- M5 p* x# Evaluate classifier performance
. P0 l& X( W: A1 ^( ~4 T6 j
. Z4 x6 L! X+ q$ Z6 K% jfrom sklearn.metrics import classification_report
& @) s/ ^( r+ ]! I7 S$ }3 |
& p9 R" x( W- N- c7 Xtarget_names = ['Class-' + str(int(i)) for i in set(y)]
* H4 f: E3 j! s$ x+ x% B7 Tprint "\n" + "#"*30
* F' i6 P. \% p' mprint "\nClassifier performance on training dataset\n"0 Y& B W& x" y
print classification_report(y_train, classifier.predict(X_train), target_names=target_names)9 m) n6 S8 x+ u0 w2 ~ }- E& E
print "#"*30 + "\n"$ P Q: u( s) @! G I
, x' y1 }; ~3 Y% v, }+ x
print "#"*30* x" U/ `( h3 I8 n n
print "\nClassification report on test dataset\n"! E& }" V+ [5 F8 u9 R; ?
print classification_report(y_test, y_test_pred, target_names=target_names)& u1 j( @! r) {( v$ z, d
print "#"*30 + "\n"
2 r' S( R/ A( v; S0 i" A! f+ t. W# S3 v5 o3 m. e
|
|