- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np
& e5 `! R$ r/ Z4 I7 X" v9 R' @import matplotlib.pyplot as plt
6 ?7 ^! T- G; u7 h E. P3 m! ^3 {# a' p/ M7 W$ {
import utilities 9 F- n; r) y# |: n' s
: a. G& d* |6 s6 T& o, N3 e D# Load input data
3 w6 S8 f6 M. L* D w( |input_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
; c) r# x( _9 l) [- X- G4 fX, y = utilities.load_data(input_file)
# p* x5 M" v3 G8 {+ b; \/ P: [( _2 X- E( t& i3 `
###############################################
8 }% r7 ]5 D1 g6 i) y) w& d. F* y4 p# Separate the data into classes based on 'y'
8 w2 Y7 f, Y' i- S+ D* q( Nclass_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
0 m# [* C% [, [0 \% B6 |class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])* N3 E$ t8 P( d
' C- s' `# S6 u, P# Plot the input data
, b2 F% Y/ i# h* M6 vplt.figure()
1 i8 x5 V* g% ~3 K( Oplt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
) @2 R& L: ^5 e m) K& pplt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')6 _! s3 v$ \% v# n
plt.title('Input data')( [5 M7 n% V' m% C# W# D
, K/ i& R# T2 H% n0 E###############################################( Q! t; s$ ^! b _0 K! u# M9 B
# Train test split and SVM training
0 d2 W/ M/ d- q r# M+ ?0 Nfrom sklearn import cross_validation
! x$ I# O) e% d5 Lfrom sklearn.svm import SVC
* ~: R; n- `4 d% G" U2 ~) R' \, h6 P, v9 v$ S& [. K- Y! t+ Z' B
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
4 H6 A, }4 _1 y- ^1 x0 r4 I* G
. ~# d$ @0 Z% l: |; a* R/ F#params = {'kernel': 'linear'}0 v% h: z: s7 l; w; i9 d/ U9 F V
#params = {'kernel': 'poly', 'degree': 3}# T1 T; d; |& ]. l0 z! w; X
params = {'kernel': 'rbf'}2 l5 B1 e3 h4 L! E2 |( m% Q
classifier = SVC(**params)
3 S6 E% {: O/ C5 Bclassifier.fit(X_train, y_train)
! A, {$ J" U/ d3 s5 X1 ?utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')$ R3 C# }/ O r1 T' v1 y
) N, v N5 b/ K& f' N3 g
y_test_pred = classifier.predict(X_test)
7 }7 O: `1 c* k* Dutilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')
* U% q$ {; b5 v) |5 Z! A+ l
. z9 s1 N2 }& l3 {; s" N###############################################" r$ S9 y9 f! s& C' M$ F' |
# Evaluate classifier performance9 h$ I& k3 X7 k! Y
1 h6 ~0 L; D% W' R3 F& k6 k
from sklearn.metrics import classification_report A2 j7 x& @$ e
) w1 y2 Z0 q% i* N+ U# B
target_names = ['Class-' + str(int(i)) for i in set(y)]
5 [) g! ?$ k, ?; o5 S. fprint "\n" + "#"*30
, d& q3 r9 R5 Bprint "\nClassifier performance on training dataset\n"
, f' Q& s9 m. y7 l8 ?2 j: Dprint classification_report(y_train, classifier.predict(X_train), target_names=target_names)2 p" m7 s' e: P5 T
print "#"*30 + "\n"' h; z* W+ Z/ C# N) h& Y. k
# B3 [/ R Z0 b0 a6 k, q: n- o% kprint "#"*30
$ Y( P1 r1 v$ S6 k9 H% b3 wprint "\nClassification report on test dataset\n"
3 J9 a+ F2 }) zprint classification_report(y_test, y_test_pred, target_names=target_names)
# v' H' O1 x0 l& _print "#"*30 + "\n"+ K, c, @' v% o: a# b
8 Q `. ?' `# Y( j) [! E |
|