- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np
0 A+ K/ ~' W$ @7 g4 Q( Ximport matplotlib.pyplot as plt
7 D2 j3 k! I9 j) q4 d( Q1 }7 e+ F7 N1 c3 O- |
import utilities 8 T d0 H: k4 A y: W* Z" @# [
5 H- T2 G2 b! q; q. p# Load input data v$ V1 t; X4 }
input_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
- z1 M/ D$ V1 jX, y = utilities.load_data(input_file)
/ C6 F- Q, A) U. _! }
$ B9 `/ I2 A1 P4 Q7 p, F###############################################
9 d, H' |- M# _. _7 a" M$ O# Separate the data into classes based on 'y'
3 i! j- j2 ^0 z7 H4 x; [ eclass_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])0 {* @* g# R- T2 s
class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
+ R. P0 m& x3 l$ z( t
w" ?& T, B8 k7 g& g# Plot the input data2 h- G6 v8 ~# E7 q
plt.figure()
/ ]0 d5 J8 i. Y* l& K2 u5 Lplt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')8 k2 P/ p3 r$ J
plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')6 i& u2 N7 h, |7 v7 y) e7 B
plt.title('Input data')
; m- n) K! S1 w% z0 |
& [1 R: K2 z- V& |+ @' l0 W) s( h################################################ T! d! Y8 N8 j+ \$ ~4 W% }
# Train test split and SVM training
5 M; @+ C3 @, B7 y6 _3 Sfrom sklearn import cross_validation) q& Q, m. a" o& j" H* r4 t
from sklearn.svm import SVC# f$ J4 y* ~% Q+ r) v3 S
0 _( J( G3 i: Q0 i/ \* @X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
+ I) ^, T$ c& M7 i$ | P" X; p
" a4 a$ U, G: ?) q. `. u% O% D#params = {'kernel': 'linear'}0 d: d. r5 K9 q" n- p/ Q
#params = {'kernel': 'poly', 'degree': 3}! U, Q5 L& U4 d
params = {'kernel': 'rbf'}. T4 ]0 |9 ~8 Y6 s8 |
classifier = SVC(**params)
' c: N1 U. e' Z! Dclassifier.fit(X_train, y_train)
) m% c6 r* y# \1 \" C( futilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')
. ~7 [# y2 w& J: d! t$ J* A
- s( f' q4 [5 `: K# by_test_pred = classifier.predict(X_test). W y# b! D, J0 B) w3 e
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')
1 J7 X7 X. N. C2 s3 e+ V. X1 U2 O# g) \7 k7 b
###############################################, s- m9 |2 W9 Y6 s( t: D& Y; c
# Evaluate classifier performance
8 F! W- k+ N2 T8 a4 P. b
% [. a, O! m- \& n1 u: Nfrom sklearn.metrics import classification_report; p1 t' ^; F& l5 n0 D7 g# e: {
- a8 s1 {8 {7 `( X; ?0 starget_names = ['Class-' + str(int(i)) for i in set(y)]
; e: L) x( } q7 ]9 Rprint "\n" + "#"*30
- ]" @, P% {5 K+ Pprint "\nClassifier performance on training dataset\n", m3 z- a% N& O5 z: g2 E. U- _
print classification_report(y_train, classifier.predict(X_train), target_names=target_names)4 m: b2 k* a& D
print "#"*30 + "\n"
2 X, R' |& R( c+ T: t# N2 r6 k( p m
print "#"*307 h5 j4 S1 q s5 D6 w
print "\nClassification report on test dataset\n"4 U* D# J+ k- g* f. A$ C" U x* v
print classification_report(y_test, y_test_pred, target_names=target_names)
2 M3 T' c& o" N$ Qprint "#"*30 + "\n"
' ^& Q; n+ U0 p# `- Z$ ^
" S" g$ B$ g. Y2 p5 z3 _7 Y |
|