1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Created on Tue Aug 7 17:57:41 2018
4
+
5
+ @author: Administrator
6
+ """
7
+
8
+ from sklearn .svm import LinearSVC , SVC
9
+ import mglearn
10
+ import matplotlib .pyplot as plt
11
+ from sklearn .datasets import make_blobs
12
+ from sklearn .datasets import load_breast_cancer
13
+ from sklearn .model_selection import train_test_split
14
+
15
+ def SVC_test ():
16
+ from sklearn .datasets import make_blobs
17
+ X , y = make_blobs (centers = 4 , random_state = 8 )
18
+ y = y % 2
19
+
20
+ mglearn .discrete_scatter (X [:,0 ],X [:,1 ], y )
21
+ plt .xlabel ("F0" ); plt .ylabel ("F1" )
22
+
23
+ lin_svm = LinearSVC ().fit (X , y )
24
+ svm = SVC (kernel = 'rbf' , C = 10 ,gamma = 0.1 ).fit (X ,y )
25
+ mglearn .plots .plot_2d_separator (lin_svm , X )
26
+ mglearn .discrete_scatter (X [:,0 ],X [:,1 ], y )
27
+ plt .xlabel ("F0" ); plt .ylabel ("F1" )
28
+
29
+ print (svm .dual_coef_ )
30
+
31
+ fig ,axes = plt .subplots (3 , 3 , figsize = (15 ,10 ))
32
+
33
+ for ax , c in zip (axes , [- 1 , 0 , 3 ]):
34
+ for a , gamma in zip (ax , range (- 1 ,2 )):
35
+ mglearn .plots .plot_svm (log_C = c , log_gamma = gamma , ax = a )
36
+
37
+ axes [0 ,0 ].legend (["class 0" , "class 1" , "sv class 0" , "sv class 1" ],
38
+ ncol = 4 , loc = (.9 ,1.2 ))
39
+
40
+ def SVC_fit ():
41
+
42
+ cancer = load_breast_cancer ()
43
+
44
+ X_train , X_test , y_train , y_test = train_test_split (
45
+ cancer .data , cancer .target , stratify = cancer .target
46
+ , random_state = 42 )
47
+
48
+ svc = SVC ()
49
+ svc .fit (X_train ,y_train )
50
+
51
+ print (svc .score (X_train ,y_train ))
52
+ print (svc .score (X_test
53
+ ,y_test ))
54
+
0 commit comments