人工稚能之sklearn分类

sklearn的naive_bayes算法提供了三种实现，BernoulliNB、MultinormialNB和GaussianNB，BernoulliNB适合抛硬币这种0/1型布尔输入，MultinormialNB适合文章中的单词数量这种数量型输入，而最后一个GaussianNB适合本例中连续性数字输入。

```# -*- coding: utf-8 -*-
import random
import matplotlib.pyplot as plt

k = 5
# 颜色标签
colors = ['green', 'red', 'blue', 'yellow', 'pink']
# 先随机出中心点
centers = []
for i in range(k):
x = 10 + 100 * random.random()
y = 10 + 100 * random.random()
centers.append((x, y))

points = []
# 然后在每个中心点的周围随机100个点
for ci, (x, y) in enumerate(centers):
ps = []
for i in range(100):
px = x + random.random() * 20 - 10
py = y + random.random() * 20 - 10
ps.append((px, py))
points.append(((px, py), ci))
# 显示数据点
plt.scatter(
[x for x, y in ps],
[y for x, y in ps],
c=colors[ci], marker='.')```

```# -*- coding: utf-8 -*-
import random
import matplotlib.pyplot as plt
from sklearn.naive_bayes import GaussianNB

k = 5
# 颜色标签
colors = ['green', 'red', 'blue', 'yellow', 'pink']
# 先随机出中心点centers = []
for i in range(k):
x = 10 + 100 * random.random()
y = 10 + 100 * random.random()
centers.append((x, y))

points = []
# 然后在每个中心点的周围随机100个点
for ci, (x, y) in enumerate(centers):
ps = []
for i in range(100):
px = x + random.random() * 20 - 10
py = y + random.random() * 20 - 10
ps.append((px, py))
points.append(((px, py), ci))
# 显示数据点
plt.scatter(
[x for x, y in ps],
[y for x, y in ps],
c=colors[ci], marker='.')

model = GaussianNB()
# 拟合输入
model.fit([p for p, ci in points], [ci for p, ci in points])

pcolors = []
# 网格点
grids = [(x, y) for x in range(0, 120, 5) for y in range(0, 120, 5)]
# 预测颜色
for i, ci in enumerate(model.predict(grids)):
pcolors.append((grids[i], ci))

# 显示带颜色的网格点
plt.scatter(
[x for (x, y), ci in pcolors],
[y for (x, y), ci in pcolors],
c=[colors[ci] for (x, y), ci in pcolors], marker='x')

plt.show()```

145 篇文章32 人订阅

0 条评论

相关文章

4728

7185

22810

4746

2291

读论文系列：Object Detection ICCV2015 Fast RCNN

Fast RCNN是对RCNN的性能优化版本，在VGG16上，Fast R-CNN训练速度是RCNN的9倍, 测试速度是RCNN213倍；训练速度是SPP-ne...

3786

人脸对齐--Dense Face Alignment

Dense Face Alignment ICCVW2017 http://cvlab.cse.msu.edu/project-pifa.html ...

5397