- 金錢
- 46
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 556
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 46
- 威望
- 3183
- 主題
- 0
|
import numpy as np i; V4 V: [. [/ v( Y, `) _" i
import matplotlib.pyplot as plt
; ~; v8 I* k+ w' X6 i" B# p& Z: D% P4 a% R: H
import utilities
5 B$ h9 c; b0 p" I/ m! w& u% n9 O! p* [. n5 @: ^& e* ^+ x
# Load input data) o) U$ ~: O: U, D# h3 O
input_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
0 H7 {/ ^+ b8 L f( g! R6 K0 M9 zX, y = utilities.load_data(input_file)
2 I0 m* K- ~1 a% V4 \) g
" ~3 g- M. d$ o0 G/ K" Y###############################################
Q z% H0 z8 Q8 k! t: H# Separate the data into classes based on 'y'# D2 G7 {5 X1 \5 q
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
4 X+ S# b% h+ }) S4 G! Tclass_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])+ S3 J. z; p$ _
8 _( x N% u( q( {$ P+ ^# Plot the input data
( F$ D' }8 }" y) L \# M# z6 splt.figure()9 q) s3 ?% z! }. H
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s')
; L" Z& c0 N: `) N# i/ |2 splt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')
! V* W; W% B/ D* n. B! e; v5 Splt.title('Input data'): `1 e- }9 J+ k4 L2 t- A' L
) N- R# _3 v3 I8 }% j
################################################ D$ g: O5 U) ?$ f, s* s y
# Train test split and SVM training
5 \- ^0 Z Z6 p/ h- h& zfrom sklearn import cross_validation
+ o8 V8 E6 z' Z/ k8 \( r9 U pfrom sklearn.svm import SVC8 | Y* R# M0 v8 k1 }
1 y/ \" }# V% d: H7 J
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)/ X# {( f* j ]/ a
2 n/ y0 o- C* v8 Z#params = {'kernel': 'linear'}
5 I+ X7 D/ j8 {8 Z( f#params = {'kernel': 'poly', 'degree': 3}
: L" L' a8 g( N3 \params = {'kernel': 'rbf'}
5 {; G. z+ p: n/ g, Gclassifier = SVC(**params); j# S, [/ q3 M1 A6 v
classifier.fit(X_train, y_train)9 |5 v- R8 f) e4 ?
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')" j: x7 Z: \% { Q8 O" j
/ p0 f, E7 `, o" j( s* w6 T/ y
y_test_pred = classifier.predict(X_test)3 M2 H1 i% c7 B9 Z) h1 j
utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')! R2 g' Q5 Q+ X6 q [. z
( k. ]6 j8 z& X/ t- {8 T* T1 `###############################################8 J7 @) W* M3 u, Y. s+ `1 ^4 E
# Evaluate classifier performance
. V0 n g! h0 X* ~# [
9 ^0 V& _8 f- H t# Vfrom sklearn.metrics import classification_report* v" {7 W) Z/ _7 r* u; w
3 h: w* A8 W# `$ c1 Z* |: n5 Otarget_names = ['Class-' + str(int(i)) for i in set(y)]
, B4 x7 Q; x+ n1 y& H% ?) Oprint "\n" + "#"*301 W" ~# Y2 c2 ~2 O
print "\nClassifier performance on training dataset\n"& `" { c7 B, Y$ [
print classification_report(y_train, classifier.predict(X_train), target_names=target_names)9 I0 ?$ p/ b- r" R7 E2 K
print "#"*30 + "\n"8 |$ t( ]4 ^2 P% x' ?$ S( P
3 [: f |; A/ B- Rprint "#"*305 l( [, J ?, }9 s
print "\nClassification report on test dataset\n"% l1 W$ I+ p8 u7 ?7 Q6 }9 e
print classification_report(y_test, y_test_pred, target_names=target_names)
9 E4 c0 F: C$ p Pprint "#"*30 + "\n"/ Z. r& h( a0 E: h. r
. n0 |6 K# |# i0 t0 i2 Z
|
|