- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np4 R0 W% o) p# y; s f- A3 ]
import matplotlib.pyplot as plt1 D' x. M; M" r1 D
4 b3 I' H+ T" b/ qimport utilities
) G8 S! V( }! C7 b
F# y- D" Z1 [% @8 G/ b# Load input data
; |6 E+ j) Q* V6 Linput_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
3 ^6 d7 e0 z0 N; o) X# \; xX, y = utilities.load_data(input_file)
2 V. p1 t4 g' G5 A2 n) ?" k% n
+ z+ [$ K, ~3 v( u###############################################
3 H1 ~" I$ R+ G7 K, A7 o# Separate the data into classes based on 'y'2 r, F7 ?# R2 O1 M* E ^
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
2 ^+ C/ b# r# G& e* cclass_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
" O' O4 U4 t4 v) H6 B/ h, l( Y" j3 l, l6 j1 ?; {3 P* e
# Plot the input data
+ E6 U$ M0 ?+ dplt.figure()
. Q: Z% M% f+ }) \1 M5 @plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')" V' l" ?; ^# B2 X/ L
plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')
" |0 k- ^; |$ i8 M9 g% Eplt.title('Input data')
1 p# Q$ W: ~. V# f
: L$ E. w; p7 p###############################################. I4 Q; }1 F9 d9 U
# Train test split and SVM training
) |5 F4 y5 G" Vfrom sklearn import cross_validation
! h% {6 M, k- s( Jfrom sklearn.svm import SVC
5 `5 y3 U3 z0 D @! z8 I8 R2 {9 v: E% F* ?
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
# f' ~4 v7 a( C
: m. r: u: o% H2 h. J#params = {'kernel': 'linear'}
& I/ ^5 B9 X0 {$ e#params = {'kernel': 'poly', 'degree': 3}: D5 r9 k4 M- C. ^1 p
params = {'kernel': 'rbf'}
D- Y m* r0 ~. O0 V% _( o s( dclassifier = SVC(**params)
6 U" N9 w" z: w% e5 aclassifier.fit(X_train, y_train)
/ x [0 ]3 c$ Tutilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')
9 K0 D8 ~8 ], K2 k+ H' x0 V! s' l( ~% a& I
y_test_pred = classifier.predict(X_test)1 m$ T& a8 x" @8 ~! b8 B" Q
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')
/ q" H( a5 E" K4 q- s8 ?5 `7 t
2 ~. n* G+ ]7 o" T/ y###############################################
2 f% o& {0 a3 Y$ \" U# Evaluate classifier performance) z3 O: B! X. S& m
4 ]- U- d! q7 N1 ?0 F/ Kfrom sklearn.metrics import classification_report3 e8 J5 h" c/ G3 O. n. `" {
+ A' {$ T( D7 f2 e y
target_names = ['Class-' + str(int(i)) for i in set(y)]6 O# `1 j2 _( j# Q$ X* p
print "\n" + "#"*30
9 n4 z, b% r, c) v3 A7 n( I5 l# Aprint "\nClassifier performance on training dataset\n"
p% J: I4 o: p N# V# F! }& Gprint classification_report(y_train, classifier.predict(X_train), target_names=target_names)/ v' { d! g6 |% I
print "#"*30 + "\n"
9 z! V. \( N/ S7 A% {- y, R* m0 G' k% \) q" k4 @' R2 C
print "#"*30
$ M) |4 D+ ?( m' u5 vprint "\nClassification report on test dataset\n"5 T! W7 D; r6 c/ i
print classification_report(y_test, y_test_pred, target_names=target_names); H3 [- P2 G1 B# g: Q
print "#"*30 + "\n"
0 K( l1 H6 x" `2 D6 E" ^6 g4 N$ M2 o$ L& Z, L( J- H- A
|
|