- 金錢
- 46
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 556
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 46
- 威望
- 3183
- 主題
- 0
|
import numpy as np3 ]5 }+ l7 R9 s# L
import matplotlib.pyplot as plt
# U' [8 v' u) Y/ P
8 W- ]+ Z) L limport utilities
* Y/ K7 Z: X$ p, G
4 s( u. r) @2 r! `* `$ m# Load input data
' {% L( \ o6 z8 `3 p% Cinput_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt', G# s1 F2 _, p5 c2 ~. |# e
X, y = utilities.load_data(input_file)
( J% ^ a8 o( P* Y0 q6 A7 |0 Z% _
, z* m. t8 Y3 p% s5 |###############################################
/ S# _/ z% n9 ^7 J) f# Separate the data into classes based on 'y'/ B+ Y: i: \9 s+ R. r
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0]); [, I5 ^# r6 Z/ x, g- t3 F9 V) V
class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])8 v# @5 S4 v8 w
" ^8 S+ M0 m& W) C/ h6 W# Plot the input data
+ ?, F) l5 f/ G) ]" f# Gplt.figure()6 J1 }/ Z2 q0 B% m) r
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
5 S9 S; o8 }- j( E2 o7 yplt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')
! ^; S* W) v; Dplt.title('Input data')
2 a- I! C9 P' v* w7 Q5 d
+ p# M- _- R9 {$ ]) h/ |###############################################
/ l$ P- U& j) A# Train test split and SVM training' N2 }+ L3 K# ] i
from sklearn import cross_validation' i6 w* V- N4 G. b8 ?2 B/ n
from sklearn.svm import SVC
/ ~% _( b% \7 D) G: k. c4 W# N! B8 V+ j( P
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
* u+ h* i! G6 h' O# `1 A6 s! [+ L; a
#params = {'kernel': 'linear'}( S4 p3 k; _5 b! f! ~
#params = {'kernel': 'poly', 'degree': 3}
& a8 F. y- T% L5 M3 Fparams = {'kernel': 'rbf'}
1 ^( {9 s. s$ {# C# r3 B3 y* sclassifier = SVC(**params)
- w9 N/ v9 Q4 M$ Q- x+ F) Rclassifier.fit(X_train, y_train)6 O) f+ F2 X& d2 D
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')# e& X& z5 P H; U
" g- e* |7 J+ S) [' z0 u- v) t
y_test_pred = classifier.predict(X_test)
2 }6 z, {/ }. Y) m# t( z6 Lutilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')$ f# f. ? t9 r
) O+ {7 e7 j# x. h
###############################################; G; @0 _" J5 b: N! Y9 h
# Evaluate classifier performance
. B6 f& `1 W2 x( Y; d3 Z6 t/ z: `9 ^; d Y0 b
from sklearn.metrics import classification_report
/ o' h4 K% R! O+ \" b6 @" U3 L- w" H1 b6 q) n. d8 r# M1 s
target_names = ['Class-' + str(int(i)) for i in set(y)]
5 C( `9 Q6 _5 Z/ [print "\n" + "#"*30
( D! ~' [. q& b. fprint "\nClassifier performance on training dataset\n"
) S8 o* N; F1 I7 d3 y1 f Mprint classification_report(y_train, classifier.predict(X_train), target_names=target_names)( w0 ~. i) I( P* A/ q1 m/ u
print "#"*30 + "\n"8 q3 g! `9 V3 n4 L& I! s
! d" P; ]: m9 l& `' Aprint "#"*30, \+ ?% o) X! q! G( X
print "\nClassification report on test dataset\n"! W6 D$ |8 u; q. i2 B* x
print classification_report(y_test, y_test_pred, target_names=target_names)
& y8 b( I3 N" C- |print "#"*30 + "\n"
1 U) A T4 g! x0 v4 J( C
% c# ?5 o& n: \ |
|