http://scikit-learn.org/stable/modules/svm.html

分类-SVC

http://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.html#sklearn.svm.SVC

# !/usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "abdata"

import time
import numpy as np
import pandas as pd
from sklearn import svm
# from sklearn import datasets
# import matplotlib.pyplot as plt

mdata = pd.read_csv('http://data.galaxystatistics.com/blog_data/regression/iris.csv')

X = mdata.iloc[:,2:6]
print(X.head())
print(X.shape)
print(type(X))
y = mdata.iloc[:,1]

t0 = time.time()
clf = svm.SVC()
clf.fit(X, y)
t = time.time() - t0

print(t)

print(clf.predict(X))
print(type(clf))
# get support vectors
print(clf.support_vectors_)
# get indices of support vectors
print(clf.support_)
# get number of support vectors for each class
print(clf.n_support_)


######
### 示例:
######
# from sklearn import svm
# X = [[0, 0], [1, 1]]
# y = [0, 1]
# clf = svm.SVC()
# clf.fit(X, y)  
# # predict
# print(clf.predict([[2., 2.]]))
# print(clf.predict([[-0.8, -1]]))
# # get support vectors
# print(clf.support_vectors_)
# # get indices of support vectors
# print(clf.support_)
# # get number of support vectors for each class
# print(clf.n_support_)
##    Sepal.Length  Sepal.Width  Petal.Length  Petal.Width
## 0           5.1          3.5           1.4          0.2
## 1           4.9          3.0           1.4          0.2
## 2           4.7          3.2           1.3          0.2
## 3           4.6          3.1           1.5          0.2
## 4           5.0          3.6           1.4          0.2
## (150, 4)
## <class 'pandas.core.frame.DataFrame'>
## 0.001003265380859375
## ['setosa' 'setosa' 'setosa' 'setosa' 'setosa' 'setosa' 'setosa' 'setosa'
##  'setosa' 'setosa' 'setosa' 'setosa' 'setosa' 'setosa' 'setosa' 'setosa'
##  'setosa' 'setosa' 'setosa' 'setosa' 'setosa' 'setosa' 'setosa' 'setosa'
##  'setosa' 'setosa' 'setosa' 'setosa' 'setosa' 'setosa' 'setosa' 'setosa'
##  'setosa' 'setosa' 'setosa' 'setosa' 'setosa' 'setosa' 'setosa' 'setosa'
##  'setosa' 'setosa' 'setosa' 'setosa' 'setosa' 'setosa' 'setosa' 'setosa'
##  'setosa' 'setosa' 'versicolor' 'versicolor' 'versicolor' 'versicolor'
##  'versicolor' 'versicolor' 'versicolor' 'versicolor' 'versicolor'
##  'versicolor' 'versicolor' 'versicolor' 'versicolor' 'versicolor'
##  'versicolor' 'versicolor' 'versicolor' 'versicolor' 'versicolor'
##  'versicolor' 'versicolor' 'versicolor' 'versicolor' 'versicolor'
##  'versicolor' 'versicolor' 'versicolor' 'virginica' 'versicolor'
##  'versicolor' 'versicolor' 'versicolor' 'versicolor' 'virginica'
##  'versicolor' 'versicolor' 'versicolor' 'versicolor' 'versicolor'
##  'versicolor' 'versicolor' 'versicolor' 'versicolor' 'versicolor'
##  'versicolor' 'versicolor' 'versicolor' 'versicolor' 'versicolor'
##  'versicolor' 'virginica' 'virginica' 'virginica' 'virginica' 'virginica'
##  'virginica' 'virginica' 'virginica' 'virginica' 'virginica' 'virginica'
##  'virginica' 'virginica' 'virginica' 'virginica' 'virginica' 'virginica'
##  'virginica' 'virginica' 'virginica' 'virginica' 'virginica' 'virginica'
##  'virginica' 'virginica' 'virginica' 'virginica' 'virginica' 'virginica'
##  'virginica' 'virginica' 'virginica' 'virginica' 'virginica' 'virginica'
##  'virginica' 'virginica' 'virginica' 'virginica' 'virginica' 'virginica'
##  'virginica' 'virginica' 'virginica' 'virginica' 'virginica' 'virginica'
##  'virginica' 'virginica' 'virginica']
## <class 'sklearn.svm.classes.SVC'>
## [[ 4.3  3.   1.1  0.1]
##  [ 5.7  4.4  1.5  0.4]
##  [ 5.7  3.8  1.7  0.3]
##  [ 5.1  3.3  1.7  0.5]
##  [ 4.8  3.4  1.9  0.2]
##  [ 4.5  2.3  1.3  0.3]
##  [ 5.1  3.8  1.9  0.4]
##  [ 7.   3.2  4.7  1.4]
##  [ 6.9  3.1  4.9  1.5]
##  [ 6.5  2.8  4.6  1.5]
##  [ 6.3  3.3  4.7  1.6]
##  [ 4.9  2.4  3.3  1. ]
##  [ 5.   2.   3.5  1. ]
##  [ 6.1  2.9  4.7  1.4]
##  [ 5.6  3.   4.5  1.5]
##  [ 6.2  2.2  4.5  1.5]
##  [ 5.9  3.2  4.8  1.8]
##  [ 6.3  2.5  4.9  1.5]
##  [ 6.8  2.8  4.8  1.4]
##  [ 6.7  3.   5.   1.7]
##  [ 6.   2.9  4.5  1.5]
##  [ 6.   2.7  5.1  1.6]
##  [ 5.4  3.   4.5  1.5]
##  [ 6.   3.4  4.5  1.6]
##  [ 6.7  3.1  4.7  1.5]
##  [ 5.1  2.5  3.   1.1]
##  [ 6.3  3.3  6.   2.5]
##  [ 4.9  2.5  4.5  1.7]
##  [ 6.5  3.2  5.1  2. ]
##  [ 7.7  2.6  6.9  2.3]
##  [ 6.   2.2  5.   1.5]
##  [ 5.6  2.8  4.9  2. ]
##  [ 6.3  2.7  4.9  1.8]
##  [ 6.2  2.8  4.8  1.8]
##  [ 6.1  3.   4.9  1.8]
##  [ 7.2  3.   5.8  1.6]
##  [ 7.9  3.8  6.4  2. ]
##  [ 6.3  2.8  5.1  1.5]
##  [ 6.1  2.6  5.6  1.4]
##  [ 6.   3.   4.8  1.8]
##  [ 6.9  3.1  5.1  2.3]
##  [ 5.8  2.7  5.1  1.9]
##  [ 6.3  2.5  5.   1.9]
##  [ 6.5  3.   5.2  2. ]
##  [ 5.9  3.   5.1  1.8]]
## [ 13  15  18  23  24  41  44  50  52  54  56  57  60  63  66  68  70  72
##   76  77  78  83  84  85  86  98 100 106 110 118 119 121 123 126 127 129
##  131 133 134 138 141 142 146 147 149]
## [ 7 19 19]

