- 金錢
- 46
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 556
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 46
- 威望
- 3183
- 主題
- 0
|
import numpy as np4 | m9 ?: i+ i
import matplotlib.pyplot as plt
6 N* ~0 _5 L7 w1 ?4 o; U' }5 E& e9 ?4 ~, {( X7 d; T
import utilities ' b5 q6 e, m7 @2 }3 f( B8 u
9 s5 N/ ]) p; g3 v; [) z
# Load input data
4 d1 c/ c( T, L2 a3 _( C8 I- kinput_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'/ c" ^0 E1 A) z; H: Z5 D6 R0 c7 u
X, y = utilities.load_data(input_file)
* A+ d# k& W, k- d
' K/ v q/ s9 m2 {# j( U L###############################################( W m. |- v& S
# Separate the data into classes based on 'y'1 b3 E( q. C) i4 T4 T; k
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])+ D5 b5 S+ a0 { \) Z2 Z6 a7 j" N8 Q9 `
class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])1 k' z0 G `! ?- n# Y* ~
$ m/ A2 n4 u1 {& A$ y, t" Q- C2 Q2 q# Plot the input data
2 J. D' P$ l7 vplt.figure()0 X3 ~. Q1 r, I* c$ \
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
# i- v; G( X$ Y2 T0 x& j$ Rplt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')3 ]' z3 f$ V! O3 q# c, @
plt.title('Input data')
. v! a+ |7 F, ^2 e/ I/ p. J/ C
% \5 p( N& z( m6 @5 k2 ^ ~( ~& X###############################################
! z) d9 ]# L8 q8 ^+ u! ?2 r+ f# Train test split and SVM training8 z# S( b2 L/ F& O
from sklearn import cross_validation
& b- k7 y( L1 r* t* y9 T" O3 Q% M, s% Pfrom sklearn.svm import SVC% @" B. F% M5 G! j
) Z8 c6 D. _3 q8 OX_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)% v6 f& `* L5 n; j# V4 b1 r
6 o' H# P. U$ Y3 z. D0 u
#params = {'kernel': 'linear'}
, D+ Y# i; J" E/ C' w#params = {'kernel': 'poly', 'degree': 3}! B, F1 [& s, \4 y; C4 o0 R% m0 I
params = {'kernel': 'rbf'} l; G! V9 g6 g7 B; t2 K
classifier = SVC(**params)! K0 `: @. w: L" |
classifier.fit(X_train, y_train): _3 c$ y/ S- Q% }* Y
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')5 T* C. r8 W' J6 V' n2 o9 g4 [
% d2 k1 O7 h) G8 l% ]" Q) O& c& [
y_test_pred = classifier.predict(X_test)- I/ x6 i# q0 H1 ~' F- R; o; s
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')! |9 n/ e3 ^. _% x2 p; d k
5 m/ W G7 d/ n###############################################9 t! W3 } o. W3 X- G
# Evaluate classifier performance
# R$ S$ s( O3 g2 Y* q: N6 o: e }& y( j( O+ W, [' o
from sklearn.metrics import classification_report/ Q4 t$ A% W$ X+ ]( Z- g3 ~
: ?# ]9 R. \& i6 x" M
target_names = ['Class-' + str(int(i)) for i in set(y)]0 l2 ]$ j, p1 R5 {/ J
print "\n" + "#"*30
2 A3 q- p: z0 k% q$ V" r' \9 Xprint "\nClassifier performance on training dataset\n"
' D' i. y- j: n" q/ P; y* Sprint classification_report(y_train, classifier.predict(X_train), target_names=target_names)) O# s1 S0 d% W* A
print "#"*30 + "\n"
8 R7 [4 f' d! o9 m, L+ A5 W- O; f- S* h4 J
print "#"*301 s- }2 q+ k( S% T/ G
print "\nClassification report on test dataset\n") Z$ K9 v t/ C; ^7 o0 z+ x, J9 R
print classification_report(y_test, y_test_pred, target_names=target_names), q/ q" N0 I5 Y' g; k
print "#"*30 + "\n"6 l6 w0 @$ Y( }7 a: G$ e
6 w/ j' x, S3 y: f+ ~# q1 ` |
|