- 金錢
- 46
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 556
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 46
- 威望
- 3183
- 主題
- 0
|
import numpy as np
, s4 P. J3 U I# m' ~3 S; Pimport matplotlib.pyplot as plt1 k# u% c- `7 \" z8 t3 [
3 L* j3 w8 P( L# l6 Kimport utilities ' O k6 |' U* H# E/ F2 O
1 C; B( x5 ]# d5 Q) u
# Load input data
7 P" |. Y9 l9 ?2 _input_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
7 y. R, C. H% ~% hX, y = utilities.load_data(input_file)
1 S: H. Z" O8 ?
- n0 `. |( W- t' c+ I0 d/ r###############################################
3 E2 t; s1 ~ {6 S# Separate the data into classes based on 'y'
* E) d2 Q4 J( c3 uclass_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
, X* ~3 |7 t F- Zclass_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
! y# E+ \, h$ l. i5 O# Q
" q- x2 p8 O( o. E C) J+ V4 ]# Plot the input data }" X/ @% `- }7 f* Q
plt.figure()
* Q0 _3 _* g& R$ |% o8 h2 T6 Fplt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')* s! I5 L, |/ y$ x! w
plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')# n' ~1 p8 N5 H; R6 U( B i
plt.title('Input data')
& h8 x# G. h9 L- K% P) ]4 j& j2 V6 X# x$ {
###############################################
5 ?: `: q1 r$ }7 O/ `* |5 R3 h" q# Train test split and SVM training
$ u, A$ g# w1 e7 }: |1 A+ Afrom sklearn import cross_validation
0 x1 }1 v$ d3 v0 \+ M8 x: O) C k& bfrom sklearn.svm import SVC
, M1 G+ y# K8 m! k1 \# M9 a9 R! a3 L) ^& N3 i
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
0 D( G2 D2 `% P. d! r% S# F
/ U) u u. X% l) p G% k: H" P#params = {'kernel': 'linear'}( I/ K \4 T' |. i: |7 @6 K
#params = {'kernel': 'poly', 'degree': 3}, L9 L4 t& k; ~5 w+ ~
params = {'kernel': 'rbf'}3 C7 Q) k" k5 n4 X
classifier = SVC(**params)
7 _6 p: l- H7 J* N. S3 a; |/ Rclassifier.fit(X_train, y_train)+ l' u4 P7 ~: q; V% O* u
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')( I- M. r _8 ?* P6 h# j. _
( `9 x ]' N# R8 Cy_test_pred = classifier.predict(X_test)
* M3 N- y- R7 ~7 dutilities.plot_classifier(classifier, X_test, y_test, 'Test dataset'): B- D* S. c' l! e8 h$ t. p
' ? F9 ^* {! V2 C l7 a4 |$ F- `###############################################
; ]( B V' H7 T+ U. q4 G9 q3 k2 X0 l9 u# Evaluate classifier performance
- Z/ t( b/ Z1 a/ u( x* G
. ^' w2 O0 u; O/ t% v( Ofrom sklearn.metrics import classification_report+ k w/ ^" J3 l- V* z. V
& z2 }1 Q7 m; V% Jtarget_names = ['Class-' + str(int(i)) for i in set(y)]
2 g) n: y4 I: @6 bprint "\n" + "#"*30
o& \7 H( n6 Xprint "\nClassifier performance on training dataset\n"0 m# c, Z$ W3 k0 v
print classification_report(y_train, classifier.predict(X_train), target_names=target_names)4 p' s8 i& W5 N8 v" g
print "#"*30 + "\n"
0 [% D R# b2 k# I" `) m- k0 \: s* p. J
print "#"*30
6 s2 {* d9 c# k, v( W7 w, Q1 pprint "\nClassification report on test dataset\n"- _1 n5 s& O. D+ n
print classification_report(y_test, y_test_pred, target_names=target_names)) F( H0 G& L" P3 Y3 Z& z
print "#"*30 + "\n"+ x/ f' H! r+ M6 S8 D5 {
$ J f: |! C9 f" B' ~* G' ~( p; J# ?
|
|