- 金錢
- 46
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 556
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 46
- 威望
- 3183
- 主題
- 0
|
import numpy as np
( g& {* C0 ?) |$ T" A' s; j6 Aimport matplotlib.pyplot as plt
; @% [; m, X3 c! j* p/ F' d* `5 v3 X: s: k; f% _$ G3 m, j
import utilities : L0 _' Z" e( h- l L. C: m
) E q" S* i" p/ e- p. Z8 J2 G8 s$ o# Load input data
" e7 x1 p% u7 h5 Y ~; ^input_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
: O; Z. Y) i' \X, y = utilities.load_data(input_file)0 `8 j, M0 L4 o6 L
1 O- Q0 T3 M% ~( r7 ^- n###############################################
: X% G( f$ }+ C+ z) p0 w$ t4 i# Separate the data into classes based on 'y'" D" R1 U% U E1 {( G
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])- y+ m: t& E1 R4 I T3 B+ C
class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
8 n0 V' y# \: O$ P" k0 o4 t: F1 S. p; H
# Plot the input data
% Z4 i: e" u$ O/ E/ P# splt.figure()
3 N& L3 K; W& E4 z1 V- s. hplt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
4 d; C# p, k8 i) T5 c* g dplt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')
8 }; Q F7 c z* S A2 P. Pplt.title('Input data')
G- t* G8 w& ?) W6 p7 \+ [& z( n. x. q4 a. C5 }! Z: ?6 K
###############################################5 k9 W2 u( W+ n3 o! \
# Train test split and SVM training
- i4 Y4 U; C! W! L4 ~' T- b, |5 u- afrom sklearn import cross_validation
- Y/ O$ T4 V% D1 Yfrom sklearn.svm import SVC
5 j1 i% Q3 S! Z! v+ h Z. j
" ^5 @, S& ~- AX_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
7 y4 C7 Q/ g: d% y7 o$ w7 c- X) I' w' G% b% v$ j. u
#params = {'kernel': 'linear'}
& r, O; X4 y, ^1 m#params = {'kernel': 'poly', 'degree': 3}
* l j' t o3 T9 b3 R' m5 G7 Iparams = {'kernel': 'rbf'}6 k1 i* b1 G( _
classifier = SVC(**params)
; D k _ W) Z# r9 ~. ]classifier.fit(X_train, y_train)
" n- h$ Z$ P* l, L8 }. o( i8 Cutilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')
; o o* P% Y4 v$ b+ n
8 S0 z! r( Y) ]1 uy_test_pred = classifier.predict(X_test)9 m* B; l9 [4 ]! k8 Y' @; X6 e; A
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')
5 R. E, G- h( L
3 L6 U" ~5 A$ f6 n###############################################, R' C$ K) D( G3 Y
# Evaluate classifier performance1 z/ G3 z7 _: c x9 t/ @/ ^1 l
8 ^7 b" ^! k4 Wfrom sklearn.metrics import classification_report
' |) w; ], b+ n2 D3 \# @1 Z6 H* m" d# J3 Z* b w R
target_names = ['Class-' + str(int(i)) for i in set(y)]
7 s# w. O G7 \5 x4 M: B2 ~: x7 N* eprint "\n" + "#"*30
- ^. b) [" k9 G( I2 `; O3 pprint "\nClassifier performance on training dataset\n"0 z/ X" C, a# k( j
print classification_report(y_train, classifier.predict(X_train), target_names=target_names)
% n" p& p9 f* v W3 H: ~print "#"*30 + "\n"/ x1 `0 b. e( t
0 c% }9 a5 Y( }0 {8 t) U* z1 {* X
print "#"*30
: m1 r- [6 t- k( h; _0 gprint "\nClassification report on test dataset\n"
5 j5 W1 Z- c T0 k7 k. `' Z7 `9 Nprint classification_report(y_test, y_test_pred, target_names=target_names)3 o2 s! d8 Q: B% K
print "#"*30 + "\n": [+ M. ?6 x# G7 ]( \5 U0 M+ ?
5 d ]# C/ R- z+ _1 {
|
|