- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np* [" Z, D* v- Y4 J
import matplotlib.pyplot as plt
7 r- N n9 D& C# r, m3 t0 s* ~* e( U$ t% Q( a$ N" K
import utilities
* ?4 k1 ^$ p$ C! l# x& ~3 o7 r6 L7 N% u9 v+ _
# Load input data- f' m% q8 k: y( g+ h
input_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
4 ?& s- s" H4 j+ N$ a2 B7 rX, y = utilities.load_data(input_file)# [7 I# {# f( e- F8 e
8 `6 O" }. c7 x6 L9 T3 }
###############################################) ^) A+ F" R: N! z/ a/ u% _
# Separate the data into classes based on 'y'! H0 P) U, d+ ]5 Z( R" S1 a& n& Y" q
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
! N+ i2 P+ `5 ]3 B) b9 Qclass_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])3 w# S" S. o5 _2 A( X- a
0 l9 C9 h+ }: E% f4 P
# Plot the input data
z1 @& U: G e5 z$ Q m/ Jplt.figure()
+ r& I* e5 C( M* `. i1 ^plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')9 {7 w8 k* F+ K+ ]; F: b
plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')0 B7 O2 U8 B2 T) ~9 x1 a0 h& f# o- t
plt.title('Input data')" B; ]; t# c( K9 o$ D
4 `# p! p9 W3 t; m7 T###############################################
/ k& ~" j& u/ U$ S( ?; F: e9 j9 X2 k# Train test split and SVM training3 w& z: Z4 m5 i! J
from sklearn import cross_validation1 r# Q& z7 c( A
from sklearn.svm import SVC
7 B7 N. ^% t/ \+ o4 k% V$ C! I$ k
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
2 L( W7 Z8 ^# C/ W2 J% [" ^" h; _3 S, p; M b: d6 u* U* W- G4 L
#params = {'kernel': 'linear'}
; C9 L5 Q J1 K. [3 _6 w#params = {'kernel': 'poly', 'degree': 3}: h$ k1 z4 A% I1 M/ [+ H6 R
params = {'kernel': 'rbf'}
/ ]% i. Z& G d: l5 B) `5 r8 d! lclassifier = SVC(**params)! I$ C6 G; e- V
classifier.fit(X_train, y_train)
5 [8 f! |. R- i. autilities.plot_classifier(classifier, X_train, y_train, 'Training dataset') k0 _# g# v8 W8 ]' n) N
6 b/ Z& {# D1 n& D2 \0 ^# hy_test_pred = classifier.predict(X_test)0 L g' A" I+ z& ~' d4 @4 A& o
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')0 w) F% ?2 k5 c% D) o
6 K+ Z4 l! ^. C4 h###############################################/ J& Z; a# P" j" l2 ^4 H
# Evaluate classifier performance
) P- o/ d. c* m2 M8 x3 Y/ h y1 o
from sklearn.metrics import classification_report
( h/ K+ W/ k" U' h, w5 Z2 z2 a; k% O: A. T5 {: h
target_names = ['Class-' + str(int(i)) for i in set(y)]
( |+ @/ o- J5 m: r. } zprint "\n" + "#"*30' L) I, }# x6 @! Q, F
print "\nClassifier performance on training dataset\n"
. f8 T6 ?8 R- A A: bprint classification_report(y_train, classifier.predict(X_train), target_names=target_names)
6 b, h9 Q# [' [/ u+ x1 Cprint "#"*30 + "\n"
' y4 g- A+ `* [( n
3 M% j7 g6 Y) s5 bprint "#"*309 Z9 @, Q$ G5 H$ G8 a+ F, |2 K, k
print "\nClassification report on test dataset\n"9 R' n- Q5 G$ c3 h& I
print classification_report(y_test, y_test_pred, target_names=target_names)( b C% O0 \* Q/ Y' j! z' n
print "#"*30 + "\n"
# X- o" {; ?! K9 \/ {9 B9 ^1 v( w( \7 ]( `! i
|
|