- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np- w9 T! P& X' T
import matplotlib.pyplot as plt1 a* s. @2 M' U: ~) z
2 Y( Y4 f9 u/ ?! yimport utilities + m& b% l# \' o7 \ Y3 J" ]7 p
# e3 q% |" r$ ]3 @$ b4 {# h3 A# Load input data
% e+ [, W4 i2 w, p! x" Kinput_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'3 K% W, l/ A& A
X, y = utilities.load_data(input_file)4 C0 [ ?+ R5 V t( c4 o
U% K& i# G0 y: Y5 R; n3 m
############################################### b3 N! @( u" o& u7 X: q% ?
# Separate the data into classes based on 'y'
( d% @% o% w: T5 V: cclass_0 = np.array([X[i] for i in range(len(X)) if y[i]==0]), {4 A: I6 c, U( m6 a
class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])4 b7 t5 ^+ I. `) \1 c3 E8 o
: [ d3 s: B$ k' l, J5 v2 g) ?
# Plot the input data
' J( w8 n' ^5 mplt.figure()
4 L+ u+ H& H( n3 c6 nplt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
9 o9 S% |$ @: kplt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')# r/ b# k. s: W3 s
plt.title('Input data')$ S. H! }7 l* L0 ^. l
7 Q% \ P. Q% j, h7 l: i, k###############################################) y, I7 |6 c$ [! R. R3 |
# Train test split and SVM training4 r- t0 o/ C7 F. N) p: f2 d. ]5 w7 N$ V
from sklearn import cross_validation
, ]" s7 U) H! E; B8 J& a7 pfrom sklearn.svm import SVC& s9 Q- }3 b! B& W' Z8 }. |
$ Z2 c, D& E! H* g0 \X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
+ Y% @8 r- Z. @9 _+ N3 K1 \" D2 W% b" l0 o
#params = {'kernel': 'linear'}. V; E" w& C$ U8 R) i3 p
#params = {'kernel': 'poly', 'degree': 3}, D- q& W1 B! ] I, i
params = {'kernel': 'rbf'}
/ h- O) |0 O6 D$ J6 rclassifier = SVC(**params)
/ m f* ^) x) a& ?( z% u+ ?classifier.fit(X_train, y_train)! Y9 V. t! a) G& \9 c
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')
+ K# d% L% y) F
3 F) g9 X2 b" z7 ty_test_pred = classifier.predict(X_test)2 N7 @) \$ Y; S8 |
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')/ P1 f/ X5 E$ z( E& W3 \* e
- @% h/ H2 a% s* J! {. W4 @###############################################3 l4 Y: F, ]! k' b
# Evaluate classifier performance
, u, i$ @7 a( G6 g& R" y. w# V
! x7 B7 E" D1 s ?from sklearn.metrics import classification_report
$ Z9 u" X5 S" ^1 a( P y2 n4 Q( B7 z
target_names = ['Class-' + str(int(i)) for i in set(y)]2 j' P) G7 e: n, G- M* Y r
print "\n" + "#"*301 y% P% l( M! W
print "\nClassifier performance on training dataset\n"$ `$ Y( S/ V/ V- Z! P0 [
print classification_report(y_train, classifier.predict(X_train), target_names=target_names)1 C' w, c- ? Y5 O3 ~" R
print "#"*30 + "\n"
: d( Q3 [' ?+ {$ k. Z5 g$ K4 x' J" c, x1 x* Q
print "#"*30
* M" b- |$ G& H6 Nprint "\nClassification report on test dataset\n"1 r/ _; q( t" b* r% _; p# X
print classification_report(y_test, y_test_pred, target_names=target_names)- K! T! l' a( p; p# J+ L8 k
print "#"*30 + "\n"& C; J+ |! H2 y: ~# S
3 }4 {1 }" ^% p: c. k; q/ i |
|