- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np
) O1 s) M" O( Ximport matplotlib.pyplot as plt
; S3 Q, i* \+ b9 ]3 |% {0 G; q+ M' ^3 Z3 \
import utilities 7 w7 q* @# _2 @- a! l! G! a
' M1 w1 Y, f% P1 a& }: g# Load input data' M. X! e! f3 e
input_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
3 [3 ]7 |% ~! n) z- d2 FX, y = utilities.load_data(input_file)7 j# o M. z" Q
9 G( O6 L: G( w3 y###############################################
0 \' P# H$ Q% k# Separate the data into classes based on 'y'' y8 L" N l" Y
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
. Y7 z) R4 `; g( A. y/ W; Mclass_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
+ @4 u s7 b+ j3 P+ `2 t% Q
: Q9 f8 \8 l# I# Plot the input data
|% [: S# m' A3 B& Splt.figure()8 ?' v0 s4 s7 A( U7 a% K8 d
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
0 r; L0 P4 k+ A3 `9 q s* c% hplt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')
0 ~0 T0 x" n( J/ Splt.title('Input data')* }, ]( ^& Q' a
; C# s/ q0 x$ E z7 S, q###############################################: }( O W/ ?8 I3 {7 d. I
# Train test split and SVM training( C) s/ c7 |4 p. \7 d1 _) r, y
from sklearn import cross_validation
9 _: k6 I. r) r" F! L9 Y" ~from sklearn.svm import SVC
! `4 B T8 S, n4 o2 [5 X( T9 G1 @% ~+ `
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)$ L3 z7 j: g* u& ]- z
" Y. f w* x' `# C1 w#params = {'kernel': 'linear'}7 Q1 {3 _+ c1 ~- n5 z" O
#params = {'kernel': 'poly', 'degree': 3}$ W- P+ k: r4 K' I5 N8 o
params = {'kernel': 'rbf'}
. E9 H5 c1 e/ n" b zclassifier = SVC(**params)
' q8 a! @2 Z) K. Qclassifier.fit(X_train, y_train)
4 Z* ?; C7 z7 k/ A/ h( F6 K' Autilities.plot_classifier(classifier, X_train, y_train, 'Training dataset'), _; z9 W3 E7 D- X+ p. j( V
* ]$ G9 l! f6 W j6 by_test_pred = classifier.predict(X_test)9 d* d: _& ^3 O6 |* S2 w0 A( L S
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')
, h# ^" c7 Z% Q3 \& Z: l( m
6 ] z1 F' M3 C3 P###############################################7 \1 D! u: p# C
# Evaluate classifier performance
2 O3 p4 N r1 w5 c3 O/ Y$ A2 N4 j' w
from sklearn.metrics import classification_report
% R1 f; m' x% u4 u/ X' U j! `8 G2 U$ w) G
target_names = ['Class-' + str(int(i)) for i in set(y)]
! U* O# D) I/ }5 T# B- l* C1 H: pprint "\n" + "#"*30
4 K/ P8 N$ @; Zprint "\nClassifier performance on training dataset\n") o+ e' J B+ j+ {+ y7 }
print classification_report(y_train, classifier.predict(X_train), target_names=target_names)$ X7 c$ x0 i0 i" z5 i0 Q1 p
print "#"*30 + "\n"
}0 I4 L; K3 j+ S6 E8 i
2 m% ?7 x ^/ M4 hprint "#"*300 M- u3 e! J: d. K: {* [
print "\nClassification report on test dataset\n"
7 r5 G$ B4 a( E' Q! D/ @5 n! ~print classification_report(y_test, y_test_pred, target_names=target_names)
* |5 w% e" m0 Y6 H7 `. _3 Mprint "#"*30 + "\n") I6 o* P- N9 g9 H
& c6 `1 w2 K" C) G8 G |
|