用户:
小萝卜cXsQ查看:0 回复:0 评论:0 创建时间:2022-03-21T19:03:08
【作品展示】

【作品介绍】
要先输入训练数据,然后才能预测;数据越多预测越正确
【作品源代码】
class Neural:
a = []
weight = 0
height = 0
x = 1
y = 1
def __init__(self):
temp = []
for j in range(201):
for i in range(201):
temp.append(0)
self.a.append(temp)
temp = []
for j in range(201):
for i in range(201):
temp.append(1)
self.a.append(temp)
temp = []
def input(self,weight,height,x):
self.weight = weight
self.height = height
self.x = x
def output(self):
return self.y
def train(self):
self.y = self.a[self.weight][self.height]
if self.y != self.x:
self.a[self.weight][self.height] = self.x
def calculate(self,weight,height):
return self.a[weight][height]
class NeuralNetwork:
aiin = []
airun = []
weight = 0
height = 0
def __init__(self):
for i in range(3):
self.aiin.append(Neural())
for i in range(4):
self.airun.append(Neural())
self.aiout=Neural()
def deliver(self,first,second,third):
w = first.weight
h = first.height
second.weight = w
second.height = h
third.weight = w
third.height = h
def train(self,data):
count = len(data)
for i in range(count):
for j in range(3):
self.aiin[i].input(data[i][0],data[i][1],data[i][2])
self.aiin[i].train()
self.deliver(self.aiin[i],self.airun[i],self.airun[i+1])
for j in range(3):
self.airun[i].input(data[i][0],data[i][1],data[i][2])
self.airun[i].train()
self.deliver(self.airun[i],self.aiout,self.aiout)
self.aiout.input(data[i][0],data[i][1],data[i][2])
self.aiout.train()
def input(self,weight,height,x):
for i in range(3):
self.aiin[i].input(weight,height,x)
def output(self):
return self.output.output()
def calculate(self,weight,height):
return self.aiout.calculate(weight,height)
a = NeuralNetwork()
data = []
temp = []
count = int(input("数据数量:"))
for i in range(count):
temp.append(int(input("体重:")))
temp.append(int(input("身高:")))
temp.append(int(input("性别:")))
data.append(temp)
temp = []
a.train(data)
weight = int(input("体重:"))
height = int(input("身高:"))
print("性别:" + str(a.calculate(weight, height)))
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn