- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np4 r5 j# n, r* ~3 |/ a3 F T
import matplotlib.pyplot as plt
* @: j8 O3 }" c
u# T! J# U3 Z9 Simport utilities $ A# i. M1 x5 L" a
1 Z7 e& N5 z- q1 m+ M
# Load input data/ i- m, H# S+ D6 u
input_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
- w7 n6 j, e. ^X, y = utilities.load_data(input_file)
( Z! D) ?, e* ?# `* P
- O, B5 Q* ?1 c% K9 ^###############################################8 B: G. Y4 g# q8 ?0 d- _
# Separate the data into classes based on 'y'' L% z5 p+ j/ C2 o7 R6 p
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
% H; D/ n0 ]6 i! v$ L2 Mclass_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
7 O) L2 L7 G" e) Y. g/ ^- l7 w( C3 o: q% C6 W: j, [2 J
# Plot the input data
1 t7 x% _3 W5 r/ r7 aplt.figure()
4 x3 f. @3 k+ B mplt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
; X+ M7 q2 h% ?5 X v+ @0 Xplt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')7 `3 S; y x7 W" K$ c* e; v
plt.title('Input data'). C `$ ~* a- ^
1 W2 c, A4 \9 w# |
###############################################! H& n2 l9 b+ g6 L$ Y7 _1 f4 {
# Train test split and SVM training/ z9 h5 \! [: P
from sklearn import cross_validation
0 t3 N. C# D8 E" ?6 [' ~$ j5 q, O' zfrom sklearn.svm import SVC% Z! ^. t @, B0 [2 K
. {) T1 c6 o4 H. v% n% EX_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)3 u' v$ q) e9 f. L8 J" t: K
$ z% c4 G& D3 Q1 e#params = {'kernel': 'linear'}" C6 W h( M c
#params = {'kernel': 'poly', 'degree': 3}8 f$ }: Z! I4 x, m
params = {'kernel': 'rbf'}
8 w6 T8 ?; n6 d+ L( b& c( pclassifier = SVC(**params)
' H7 [! ~0 C* c E8 L" D5 Tclassifier.fit(X_train, y_train)4 t6 t% y0 Y4 d% ^* V# O3 v
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')
- K# ?4 [# B$ X. v% Z+ @, n* V7 s' V. |8 O+ `
y_test_pred = classifier.predict(X_test)
( w2 g- t" O) o5 r# Butilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')
V/ v' `0 O- u5 {' w* t+ A& S8 `# H/ A' f% \4 E" |
###############################################* Y% o e. {- ~/ P3 q! w: ]
# Evaluate classifier performance
1 ?7 ?4 e% {& a0 p9 P8 T' f( E2 M) B0 K) X8 w0 |4 K
from sklearn.metrics import classification_report/ X8 q. j+ w C v: x# B) ?
! B0 K; S% {& ~! }8 ^' t% U/ [+ a
target_names = ['Class-' + str(int(i)) for i in set(y)]0 p# d2 c& I H
print "\n" + "#"*30" t' B: a& c ~: ~
print "\nClassifier performance on training dataset\n"
5 d2 P, Z. x7 c9 ]. C# l4 Z! cprint classification_report(y_train, classifier.predict(X_train), target_names=target_names)) H$ o$ k" ^5 T6 G4 Y1 F/ s
print "#"*30 + "\n"4 f" d q8 M- B2 ~( P/ e
! k4 S2 p1 |+ j6 ~- Bprint "#"*30
# U- X# l' c; E8 [7 z* nprint "\nClassification report on test dataset\n"" t/ }3 l, U0 K# j: w1 X
print classification_report(y_test, y_test_pred, target_names=target_names)8 q+ ]4 C: m0 B7 C3 l
print "#"*30 + "\n": \3 E x% h' h
! b' ]! U9 P, |7 ], E4 U |
|