- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np* r2 C/ {8 V7 P( I, g
import matplotlib.pyplot as plt
1 k' s# U I# M- _& v
) d& D% s5 K1 R$ O" j6 cimport utilities " m% \/ i5 O% |0 u. b1 z, A
5 k+ K9 v, t! d: X4 b3 y# G, Q9 c' S
# Load input data5 d; n5 r5 g# a5 A
input_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt') {- h4 d$ O# j* c
X, y = utilities.load_data(input_file); D* `4 E c4 A. A- B4 r& w- X
! X( q" h5 o0 c* E1 O8 i
###############################################6 J- e7 H% X( M- `
# Separate the data into classes based on 'y'6 i3 D9 Y$ d. E- X
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])3 a! \5 @9 m3 F' c; d
class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
2 Q3 T0 b/ {2 S2 S2 I+ @; y G- c4 m* s) g6 R
# Plot the input data$ m1 d- S% e0 H. @/ e/ Z7 D
plt.figure()
) e9 |4 K: {0 F8 P) E1 f3 rplt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')+ V9 b6 Z+ j7 S6 ~
plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')
2 j8 M& B; \' C% iplt.title('Input data') i* \$ ]3 \$ X9 |" Q! {
) Y$ I% U; p3 l& n( \/ M! p###############################################0 O6 M: R2 c) I2 }
# Train test split and SVM training
6 L" `6 q# d7 p# L, Y# ^% l* ufrom sklearn import cross_validation
% \' t0 O' M( O! Bfrom sklearn.svm import SVC
: w" y1 ?+ Q: h+ B4 |
! \' b* l9 P$ b# B4 s$ wX_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
& t A4 H- d9 \8 W; }4 w, @/ a5 k# ]' x& O& k* E0 X2 ^
#params = {'kernel': 'linear'}0 Q/ f* a8 H* `6 N& t7 {
#params = {'kernel': 'poly', 'degree': 3}
2 q& f, P( ]+ e2 C+ \/ q. V3 zparams = {'kernel': 'rbf'}
/ X. L& P, O5 l' z& qclassifier = SVC(**params)
# c* l9 p w8 mclassifier.fit(X_train, y_train)* f/ M3 u5 y% N7 Z
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')
: k( |0 \, T* l1 h1 H4 Z' t! {1 V6 Y) \9 E0 P$ V
y_test_pred = classifier.predict(X_test)
" s" A* \1 S/ V1 P2 vutilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')$ o, |! y9 ?/ Y* |# z
+ }2 x6 p* u/ A0 c4 c; }, ?9 [, B8 Z3 D###############################################
6 _. m1 L k0 T# Evaluate classifier performance
5 K! t- J* f# A' i$ N0 w5 A/ K" x
; R' B, M7 r4 P6 S. n) x) Gfrom sklearn.metrics import classification_report
2 F$ ~8 { m) W3 J7 J& x0 ^) j, Q6 u3 C3 Q
target_names = ['Class-' + str(int(i)) for i in set(y)]
3 _+ I4 i0 W. Z8 L) ?print "\n" + "#"*30+ y, z2 q+ B* u8 y- U$ }
print "\nClassifier performance on training dataset\n"
2 z, W3 H1 Y, T5 e+ [print classification_report(y_train, classifier.predict(X_train), target_names=target_names)
; n W3 e: c9 ~; J" ]print "#"*30 + "\n"6 j- L0 j, V7 o: B5 G' y
b; b7 N6 b, ?& V- A* z0 R ?
print "#"*30, Z* J! }6 P: B. \7 s; M& F
print "\nClassification report on test dataset\n"+ |& o9 K* S' p# C
print classification_report(y_test, y_test_pred, target_names=target_names)0 E7 w/ n+ V) }# Y, g y
print "#"*30 + "\n"" L1 w( N0 P) a/ g
7 g. I5 ?6 l! D- @" ~3 R" O5 p
|
|