Python 利用 sklearn 实现 knn
Python 利用 sklearn 实现 knn
Python
# 导入数据集生成器
from sklearn.datasets import make_blobs
# 导入 <span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/knn" title="View all posts in knn" target="_blank">knn</a></span> 分类器
from sklearn.neighbors import KNeighborsClassifier
# 导入画图工具
import matplotlib.pyplot as plt
# 导入数据集拆分
from sklearn.model_selection import train_test_split
1
2
3
4
5
6
7
8
9
|
# 导入数据集生成器
from sklearn.datasets import make_blobs
# 导入 knn 分类器
from sklearn.neighbors import KNeighborsClassifier
# 导入画图工具
import matplotlib.pyplot as plt
# 导入数据集拆分
from sklearn.model_selection import train_test_split
|
Python
make_blobs??
# 在学习机器学习的过程中,常常遇到random_state这个参数,下面来简单叙述一下它的作用。
# 作用:控制随机状态。
1
2
3
4
5
|
make_blobs??
# 在学习机器学习的过程中,常常遇到random_state这个参数,下面来简单叙述一下它的作用。
# 作用:控制随机状态。
|
Python
datas= make_blobs(n_samples=200,centers=2,random_state=8)
1
2
|
datas= make_blobs(n_samples=200,centers=2,random_state=8)
|
Python
from pprint import pprint
pprint(datas)
1
2
3
|
from pprint import pprint
pprint(datas)
|
Python
(array([[ 6.75445054, 9.74531933],
[ 6.80526026, -0.2909292 ],
[ 7.07978644, 7.81427747],
[ 6.87472003, -0.16069949],
[ 8.20316159, 12.01375618],
[ 6.97321804, 2.576281 ],
[ 6.42049196, 0.26683712],
[ 7.40783871, 6.93633083],
[ 6.54464509, 0.89987351],
[ 7.58423725, 10.70124388],
[ 8.80002143, 8.54323521],
[ 7.1847723 , 2.22950427],
[ 7.80361128, 9.74561264],
[ 7.96481592, 8.03914659],
[ 6.6571269 , 7.72756233],
[ 7.29433984, 9.79486468],
[ 7.237824 , 1.70291874],
[ 8.37153676, 0.98810496],
[ 6.49932355, 0.24955722],
[ 9.02255525, 10.06777901],
[ 7.61227907, 9.4463627 ],
[ 8.89464606, 10.29806397],
[ 7.01747287, -1.22016798],
[ 8.10434971, 1.83659293],
[ 7.68373899, 1.5632695 ],
[ 9.43042008, 0.68726533],
[ 6.26211747, 1.577057 ],
[ 9.59017028, 0.58441955],
[ 7.82182216, 0.52633087],
[ 7.6025272 , 8.98962387],
[ 8.48011698, 0.69122126],
[ 7.63890536, -0.06731493],
[ 5.84965451, 0.72241791],
[ 7.46996922, 8.44935323],
[ 6.8117005 , 10.8840413 ],
[ 8.67502392, 0.37561206],
[ 8.12519495, 1.67159478],
[ 5.07337492, 10.52482973],
[ 7.48665378, 0.21345453],
[ 8.11950967, 0.56120493],
[ 6.15895483, 8.70208685],
[ 7.94310647, 8.20622208],
[ 7.95311372, 8.36897664],
[ 4.96938735, 1.32531048],
[ 8.8583269 , -0.34648253],
[10.01367527, 10.52089453],
[ 8.99334153, 9.7313491 ],
[ 8.22871505, 1.23014656],
[ 6.19407512, -0.03183561],
[ 7.26697254, 9.87045836],
[ 7.94970781, -0.37340645],
[ 5.62803952, 9.77585443],
[ 8.50049461, 9.12147855],
[ 7.31054144, 0.39102866],
[ 7.49814373, 9.29677019],
[ 8.32245091, 9.67819196],
[ 8.32813617, 9.14002426],
[ 7.56475962, 11.24762868],
[ 7.92129785, 0.78018447],
[ 8.00236864, 10.1691733 ],
[ 4.33366829, 10.51034676],
[ 6.02937898, 10.31974057],
[ 6.88953097, 0.80526874],
[ 7.51239046, 2.06597042],
[ 9.17061801, 10.37690696],
[ 7.63027116, 8.69797933],
[ 8.35312192, 0.20325714],
[ 8.72578696, 10.34691678],
[ 5.44099009, 1.59585563],
[ 7.56093115, -0.51702689],
[ 6.02376341, -0.52025947],
),
array([0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1,
1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0,
1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0,
1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1,
0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1,
0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1,
0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0,
0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1,
0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0,
0, 1]))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
(array([[ 6.75445054, 9.74531933],
[ 6.80526026, -0.2909292 ],
[ 7.07978644, 7.81427747],
[ 6.87472003, -0.16069949],
[ 8.20316159, 12.01375618],
[ 6.97321804, 2.576281 ],
[ 6.42049196, 0.26683712],
[ 7.40783871, 6.93633083],
[ 6.54464509, 0.89987351],
[ 7.58423725, 10.70124388],
[ 8.80002143, 8.54323521],
[ 7.1847723 , 2.22950427],
[ 7.80361128, 9.74561264],
[ 7.96481592, 8.03914659],
[ 6.6571269 , 7.72756233],
[ 7.29433984, 9.79486468],
[ 7.237824 , 1.70291874],
[ 8.37153676, 0.98810496],
[ 6.49932355, 0.24955722],
[ 9.02255525, 10.06777901],
[ 7.61227907, 9.4463627 ],
[ 8.89464606, 10.29806397],
[ 7.01747287, -1.22016798],
[ 8.10434971, 1.83659293],
[ 7.68373899, 1.5632695 ],
[ 9.43042008, 0.68726533],
[ 6.26211747, 1.577057 ],
[ 9.59017028, 0.58441955],
[ 7.82182216, 0.52633087],
[ 7.6025272 , 8.98962387],
[ 8.48011698, 0.69122126],
[ 7.63890536, -0.06731493],
[ 5.84965451, 0.72241791],
[ 7.46996922, 8.44935323],
[ 6.8117005 , 10.8840413 ],
[ 8.67502392, 0.37561206],
[ 8.12519495, 1.67159478],
[ 5.07337492, 10.52482973],
[ 7.48665378, 0.21345453],
[ 8.11950967, 0.56120493],
[ 6.15895483, 8.70208685],
[ 7.94310647, 8.20622208],
[ 7.95311372, 8.36897664],
[ 4.96938735, 1.32531048],
[ 8.8583269 , -0.34648253],
[10.01367527, 10.52089453],
[ 8.99334153, 9.7313491 ],
[ 8.22871505, 1.23014656],
[ 6.19407512, -0.03183561],
[ 7.26697254, 9.87045836],
[ 7.94970781, -0.37340645],
[ 5.62803952, 9.77585443],
[ 8.50049461, 9.12147855],
[ 7.31054144, 0.39102866],
[ 7.49814373, 9.29677019],
[ 8.32245091, 9.67819196],
[ 8.32813617, 9.14002426],
[ 7.56475962, 11.24762868],
[ 7.92129785, 0.78018447],
[ 8.00236864, 10.1691733 ],
[ 4.33366829, 10.51034676],
[ 6.02937898, 10.31974057],
[ 6.88953097, 0.80526874],
[ 7.51239046, 2.06597042],
[ 9.17061801, 10.37690696],
[ 7.63027116, 8.69797933],
[ 8.35312192, 0.20325714],
[ 8.72578696, 10.34691678],
[ 5.44099009, 1.59585563],
[ 7.56093115, -0.51702689],
[ 6.02376341, -0.52025947],
),
array([0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1,
1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0,
1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0,
1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1,
0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1,
0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1,
0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0,
0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1,
0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0,
0, 1]))
|
Python
x,y = datas
1
2
|
x,y = datas
|
Python
plt.scatter(x[:,0],x[:,1],c=y,cmap=plt.cm.spring,edgecolors='k')
1
2
|
plt.scatter(x[:,0],x[:,1],c=y,cmap=plt.cm.spring,edgecolors='k')
|
Python
x[:,0]
1
2
|
x[:,0]
|
Python
array([ 6.75445054, 6.80526026, 7.07978644, 6.87472003, 8.06164078,
7.4934131 , 4.69777002, 9.19642422, 8.80996213, 7.5952749 ,
8.20330317, 8.59258191, 6.89228905, 8.00405631, 8.14715032,
7.06363179, 6.34526126, 5.28435774, 6.62257531, 7.40314915,
7.27423265, 8.77188508, 6.39995999, 7.44636985, 7.74488453,
9.10088858, 8.10044749, 8.73747674, 6.51876894, 7.16251356,
6.57119411, 7.1354011 , 7.31294296, 7.52733204, 6.0160163 ,
6.73117031, 6.11962018, 7.88579276, 7.32112244, 7.62051584,
6.96767867, 8.51730001, 7.92672195, 5.52161775, 6.93568163,
7.89765814, 7.40292703, 8.28827095, 7.33912656, 5.27801757,
5.57550594, 8.67425268, 7.55303352, 6.84661976, 6.26977193,
7.09962807, 5.5987887 , 8.0060449 , 6.85769503, 6.19399963,
8.68173394, 5.82259795, 5.30528133, 6.89703841, 5.9389756 ,
7.13760133, 7.51718983, 8.08034605, 6.89078889, 6.95802459,
8.91111219, 7.57818277, 6.24007751, 7.79924692, 7.49985237,
9.94109903, 7.07232613, 7.50126258, 6.63110319, 6.6060513 ,
8.81545663, 6.5688005 , 9.15668309, 7.45637594, 7.29548244,
8.20316159, 6.97321804, 6.42049196, 7.40783871, 6.54464509,
7.58423725, 8.80002143, 7.1847723 , 7.80361128, 7.96481592,
6.6571269 , 7.29433984, 7.237824 , 8.37153676, 6.49932355,
9.02255525, 7.61227907, 8.89464606, 7.01747287, 8.10434971,
7.68373899, 9.43042008, 6.26211747, 9.59017028, 7.82182216,
7.6025272 , 8.48011698, 7.63890536, 5.84965451, 7.46996922,
6.8117005 , 8.67502392, 8.12519495, 5.07337492, 7.48665378,
8.11950967, 6.15895483, 7.94310647, 7.95311372, 4.96938735,
8.8583269 , 10.01367527, 8.99334153, 8.22871505, 6.19407512,
7.26697254, 7.94970781, 5.62803952, 8.50049461, 7.31054144,
7.49814373, 8.32245091, 8.32813617, 7.56475962, 7.92129785,
8.00236864, 4.33366829, 6.02937898, 6.88953097, 7.51239046,
9.17061801, 7.63027116, 8.35312192, 8.72578696, 5.44099009,
7.56093115, 6.02376341, 7.15013321, 7.56833386, 7.09022949,
5.94356564, 6.25817082, 5.94205586, 7.82510107, 5.88994248,
6.40269472, 7.64534862, 6.8830708 , 7.24044576, 9.4035308 ,
6.55819206, 6.58341965, 7.83939881, 7.22095192, 7.8440213 ,
7.39634594, 9.10772988, 6.93540782, 7.9465776 , 7.92430026,
6.79156708, 6.28516091, 7.54257819, 7.40565933, 7.51463404,
6.40863862, 6.5342397 , 5.17209648, 5.49953213, 9.86936252,
7.84725158, 8.14330144, 7.28724996, 6.0888764 , 7.59635095,
6.71388804, 7.3307687 , 8.18240421, 8.53178848, 6.91511696,
7.82944816, 6.09382282, 7.24211001, 8.2634157 , 8.39800148])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
array([ 6.75445054, 6.80526026, 7.07978644, 6.87472003, 8.06164078,
7.4934131 , 4.69777002, 9.19642422, 8.80996213, 7.5952749 ,
8.20330317, 8.59258191, 6.89228905, 8.00405631, 8.14715032,
7.06363179, 6.34526126, 5.28435774, 6.62257531, 7.40314915,
7.27423265, 8.77188508, 6.39995999, 7.44636985, 7.74488453,
9.10088858, 8.10044749, 8.73747674, 6.51876894, 7.16251356,
6.57119411, 7.1354011 , 7.31294296, 7.52733204, 6.0160163 ,
6.73117031, 6.11962018, 7.88579276, 7.32112244, 7.62051584,
6.96767867, 8.51730001, 7.92672195, 5.52161775, 6.93568163,
7.89765814, 7.40292703, 8.28827095, 7.33912656, 5.27801757,
5.57550594, 8.67425268, 7.55303352, 6.84661976, 6.26977193,
7.09962807, 5.5987887 , 8.0060449 , 6.85769503, 6.19399963,
8.68173394, 5.82259795, 5.30528133, 6.89703841, 5.9389756 ,
7.13760133, 7.51718983, 8.08034605, 6.89078889, 6.95802459,
8.91111219, 7.57818277, 6.24007751, 7.79924692, 7.49985237,
9.94109903, 7.07232613, 7.50126258, 6.63110319, 6.6060513 ,
8.81545663, 6.5688005 , 9.15668309, 7.45637594, 7.29548244,
8.20316159, 6.97321804, 6.42049196, 7.40783871, 6.54464509,
7.58423725, 8.80002143, 7.1847723 , 7.80361128, 7.96481592,
6.6571269 , 7.29433984, 7.237824 , 8.37153676, 6.49932355,
9.02255525, 7.61227907, 8.89464606, 7.01747287, 8.10434971,
7.68373899, 9.43042008, 6.26211747, 9.59017028, 7.82182216,
7.6025272 , 8.48011698, 7.63890536, 5.84965451, 7.46996922,
6.8117005 , 8.67502392, 8.12519495, 5.07337492, 7.48665378,
8.11950967, 6.15895483, 7.94310647, 7.95311372, 4.96938735,
8.8583269 , 10.01367527, 8.99334153, 8.22871505, 6.19407512,
7.26697254, 7.94970781, 5.62803952, 8.50049461, 7.31054144,
7.49814373, 8.32245091, 8.32813617, 7.56475962, 7.92129785,
8.00236864, 4.33366829, 6.02937898, 6.88953097, 7.51239046,
9.17061801, 7.63027116, 8.35312192, 8.72578696, 5.44099009,
7.56093115, 6.02376341, 7.15013321, 7.56833386, 7.09022949,
5.94356564, 6.25817082, 5.94205586, 7.82510107, 5.88994248,
6.40269472, 7.64534862, 6.8830708 , 7.24044576, 9.4035308 ,
6.55819206, 6.58341965, 7.83939881, 7.22095192, 7.8440213 ,
7.39634594, 9.10772988, 6.93540782, 7.9465776 , 7.92430026,
6.79156708, 6.28516091, 7.54257819, 7.40565933, 7.51463404,
6.40863862, 6.5342397 , 5.17209648, 5.49953213, 9.86936252,
7.84725158, 8.14330144, 7.28724996, 6.0888764 , 7.59635095,
6.71388804, 7.3307687 , 8.18240421, 8.53178848, 6.91511696,
7.82944816, 6.09382282, 7.24211001, 8.2634157 , 8.39800148])
|
Python
import numpy as np
1
2
|
import numpy as np
|
Python
a = np.zeros((2,3))
1
2
|
a = np.zeros((2,3))
|
Python
a[0]
1
2
|
a[0]
|
Python
array([0., 0., 0.])
1
2
|
array([0., 0., 0.])
|
Python
a[0] = range(1,4)
1
2
|
a[0] = range(1,4)
|
Python
a
1
2
|
a
|
Python
array([[1., 2., 3.],
[0., 0., 0.]])
1
2
3
|
array([[1., 2., 3.],
[0., 0., 0.]])
|
Python
# 分片操作,选取二维数组的第一列,第二列
1
2
|
# 分片操作,选取二维数组的第一列,第二列
|
Python
a[:,0]
1
2
|
a[:,0]
|
Python
array([1., 0.])
1
2
|
array([1., 0.])
|
Python
a[:,1]
1
2
|
a[:,1]
|
Python
array([2., 0.])
1
2
|
array([2., 0.])
|
Python
# 获取第一行 第二行
1
2
|
# 获取第一行 第二行
|
Python
a[0]
1
2
|
a[0]
|
Python
array([1., 2., 3.])
1
2
|
array([1., 2., 3.])
|
Python
a[1]
1
2
|
a[1]
|
Python
array([0., 0., 0.])
1
2
|
array([0., 0., 0.])
|
Python
<br />
1 |
<br />
|
-
zeropython 微信公众号
5868037 QQ号
[email protected] QQ邮箱