- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np- u" Q9 R, {, \1 j- c9 K6 Q
import matplotlib.pyplot as plt
9 z9 ?4 Y0 Z! ]# T
$ j; c5 a' N# g6 b! bimport utilities
- h; D& J- h) l0 a- j9 w! J& g1 t. p4 i) r) Q3 z, l2 c
# Load input data5 Q0 u. w* J/ r2 {) [
input_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
4 A& o1 i3 o& G% Z! b* V$ I2 ~X, y = utilities.load_data(input_file)
! s' w: p' l; ?' _
0 {6 a. P/ F; v: M1 B0 U: [###############################################1 x3 T2 l _2 E3 z- U( r/ k
# Separate the data into classes based on 'y' a$ A# ?) T4 ~- L3 W
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
$ x- [! `/ p, v0 u) C) Nclass_1 = np.array([X[i] for i in range(len(X)) if y[i]==1]), x9 b! d; Z$ {' O+ N, F
1 n: [8 N- p+ g
# Plot the input data
6 c$ E3 i7 ^( ^% M3 f% u8 Rplt.figure()' T! c. R* h7 `3 f3 L: v, }" d; i
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
0 t; ~: W4 _. D( Bplt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')
4 a: [4 e( I3 z/ j/ J7 U" oplt.title('Input data')- T! l% h5 ]4 O- J
9 U$ n7 H6 P0 J. j
###############################################; y; R- z ~$ ~/ w: [5 |9 x
# Train test split and SVM training# q. n) ]5 L# o0 q$ S: o; D& K! F7 H3 H
from sklearn import cross_validation3 _; B5 B ]% j& R( E3 |$ I
from sklearn.svm import SVC
: Q R) u! v1 o( i' [/ z- K7 Z9 x7 w" A) W6 N+ r1 K
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
. l6 f6 a, c8 |( L2 \+ H" a
5 K2 d& _# w8 S3 m#params = {'kernel': 'linear'}1 `' A! `1 c; W% L# D
#params = {'kernel': 'poly', 'degree': 3}
+ _. m* h/ i# Q. F. U$ j: V9 j5 Pparams = {'kernel': 'rbf'}
. l2 ~4 V. ]% u3 o( }, H2 Wclassifier = SVC(**params)
2 g6 |' J) I9 h: l6 L* mclassifier.fit(X_train, y_train)
' U" M; N# P$ a. j6 H$ ?utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')
0 c! m* ?9 d' c
4 d& x) m! n0 N" r# C4 ?- fy_test_pred = classifier.predict(X_test)! `; r5 t% \% X* {3 K8 Y
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')% r/ b* z: j# T; x* r' ?
( `! m3 n, B+ L+ y; p###############################################
% e6 A# Z" ]4 R( w# Evaluate classifier performance
a8 d5 D) x' [: y2 _1 Z
( Y3 a" J( c: E8 u, M# efrom sklearn.metrics import classification_report
l. B( V( l6 e9 [0 m, V' n3 W A* b7 J8 I- B0 f
target_names = ['Class-' + str(int(i)) for i in set(y)]9 E0 h5 y) w+ h! J
print "\n" + "#"*30
; {, S( {5 Z7 R3 v+ | w0 ~print "\nClassifier performance on training dataset\n"# K& M3 o X+ z
print classification_report(y_train, classifier.predict(X_train), target_names=target_names)# ^. C6 {/ i5 ?
print "#"*30 + "\n"
& L6 c+ i# }! q3 Q; c1 R& P' N) U& ]9 E' V P6 \( r7 Q- T) @+ |
print "#"*30) J! F5 R; o: B! r
print "\nClassification report on test dataset\n"3 \& X# I7 `; Z( y) h# K" F- {
print classification_report(y_test, y_test_pred, target_names=target_names)
9 J1 J0 ~# m# eprint "#"*30 + "\n"
* ~8 l [! T- T) T( S" b
# N* g a4 f% E$ e |
|