- 金錢
- 46
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 556
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 46
- 威望
- 3183
- 主題
- 0
|
import numpy as np
+ D0 ^! e2 H, |3 V$ gimport matplotlib.pyplot as plt b( l3 G6 g* v0 Q# z
. G) _" B# v$ Z8 B$ ^import utilities
0 P: {8 Z( M' W, I. ]; ~1 x
/ I! l4 n# k6 c! b0 F4 a# Load input data
/ Y" @: r0 ?+ g/ rinput_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
% k) F* I8 M2 Y8 W' f5 jX, y = utilities.load_data(input_file)
7 T6 o1 r- m& Z0 V8 X! R- A! `
# w7 ]! {% [# l$ D. i& s. o###############################################
9 a9 h# n% Y5 Y. r( G9 W3 H# Separate the data into classes based on 'y'7 u' }, |9 ]) n4 c) M" j
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])* ^- a1 Q) b' ?
class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])8 O* H0 ]0 F5 `* l$ \- C% O! ?
7 J+ a6 [ K/ W* f/ K- N; N
# Plot the input data" y" @% }" N4 X" `7 k. E+ d, h
plt.figure()
; T7 ~- V7 ~, t2 ]& _3 _plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')3 k; _* w5 r/ P. B7 U
plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')6 i; U' ^: O8 t/ K9 I2 z
plt.title('Input data')
" o e7 ]: }8 W3 h) O, ]: m
! r! ~7 p; I6 y! W, B###############################################, H+ @2 k+ |8 s- e3 l- |: ~8 p
# Train test split and SVM training9 ^ L! s8 \" l: ?4 x
from sklearn import cross_validation
' m* Q- O5 O, z! \6 [8 zfrom sklearn.svm import SVC
; f: N/ J- o* H1 x! Q# h* I L* s4 b, h& I% z- u" A. t! ?
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
" a" z6 W: X) u6 I) m
* b! F: g- D; ^#params = {'kernel': 'linear'}
% @" f P- \% \" w#params = {'kernel': 'poly', 'degree': 3}
. q) M s4 h b% p/ z/ cparams = {'kernel': 'rbf'}
o6 t- K, |2 z( s m2 _8 gclassifier = SVC(**params)8 \$ b0 L3 A0 d* B) Y# ` P
classifier.fit(X_train, y_train)
* ^/ G' l5 M$ R) f8 P9 Butilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')6 ~7 R7 F) E e# p
1 M1 y9 ^ [/ J1 j" Z7 g# z2 I
y_test_pred = classifier.predict(X_test)
/ [4 f; N* T& m8 L8 R# qutilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')
" u. x& V) Q3 x) }$ f, p* n% r( l+ o4 p) J! Q( T
###############################################
: p( o" C# c7 l7 D7 B# Evaluate classifier performance
8 r+ R# o6 |, a8 U# E
N' H2 m8 m' e7 o: x8 y! f8 D, i, hfrom sklearn.metrics import classification_report
0 h1 o4 t/ D; d' L0 }: }
t# _9 y3 t/ M" c5 _3 Ttarget_names = ['Class-' + str(int(i)) for i in set(y)]
8 O* ~; v$ E: d# m4 oprint "\n" + "#"*30
& o. q) u6 z5 k0 g) u4 z+ M! Sprint "\nClassifier performance on training dataset\n"
1 t( b+ w- m4 Nprint classification_report(y_train, classifier.predict(X_train), target_names=target_names)
2 ~* V% l& I ~: i1 q+ o! W% U- dprint "#"*30 + "\n"
3 l3 V* M$ |5 F1 x" E! O' |( a+ s. i. d, Z
print "#"*30$ g# l5 C+ F& e* z& M. N' ]
print "\nClassification report on test dataset\n"& @" Y b0 x) ^
print classification_report(y_test, y_test_pred, target_names=target_names)
( L6 X. S6 x+ \" y: ]! e5 pprint "#"*30 + "\n"
0 @5 `* t I% M1 a
0 C! X6 Q5 y& U$ u. w: o |
|