- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np
7 v9 o5 n [- {) r# z Uimport matplotlib.pyplot as plt8 t) @+ C4 T" B
' e$ g& F5 H F8 Q
import utilities # ?: f$ j6 B- a4 D* x) ~! w
9 _2 P& c/ V) C: O$ q: E8 v. R; w( \
# Load input data: }# J4 R" b" e: b
input_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
Q E7 q0 I. vX, y = utilities.load_data(input_file)
9 r* d2 t; L8 b& {" n8 z) X! i: O8 m" I, i, V+ d
###############################################* h- t; @6 B( P5 p2 \
# Separate the data into classes based on 'y'* ?" V, j0 C$ l7 r* L0 D' F, F
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])5 Y; W8 x$ x5 g n9 @
class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1]): Z' _1 G2 @: W
: W8 e8 p3 {4 W4 e
# Plot the input data
, q8 b# |9 N. M! O1 \9 |, Eplt.figure()
/ |( z8 D* o! `; s7 @plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')% o% ?; Y3 B$ t, f+ v
plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')
4 x6 ^& b8 ~0 O& n0 a$ Cplt.title('Input data')2 B$ ]# l' C; F. ~% N* O+ S8 o
+ Y/ w8 p m, ~, f
###############################################$ |1 `5 \5 t) s8 W1 K
# Train test split and SVM training
: M0 O) h3 J# u! }# a0 Lfrom sklearn import cross_validation3 D1 ?5 g; x! d) B) V5 u- R9 x
from sklearn.svm import SVC
+ ^! I+ J+ h; _/ B8 Z( ^1 m& X* x8 @" n+ k/ @# W6 O6 O! W
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
5 v1 y7 Q4 k7 G. \, y8 r, l5 U
#params = {'kernel': 'linear'}
g; `4 z8 e3 u5 y; l+ C#params = {'kernel': 'poly', 'degree': 3}' T6 @8 S' c/ z" u/ i
params = {'kernel': 'rbf'}+ t( q+ D! A4 ^3 v
classifier = SVC(**params)
8 \/ s% o& b$ Y& u5 b* ^classifier.fit(X_train, y_train)/ ^/ X7 x: M5 d4 L
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')" F% R! v/ U: }8 v% ~& e1 y
. {8 r2 M) |, V+ j8 r% hy_test_pred = classifier.predict(X_test)
1 h# k8 k6 Y" g0 zutilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')- ]/ t; g- P' F' q
% `) h f+ x; u3 L6 `###############################################9 R! W- q: g* h* D4 a. q8 Z8 z' R
# Evaluate classifier performance
7 Y6 ]9 j7 L, P
$ w) u' r0 J# O( |from sklearn.metrics import classification_report
# @/ P2 L4 F5 x* Z" y7 @) n8 S* d( X
target_names = ['Class-' + str(int(i)) for i in set(y)]
% g% o u; A6 p' r6 f5 C; ^6 ~# tprint "\n" + "#"*30' B. o9 }+ w/ v; t G9 U; t* C
print "\nClassifier performance on training dataset\n"
& m. z+ U- G6 J) \' }8 r" ^/ Mprint classification_report(y_train, classifier.predict(X_train), target_names=target_names)
4 U; B1 f6 v9 E2 z% `+ d; vprint "#"*30 + "\n"
% l6 E- `, q: \4 G* z6 ]5 i7 [( V. v( v" f
print "#"*30
3 A' N7 K6 [ ?0 s7 Qprint "\nClassification report on test dataset\n"7 F+ p; N+ a7 S6 {" W( K
print classification_report(y_test, y_test_pred, target_names=target_names)
/ G5 j% [1 _- t5 ?) t6 c" o2 hprint "#"*30 + "\n"
! _0 Y5 S% I" c
+ F0 h L3 E# g a |
|