- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np
r0 {2 Q; M: T! V: B6 ?3 {import matplotlib.pyplot as plt3 i, x1 n! X' d/ }- Z
' V$ }4 K' h6 _( K0 G$ M7 Q; gimport utilities ! O, M8 ~1 A9 ^4 V. L& f$ P& a1 ^
9 N2 {! |- `4 A
# Load input data
+ Q8 _3 U: z( t: b( ^, Q5 a( h. z* Hinput_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
' d# L& Q- M& W7 ?$ X/ KX, y = utilities.load_data(input_file)
: n; x2 `1 U+ g2 T( ?- H( c8 h: i& _* v1 V& R
###############################################! G' c/ ]) m1 Z; {: V
# Separate the data into classes based on 'y'" `* v& f5 ^0 f" J- x4 P
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
5 Q) D) b& h' p9 T* o/ q8 K8 C0 S7 wclass_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
; x/ k2 K+ M k9 e- ^
" T7 f* g$ X( X) z' y9 s; h& b# t# Plot the input data. T |; Y% A' t+ U- q! ]
plt.figure()0 C4 S/ M5 H* _* D* h2 a6 F# \' B
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
; ]+ e- ?% K' Q8 Zplt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')# r7 {0 w1 m9 g7 u( X! I
plt.title('Input data')
3 {/ E7 v7 c" u, N: e' K, t
: F" S$ B; j/ K###############################################
& Z& w6 F7 c/ ~3 {) x# Train test split and SVM training
9 s! R7 {& |; h; c; n% jfrom sklearn import cross_validation X+ Z9 p% _3 p9 \
from sklearn.svm import SVC
: i1 _) [3 m5 w7 k7 c% h% m- [3 T- `1 q
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
/ Q+ w8 m& L8 q! A7 x t6 Q5 A$ Q4 U) A y8 {. Z, x" `7 I
#params = {'kernel': 'linear'}
. K" x9 i7 K' Y/ h# @3 r#params = {'kernel': 'poly', 'degree': 3}
5 `9 p3 @8 @ J! ^params = {'kernel': 'rbf'}2 o( n M& Q& A+ P9 i
classifier = SVC(**params)
8 ?7 w/ m, K2 G1 a8 Uclassifier.fit(X_train, y_train)$ n! l1 A; L6 E0 d7 y; l
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')! j: J; N: T+ q, d! S( c7 `0 y
- z9 \' f8 Y6 n3 W u4 Y1 V6 [
y_test_pred = classifier.predict(X_test)3 c& g% W& B) `8 u6 d
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')
4 e/ Q3 y( v" m9 E; J$ i' x. B7 V# t9 e) B
###############################################
- k. ~ B5 M& ~, v# [9 M R# Evaluate classifier performance
& _4 ?+ E3 R) N: ]6 o
% m9 t) v" v0 e* Lfrom sklearn.metrics import classification_report
. s/ Z6 ] a& x2 s* H- K) q/ t* ~% E w M3 z8 E3 g8 y
target_names = ['Class-' + str(int(i)) for i in set(y)]6 E t" c7 x0 H7 [
print "\n" + "#"*30% V9 U+ G& } Z/ h$ n) s
print "\nClassifier performance on training dataset\n", Z4 T; H8 u0 T2 Z3 ]5 O
print classification_report(y_train, classifier.predict(X_train), target_names=target_names)
. u! @, S9 r8 ]1 H6 @* y Tprint "#"*30 + "\n"( t+ m3 m: j8 H
$ m# Q+ N* Y3 r* s0 s( R
print "#"*30: b* N: o; h8 Y5 V, b
print "\nClassification report on test dataset\n"
. j4 |' e# t# a6 M; kprint classification_report(y_test, y_test_pred, target_names=target_names). C. h n$ C3 h8 J/ W
print "#"*30 + "\n"
9 ~ W* s7 @7 g' w4 R8 v2 v9 c1 d) u7 N
|
|