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

分类-KNeighborsClassifier

http://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KNeighborsClassifier.html#sklearn.neighbors.KNeighborsClassifier

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

import time
import numpy as np
import pandas as pd
from sklearn.neighbors import KNeighborsClassifier
# 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()
neigh = KNeighborsClassifier(n_neighbors=3)
neigh.fit(X, y)
t = time.time() - t0

print(t)

print(neigh.predict(X))
print(neigh.predict_proba(X))

print(type(neigh))

# from sklearn.neighbors import KNeighborsClassifier
# X = [[0], [1], [2], [3]]
# y = [0, 0, 1, 1]
# neigh = KNeighborsClassifier(n_neighbors=3)
# neigh.fit(X, y) 
# # KNeighborsClassifier(...)
# print(neigh.predict([[1.1]]))
# print(neigh.predict_proba([[0.9]]))
##    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.0010030269622802734
## ['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' 'virginica' 'versicolor' 'virginica' 'versicolor'
##  'versicolor' 'versicolor' 'versicolor' 'versicolor' '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' 'versicolor' 'virginica' 'virginica' 'virginica' 'virginica'
##  'virginica' 'virginica' 'virginica' 'virginica' 'virginica' 'virginica'
##  'virginica' 'virginica' 'versicolor' 'virginica' 'virginica' 'virginica'
##  'virginica' 'virginica' 'virginica' 'virginica' 'virginica' 'virginica'
##  'virginica' 'virginica' 'virginica' 'virginica' 'versicolor' 'virginica'
##  'virginica' 'virginica' 'virginica' 'virginica' 'virginica' 'virginica'
##  'virginica' 'virginica' 'virginica' 'virginica' 'virginica' 'virginica'
##  'virginica' 'virginica' 'virginica']
## [[ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 1.          0.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          0.33333333  0.66666667]
##  [ 0.          1.          0.        ]
##  [ 0.          0.33333333  0.66666667]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          0.33333333  0.66666667]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          1.          0.        ]
##  [ 0.          0.          1.        ]
##  [ 0.          0.          1.        ]
##  [ 0.          0.          1.        ]
##  [ 0.          0.          1.        ]
##  [ 0.          0.          1.        ]
##  [ 0.          0.          1.        ]
##  [ 0.          0.66666667  0.33333333]
##  [ 0.          0.          1.        ]
##  [ 0.          0.          1.        ]
##  [ 0.          0.          1.        ]
##  [ 0.          0.          1.        ]
##  [ 0.          0.          1.        ]
##  [ 0.          0.          1.        ]
##  [ 0.          0.          1.        ]
##  [ 0.          0.          1.        ]
##  [ 0.          0.          1.        ]
##  [ 0.          0.          1.        ]
##  [ 0.          0.          1.        ]
##  [ 0.          0.          1.        ]
##  [ 0.          0.66666667  0.33333333]
##  [ 0.          0.          1.        ]
##  [ 0.          0.          1.        ]
##  [ 0.          0.          1.        ]
##  [ 0.          0.          1.        ]
##  [ 0.          0.          1.        ]
##  [ 0.          0.          1.        ]
##  [ 0.          0.          1.        ]
##  [ 0.          0.          1.        ]
##  [ 0.          0.          1.        ]
##  [ 0.          0.          1.        ]
##  [ 0.          0.          1.        ]
##  [ 0.          0.          1.        ]
##  [ 0.          0.          1.        ]
##  [ 0.          0.66666667  0.33333333]
##  [ 0.          0.33333333  0.66666667]
##  [ 0.          0.          1.        ]
##  [ 0.          0.          1.        ]
##  [ 0.          0.          1.        ]
##  [ 0.          0.33333333  0.66666667]
##  [ 0.          0.          1.        ]
##  [ 0.          0.          1.        ]
##  [ 0.          0.          1.        ]
##  [ 0.          0.          1.        ]
##  [ 0.          0.          1.        ]
##  [ 0.          0.          1.        ]
##  [ 0.          0.          1.        ]
##  [ 0.          0.          1.        ]
##  [ 0.          0.          1.        ]
##  [ 0.          0.          1.        ]
##  [ 0.          0.          1.        ]]
## <class 'sklearn.neighbors.classification.KNeighborsClassifier'>

回归-KNeighborsRegressor

http://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KNeighborsRegressor.html#sklearn.neighbors.KNeighborsRegressor

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

import time
import numpy as np
import pandas as pd
from sklearn.neighbors import KNeighborsRegressor
# 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()
neigh = KNeighborsRegressor(n_neighbors=3)
neigh.fit(X, y) 
t = time.time() - t0

print(t)

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

print(type(neigh))

# from sklearn.neighbors import KNeighborsRegressor
# X = [[0], [1], [2], [3]]
# y = [0, 0, 1, 1]
# neigh = KNeighborsRegressor(n_neighbors=3)
# neigh.fit(X, y) 
# print(neigh.predict([[1.5]]))
##    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.0005016326904296875
## [ 5.23333333  4.7         4.56666667  4.8         5.          5.4
##   4.96666667  5.1         4.56666667  4.8         5.26666667  5.06666667
##   4.7         4.56666667  5.56666667  5.46666667  5.43333333  5.06666667
##   5.4         5.26666667  5.          5.2         5.03333333  5.16666667
##   5.          4.8         5.16666667  5.1         4.96666667  4.7
##   4.83333333  5.          5.26666667  5.5         4.8         4.7         5.2
##   5.          4.7         5.1         5.2         4.43333333  4.56666667
##   5.13333333  5.4         4.7         5.26666667  4.66666667  5.26666667
##   4.93333333  6.66666667  6.23333333  6.76666667  5.53333333  6.2
##   6.26666667  6.66666667  5.2         6.13333333  5.7         4.96666667
##   5.73333333  5.66666667  6.5         5.53333333  6.56666667  5.66666667
##   5.8         5.8         5.63333333  6.          5.66666667  6.1         6.5
##   6.1         6.3         6.33333333  6.23333333  5.66666667  5.36666667
##   5.53333333  5.53333333  5.9         6.2         5.66666667  6.23333333
##   6.6         6.          5.66666667  5.6         5.6         5.7         5.7
##   4.96666667  5.66666667  5.66666667  6.1         6.1         5.          5.8
##   6.6         6.          7.1         6.4         6.83333333  7.53333333
##   5.86666667  7.3         6.8         6.76666667  6.3         6.          6.7
##   5.93333333  6.46666667  6.43333333  6.4         7.6         7.66666667
##   6.2         6.8         6.03333333  7.66666667  6.1         6.8
##   7.16666667  6.16666667  6.26666667  6.53333333  6.9         7.26666667
##   7.6         6.53333333  6.33333333  6.66666667  7.2         6.4         6.4
##   6.          6.7         6.76666667  6.66666667  6.          6.66666667
##   6.63333333  6.66666667  5.93333333  6.3         6.3         6.23333333]
## (150,)
## <class 'sklearn.neighbors.regression.KNeighborsRegressor'>

other article

https://zhuanlan.zhihu.com/p/23191325

http://blog.csdn.net/luoshixian099/article/details/50923056

http://www.cnblogs.com/jtianwen2014/p/4249003.html

http://www.cnblogs.com/ybjourney/p/4702562.html

http://www.jianshu.com/p/d1c907148f22

http://www.afenxi.com/post/22016

http://www.jianshu.com/p/e3d4ded08c0b