- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np4 ^4 D; U# e* j4 |
import matplotlib.pyplot as plt
. J/ u: s x2 z1 ~- V3 l1 c' z
T' T, q) K/ H% i& Pimport utilities
# D# t5 e( J9 h# e, `
( E; G* e$ l8 e# ?/ k H+ c' N# Load input data
; q/ t* S y7 C8 S: o5 D% Oinput_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'3 v6 e3 y }- v( X# p7 u* y" }
X, y = utilities.load_data(input_file)3 s& a5 c* m4 j% y$ q, o R
: t* n3 Q% j* q1 i5 X###############################################
* W, y% @, H2 b* ^1 s) \# Separate the data into classes based on 'y'- T; R. }0 \$ C9 I- U
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
; l t! ?2 U/ x/ T0 o+ } C; zclass_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
3 g! j5 Q* ~4 B, ~6 ^7 x9 b* a. f3 S' V; ?: A7 @
# Plot the input data2 m3 h, \1 G9 |
plt.figure()" d% H% g8 E1 {2 J7 v2 R
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
! o. Z! J! T, \( t6 o* xplt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')6 f% M9 x8 H Y% ?* L
plt.title('Input data')6 f/ e% a3 v" x/ E5 S6 n) X
* J* e! ^0 z" K: @5 c8 c
###############################################0 q. m# Z' ]8 B& l: K5 v
# Train test split and SVM training
+ J {# u: H# G0 L' [from sklearn import cross_validation( F. z+ m4 m; \% _8 R$ A' s. K
from sklearn.svm import SVC; M- I! J- f/ _
7 z* G2 C& g4 A! c% }( M
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
; @/ f% ~0 B# j) W/ _* ]; x- D& Y3 Y! ^1 N. `
#params = {'kernel': 'linear'}
4 h8 A4 v1 b& @# r9 T) D$ n#params = {'kernel': 'poly', 'degree': 3}, `# S2 f2 V* X# t# q. o2 [' Q
params = {'kernel': 'rbf'}" n' v! Q* @9 T6 {2 ~
classifier = SVC(**params): a% [/ _, k, c1 C
classifier.fit(X_train, y_train)
. [( c. U( |( p, H7 dutilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')
! s6 P9 V6 A1 h$ J s3 j$ P/ t9 T- c& M2 M( m( Y
y_test_pred = classifier.predict(X_test)8 {3 x8 S+ v* k5 Q9 H- E' y6 a
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')
! {7 A# J$ \! Z3 c6 s$ a# r) c* ?
: ]0 J! j6 A m& i- \###############################################
m" r% `$ G9 s. ~; T5 K2 f" S# Evaluate classifier performance- z# c: j" p. t" ?! [8 s& L0 L
, F# C' @& S/ A% U+ Z" ^" C
from sklearn.metrics import classification_report; A1 L6 L3 P4 C% c1 }; t" p
" ~2 _- k [$ Etarget_names = ['Class-' + str(int(i)) for i in set(y)]
# ~1 x' }8 [& c1 |3 t$ sprint "\n" + "#"*30+ E& K; Z& j, J6 Z# E+ t9 v% E
print "\nClassifier performance on training dataset\n"
" p7 E9 E4 G3 D7 g" h. yprint classification_report(y_train, classifier.predict(X_train), target_names=target_names)* ?% X5 S8 G/ b2 b
print "#"*30 + "\n"9 P7 e# T/ s0 T1 D7 R, M2 {3 n; l
: `1 B+ z* U- Z0 W8 Y# X5 l
print "#"*30
1 ^" |6 C/ q, t6 h8 I4 t' oprint "\nClassification report on test dataset\n"
, h- j8 Q# M. `+ U2 ?print classification_report(y_test, y_test_pred, target_names=target_names)% G7 M8 W% T, {0 i! @
print "#"*30 + "\n"7 d: d, u1 y( M' U; H. O
( E: j. U3 |% Z1 e- N0 e- E
|
|