我正在尝试如下实现感知器算法:
import csv
import sys
class perceptron(object):
def __init__(self, infile, outfile, learn_rate = 1, w1 = 0, w2 = 0, b = 0):
self.infile = infile
self.w1 = w1
self.w2 = w2
self.b = b
self.learn_rate = learn_rate
self.outfile = outfile
def main(self):
while True:
lw = self.w1, self.w2, self.b
example_list = self.get_input()
for i in example_list:
result = self.predictor(i)
if result != int(i[2]):
self.train(result, i)
self.write_output()
if lw[0] == self.w1 and lw[1] == self.w2 and lw[2] == self.b:
return self.w1, self.w2, self.b
def train(self, result, i):
self.b = round(self.b + self.learn_rate * (int(i[2]) - result),2)
self.w1 = round(self.w1 + self.learn_rate * (int(i[2]) - result) * int(i[0]), 2)
self.w2 = round(self.w2 + self.learn_rate * (int(i[2]) - result) * int(i[1]), 2)
def predictor(self, example):
activation = (self.w1 * int(example[0])) + (self.w2 * int(example[1])) + self.b
return 1 if activation >= 0 else -1
def write_output(self):
with open(self.outfile, "a", newline='') as w:
writer = csv.writer(w)
text = self.w1, self.w2, self.b
writer.writerow(text)
def get_input(self):
example_list = []
with open(self.infile, "r") as r:
reader = csv.reader(r)
for row in reader:
example_list.append(row)
return example_list
Object = perceptron(sys.argv[0], sys.argv[1])
#Object = perceptron("input1.csv", "output1.csv")
Object.main()
当我尝试使用sysargv参数时,只要通过简单地设置输入和输出文件的名称进行调用,一切似乎都可以正常工作,但是我得到以下输出:
Traceback (most recent call last):
File "c:/Users/xNesTea/python/P3/part1/problem1_3.py", line 51, in <module>
Object.main()
File "c:/Users/xNesTea/python/P3/part1/problem1_3.py", line 18, in main
result = self.predictor(i)
File "c:/Users/xNesTea/python/P3/part1/problem1_3.py", line 32, in predictor
activation = (self.w1 * int(example[0])) + (self.w2 * int(example[1])) + self.b
ValueError: invalid literal for int() with base 10: 'import csv'
我在谷歌上搜索了一下,但似乎找不到正确的解释来说明哪里出了问题以及为什么只有在使用命令行参数时才会发生