- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np$ Z Y- ^3 k# c3 A5 {; Q; ^# s
import matplotlib.pyplot as plt* d, j s4 d& `/ z
" A$ n8 b `8 ^+ }4 ]4 Ximport utilities 6 o! |5 t( l9 Y* Z0 N: H
: D. s* G! _: S" r# Load input data" R: x; u, Q, {# B7 x
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: G. ^1 U1 a! d- vX, y = utilities.load_data(input_file)
4 d. J& f" u, Q: O2 c: J
& y& P. u. L5 Q2 z+ F' Z a; M& P###############################################
/ ?0 h( V, u: x! l) p) E; W) R/ F# Separate the data into classes based on 'y'' E' L1 t% Q+ M$ ?1 o. N
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])% P5 V0 }3 C; E) ~) U; U) H# d
class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
; R5 z/ _( k4 c' B- P2 i: t2 a6 L9 J2 J( t3 T
# Plot the input data# z: l. z1 o- d
plt.figure()% n% ?) E& U8 A o% Z/ Q; J
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s'); }. T m. \3 f2 T
plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')$ E4 N. p6 H5 v& j1 d
plt.title('Input data'): @" l9 m1 c2 ~/ e- _ t: W' x
9 k/ P0 V+ \" Y, H
###############################################
; f; S2 c# t5 K6 v/ R) `# Train test split and SVM training3 n9 B4 m% b+ j) m
from sklearn import cross_validation( H/ b1 n9 g, @" T( z" D
from sklearn.svm import SVC7 T2 ^1 |. i _; M( l# {
& M$ C+ U A" e" p7 m7 v7 HX_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
+ L2 k, q% {& t L
. O5 M- n2 I% O5 I+ k( \% K& F( |#params = {'kernel': 'linear'}7 v8 l* q3 }' q6 Q2 Z. E
#params = {'kernel': 'poly', 'degree': 3}; k) W, c E" G( U- I+ ]6 Z! S, J
params = {'kernel': 'rbf'}- R- U( y" T, [. ?$ I5 V
classifier = SVC(**params). K' g8 z0 |0 [5 B' d
classifier.fit(X_train, y_train)
2 \9 ^6 \$ ?5 ~: w1 Nutilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')
/ S0 l8 S# D% }. ?# k* ?
4 u# y1 W: p& j; ^2 n1 \( q! @y_test_pred = classifier.predict(X_test)
( C G) h U1 L5 O, u8 ]0 Eutilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')* o+ p" U0 l$ x9 M
$ t& l8 ~- P- c g
###############################################
( X' T. r# N, h z% B* Z$ n# Evaluate classifier performance5 f8 u0 E/ W [) u: k
& j& M t& v/ ~; m* ^& o
from sklearn.metrics import classification_report
y8 u; {5 ?; q x% t9 b! i# Q S8 ^ K: f: A: |% Q9 E9 I0 X& q( e
target_names = ['Class-' + str(int(i)) for i in set(y)]3 X9 G( X2 e) Z
print "\n" + "#"*30( f0 N! q, o$ S8 U! h
print "\nClassifier performance on training dataset\n" H) B9 i& x) j0 ~; R. P
print classification_report(y_train, classifier.predict(X_train), target_names=target_names)& L! G* n7 g g# L
print "#"*30 + "\n"
9 N/ w5 e7 C1 E
0 v0 v! b, W5 x3 D6 A- f$ c9 p; Sprint "#"*307 q, Y" @ R8 |3 O0 D# c1 E
print "\nClassification report on test dataset\n"7 J& T5 _8 F5 p% ~7 h% Q
print classification_report(y_test, y_test_pred, target_names=target_names). G: Z% b) J7 `0 [$ [" Y% e! \
print "#"*30 + "\n"
6 I$ z$ d" z5 [0 V# U( `$ z' U, ^0 h! k$ u$ g* q, S8 Q
|
|