- 金錢
- 45
- 威望
- 3183
- 貢獻值
- 0
- 推廣值
- 0
- 性別
- 保密
- 在線時間
- 38 小時
- 最後登錄
- 2024-2-25
- 主題
- 0
- 精華
- 0
- 閱讀權限
- 70
- 註冊時間
- 2012-3-17
- 帖子
- 553
 
該用戶從未簽到 - 推廣值
- 0
- 貢獻值
- 0
- 金錢
- 45
- 威望
- 3183
- 主題
- 0
|
import numpy as np
8 i( p8 r+ N$ ?: j3 H2 \import matplotlib.pyplot as plt
# U2 _2 V2 ~/ j6 Q
4 _; Q' `% H3 E* G9 c* h7 m8 iimport utilities ; g; O! c* D/ E+ A" c# ~- @
E O& N% q% S+ u1 o
# Load input data
2 Q; D+ W* R) Y; cinput_file = 'D:\\1.Modeling material\\Py_Study\\2.code_model\\Python-Machine-Learning-Cookbook\\Python-Machine-Learning-Cookbook-master\\Chapter03\\data_multivar.txt'
/ v" o4 ?* q' Z$ y' pX, y = utilities.load_data(input_file)& @1 ~1 ]5 G; A, P) ?0 J E6 q
; I. M6 T0 n* K+ G d8 x% L###############################################
9 P: a7 o n- h/ \# Separate the data into classes based on 'y'$ }% c( A( t/ J& \1 ?8 |7 i7 x
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
. r0 y+ z- m+ p( d3 I1 p3 L! o6 C0 Eclass_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
0 v4 p9 [: d* @
8 x2 l5 N) s2 W& @# U/ Y7 ]# Plot the input data
: d1 i+ _4 B& y# z4 pplt.figure(), s' W! c4 F" }) d% @: a |
plt.scatter(class_0[:,0], class_0[:,1], facecolors='black', edgecolors='black', marker='s'). g+ L$ D" t0 z7 C" u
plt.scatter(class_1[:,0], class_1[:,1], facecolors='None', edgecolors='black', marker='s')6 H' `6 t6 a. G l5 ^# L! T+ q7 V
plt.title('Input data')
6 N, Y0 G. Z# P% W
2 ]+ N0 j" H, W- r###############################################& L$ \) b! M9 f$ v+ b: W* j$ D
# Train test split and SVM training
8 z2 n+ o- M3 F7 |$ S$ ifrom sklearn import cross_validation2 B4 }. g: A. r( U% t
from sklearn.svm import SVC) C+ q1 E1 e8 l! [
4 Y; @$ D. `$ n: jX_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.25, random_state=5)
+ C1 m% k; W: m% k6 u
( w* ^- h9 J( w- R- d+ a1 e+ H#params = {'kernel': 'linear'}
. f8 |0 G T" v7 r2 N#params = {'kernel': 'poly', 'degree': 3}9 t9 J) k6 w9 A y. X$ Y
params = {'kernel': 'rbf'}2 C3 G8 [; W( S6 j2 A; M
classifier = SVC(**params)
+ m$ V v. f& o/ C% Z0 K/ Bclassifier.fit(X_train, y_train)9 s8 m& Z6 V' S1 Q2 P9 p- [
utilities.plot_classifier(classifier, X_train, y_train, 'Training dataset')) [/ [- u0 N7 }% _* D! s) X
5 q5 I9 E1 b6 V3 ?; B4 Q% Oy_test_pred = classifier.predict(X_test)
( u3 U" q7 e/ |utilities.plot_classifier(classifier, X_test, y_test, 'Test dataset')" }' T6 S U" e) C, t2 ^
2 ?, P7 V6 {5 r2 D/ X; j
###############################################9 \7 T) L" v% o, Y
# Evaluate classifier performance
! _. Y* B0 R8 A" P) y& p6 l* p9 m% w2 g8 Z
from sklearn.metrics import classification_report, A4 U: }7 [0 _% u4 f& H/ o4 x# ?
3 [" }4 u) ?) p4 u4 p9 ytarget_names = ['Class-' + str(int(i)) for i in set(y)]9 m: O) ]- `6 \$ N
print "\n" + "#"*309 M4 d8 h: @$ _2 t5 |- l3 w
print "\nClassifier performance on training dataset\n"/ d; T. V) Q* {# A z4 Z
print classification_report(y_train, classifier.predict(X_train), target_names=target_names)% q% L% V5 v- L" I9 q2 W4 M! T& R
print "#"*30 + "\n". m! f1 M# F2 N8 ?* W0 }
' [$ k# \6 B' B
print "#"*30
, U+ w% y$ `3 s9 o; M# S" E- [$ sprint "\nClassification report on test dataset\n"
; d( o: l$ W+ w# w% Wprint classification_report(y_test, y_test_pred, target_names=target_names)# V% k$ R, J/ h' D5 y
print "#"*30 + "\n"5 J* C$ H4 ^, u1 g- }3 Z; G
" }5 i s8 S& Z0 B* t |
|