加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ganzhiji.py 1.66 KB
一键复制 编辑 原始数据 按行查看 历史
geyunfeng369 提交于 2022-01-13 15:37 . 输出图片包含了正确答案
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
import pandas as pd
import random
import time
'''initialize weight'''
ww = [0, 0]
b = 0
positive_x = [3,4]
positive_y = [3,3]
negative_x = [1]
negative_y = [1]
label = [1,1,-1]
label_select = [0,0,0]
data_size = 3
data_x = positive_x + negative_x
data_y = positive_y + negative_y
c_pos = [positive_x,positive_y]
c_pos = np.asarray(c_pos).T
d_neg = [negative_x,negative_y]
d_neg = np.asarray(d_neg).T
data = [data_x,data_y]
data = np.asarray(data).T
for iteration in range(1000):
i = random.randint(0,data_size-1)
print('iteration=', iteration)
print('i=',i)
plt.show()
time.sleep(1)
st = label[i]*(np.matmul(ww,data[i].T+b)+b)
if st <= 0:
ww = ww +label[i] * data[i]
b = b + label[i]
w = ww[0]+0.0001/(ww[1]+0.0001)
fig, ax = plt.subplots()
ax.scatter(positive_x,positive_y,c='b',marker='o')
ax.scatter(negative_x,negative_y,c='r',marker='x')
ax.plot([w*i + b for i in list(range(-5,5,1))])
fig.suptitle('perception')
plt.show()
else:
label_select[i]=1
an = 1
for j in label_select:
an = an & j
print(an)
if an == 1:
break
'''for i in range(len(data_x)):
st = label[i]*(np.matmul(ww,data[i].T+b)+b)
if st <= 0:
ww = ww +label[i] * data[i]
b = b + label[i]
w = ww[0]+0.0001/(ww[1]+0.0001)
fig, ax = plt.subplots()
ax.scatter(positive_x,positive_y,c='b',marker='o')
ax.scatter(negative_x,negative_y,c='r',marker='x')
ax.plot([w*i + b for i in list(range(-5,5,1))])
fig.suptitle('perception')
plt.show()'''
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化