回归-SVR

http://scikit-learn.org/stable/modules/generated/sklearn.svm.SVR.html#sklearn.svm.SVR

# !/usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "abdata"

import time
import numpy as np
import pandas as pd
from sklearn import svm
# from sklearn import datasets
# import matplotlib.pyplot as plt

mdata = pd.read_csv('http://data.galaxystatistics.com/blog_data/regression/iris.csv')

X = mdata.iloc[:,3:6]
print(X.head())
print(X.shape)
print(type(X))
y = mdata.iloc[:,2]

t0 = time.time()
clf = svm.SVR()
clf.fit(X, y) 
t = time.time() - t0

print(t)

pre = clf.predict(X)
print(pre)
print(pre.shape)

print(type(clf))
# get support vectors
print(clf.support_vectors_)
# get indices of support vectors
print(clf.support_)
# get number of support vectors for each class
print(clf.n_support_)


######
### 示例:
######
# from sklearn import svm
# X = [[0, 0], [2, 2]]
# y = [0.5, 2.5]
# clf = svm.SVR()
# clf.fit(X, y) 
# clf.predict([[1, 1]])
# # get support vectors
# print(clf.support_vectors_)
# # get indices of support vectors
# print(clf.support_)
# # get number of support vectors for each class
# print(clf.n_support_)
##    Sepal.Width  Petal.Length  Petal.Width
## 0          3.5           1.4          0.2
## 1          3.0           1.4          0.2
## 2          3.2           1.3          0.2
## 3          3.1           1.5          0.2
## 4          3.6           1.4          0.2
## (150, 3)
## <class 'pandas.core.frame.DataFrame'>
## 0.0015039443969726562
## [ 5.02748142  4.73571387  4.83276105  4.79627722  5.09416934  5.32995468
##   4.96908089  4.97250071  4.69244634  4.79968913  5.16895611  4.98557028
##   4.7390479   4.72930067  5.35479204  5.59995925  5.31240281  5.0348059
##   5.25867121  5.2421861   5.00118991  5.18788009  5.08808426  4.96486226
##   5.03920079  4.7609706   5.00021578  5.03701049  4.96225877  4.86543867
##   4.81077704  4.98967371  5.41670896  5.47748792  4.79627722  4.82935651
##   5.02081771  5.09156899  4.72893682  4.97250071  5.02900754  4.59986775
##   4.83276105  5.0999624   5.28886301  4.73974046  5.24290436  4.84014774
##   5.16895611  4.89948204  6.29171381  6.14052008  6.33963545  5.59518138
##   6.07085889  6.08496073  6.24281764  5.13579423  6.17753533  5.61836942
##   5.21179418  5.89598283  5.62878367  6.19998607  5.50034062  6.08668269
##   6.07839915  5.85097543  5.84465931  5.60891455  6.18399633  5.74617055
##   6.13790387  6.23313919  5.99008472  6.05482318  6.22251065  6.28557429
##   6.04559245  5.33893509  5.50007799  5.43319504  5.65699213  6.28057434
##   6.07839915  6.15674178  6.22552705  5.87154703  5.88788015  5.64739823
##   5.98732144  6.17484483  5.69977498  5.11246758  5.85476108  5.98281105
##   5.92251641  5.99008472  4.99995752  5.81803186  6.97918012  6.15696378
##   6.93669716  6.66597325  6.79406571  7.61622237  5.84140648  7.33001711
##   6.66488807  7.10001532  6.28442978  6.30033011  6.51299432  5.98630492
##   5.98077803  6.30856387  6.61650816  7.60004206  7.56834266  6.09822927
##   6.70783629  6.0189936   7.59995932  6.07714296  6.81015631  7.15475405
##   6.05280882  6.17909737  6.53529588  6.91166792  7.09886549  7.4749038
##   6.50040881  6.35366987  6.59917562  7.10283689  6.60440559  6.65242786
##   6.11974352  6.45329028  6.52793286  6.11616648  6.15696378  6.9326115
##   6.64442026  6.16071834  6.02838773  6.29246693  6.45286309  6.30748264]
## (150,)
## <class 'sklearn.svm.classes.SVR'>
## [[ 3.   1.4  0.2]
##  [ 3.2  1.3  0.2]
##  [ 3.1  1.5  0.2]
##  [ 3.4  1.4  0.3]
##  [ 2.9  1.4  0.2]
##  [ 3.1  1.5  0.1]
##  [ 3.7  1.5  0.2]
##  [ 3.4  1.6  0.2]
##  [ 3.   1.1  0.1]
##  [ 4.   1.2  0.2]
##  [ 4.4  1.5  0.4]
##  [ 3.8  1.7  0.3]
##  [ 3.8  1.5  0.3]
##  [ 3.4  1.7  0.2]
##  [ 3.6  1.   0.2]
##  [ 3.3  1.7  0.5]
##  [ 3.4  1.9  0.2]
##  [ 3.   1.6  0.2]
##  [ 3.5  1.5  0.2]
##  [ 3.4  1.4  0.2]
##  [ 3.2  1.6  0.2]
##  [ 3.4  1.5  0.4]
##  [ 4.1  1.5  0.1]
##  [ 3.1  1.5  0.2]
##  [ 3.2  1.2  0.2]
##  [ 3.5  1.3  0.2]
##  [ 3.6  1.4  0.1]
##  [ 3.   1.3  0.2]
##  [ 3.4  1.5  0.2]
##  [ 2.3  1.3  0.3]
##  [ 3.2  1.3  0.2]
##  [ 3.5  1.6  0.6]
##  [ 3.8  1.9  0.4]
##  [ 3.8  1.6  0.2]
##  [ 3.2  1.4  0.2]
##  [ 3.7  1.5  0.2]
##  [ 3.3  1.4  0.2]
##  [ 3.2  4.7  1.4]
##  [ 3.2  4.5  1.5]
##  [ 3.1  4.9  1.5]
##  [ 2.8  4.6  1.5]
##  [ 2.8  4.5  1.3]
##  [ 2.4  3.3  1. ]
##  [ 2.9  4.6  1.3]
##  [ 2.7  3.9  1.4]
##  [ 2.   3.5  1. ]
##  [ 2.2  4.   1. ]
##  [ 2.9  4.7  1.4]
##  [ 2.9  3.6  1.3]
##  [ 3.1  4.4  1.4]
##  [ 3.   4.5  1.5]
##  [ 2.2  4.5  1.5]
##  [ 3.2  4.8  1.8]
##  [ 2.8  4.   1.3]
##  [ 2.5  4.9  1.5]
##  [ 2.8  4.7  1.2]
##  [ 2.9  4.3  1.3]
##  [ 3.   4.4  1.4]
##  [ 2.8  4.8  1.4]
##  [ 3.   5.   1.7]
##  [ 2.6  3.5  1. ]
##  [ 2.7  3.9  1.2]
##  [ 2.7  5.1  1.6]
##  [ 3.   4.5  1.5]
##  [ 3.4  4.5  1.6]
##  [ 3.1  4.7  1.5]
##  [ 2.3  4.4  1.3]
##  [ 3.   4.1  1.3]
##  [ 2.5  4.   1.3]
##  [ 2.6  4.4  1.2]
##  [ 2.6  4.   1.2]
##  [ 2.3  3.3  1. ]
##  [ 2.7  4.2  1.3]
##  [ 3.   4.2  1.2]
##  [ 2.9  4.2  1.3]
##  [ 2.9  4.3  1.3]
##  [ 2.5  3.   1.1]
##  [ 2.8  4.1  1.3]
##  [ 3.3  6.   2.5]
##  [ 2.7  5.1  1.9]
##  [ 3.   5.9  2.1]
##  [ 2.9  5.6  1.8]
##  [ 3.   5.8  2.2]
##  [ 2.5  4.5  1.7]
##  [ 3.6  6.1  2.5]
##  [ 3.2  5.1  2. ]
##  [ 2.7  5.3  1.9]
##  [ 3.   5.5  2.1]
##  [ 2.5  5.   2. ]
##  [ 2.8  5.1  2.4]
##  [ 3.   5.5  1.8]
##  [ 3.8  6.7  2.2]
##  [ 2.6  6.9  2.3]
##  [ 3.2  5.7  2.3]
##  [ 2.8  4.9  2. ]
##  [ 2.8  6.7  2. ]
##  [ 2.7  4.9  1.8]
##  [ 3.3  5.7  2.1]
##  [ 2.8  4.8  1.8]
##  [ 2.8  5.6  2.1]
##  [ 3.   5.8  1.6]
##  [ 2.8  6.1  1.9]
##  [ 3.8  6.4  2. ]
##  [ 2.8  5.6  2.2]
##  [ 2.6  5.6  1.4]
##  [ 3.   6.1  2.3]
##  [ 3.4  5.6  2.4]
##  [ 3.1  5.5  1.8]
##  [ 3.   4.8  1.8]
##  [ 3.1  5.4  2.1]
##  [ 3.1  5.6  2.4]
##  [ 3.1  5.1  2.3]
##  [ 2.7  5.1  1.9]
##  [ 3.2  5.9  2.3]
##  [ 3.   5.2  2.3]
##  [ 2.5  5.   1.9]
##  [ 3.   5.2  2. ]
##  [ 3.4  5.4  2.3]
##  [ 3.   5.1  1.8]]
## [  1   2   3   6   8   9  10  11  13  14  15  18  19  20  22  23  24  25
##   27  28  29  31  32  34  35  36  37  38  39  41  42  43  44  46  47  48
##   49  50  51  52  54  55  57  58  59  60  62  63  64  65  66  68  70  71
##   72  73  74  75  76  77  79  82  83  84  85  86  87  88  89  90  92  93
##   94  95  96  97  98  99 100 101 102 103 104 106 109 110 111 112 113 114
##  116 117 118 120 121 122 123 124 126 128 129 130 131 132 134 135 136 137
##  138 139 140 141 142 143 145 146 147 148 149]
## [1141240072        676]