我正在尝试用Python训练感知器,该感知器具有3种不同的猫类:老虎,狮子和猎豹。为了做到这一点,我希望创建一个感知器精确度进度图。最初,我创建了3个python文件,每个文件的目的是训练每个类的感知器。下面的代码对于每个文件都是通用的-在python中,有没有一种方法可以合并这三个文件并将下面的代码实现为def?
公用代码:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import Perceptron as nn
def commonCode(!WHAT PARAMETERS SHOULD GO HERE?!):
理想情况下,我希望在此处调用Lion和Tiger函数(见下文),但是我不确定需要调用哪些参数,也不确定如何实现此功能。
(weigths, accuracy, accuracy_progression) = nn.perceptronLearning(data,epochs,learning_rate, target_accuracy)
(tp,tn,fp,fn) = p.confusionMatrix(weigths,data)
print('weigths: ', weigths)
print('accuracy: ', accuracy)
print('true positive: %d true negative: %d',(tp,tn))
print('false positive: %d false negative: %d',(fp,fn))
title = "%d_iterations_lambda=%f" %(len(accuracy_progression),learning_rate)
path = "./Plots/%s.png" %(title)
plt.title(title)
plt.ylabel('accuracy (%)')
plt.xlabel('iteration')
plt.plot(accuracy_progression)
plt.show()
train_Lion.py文件:
def Lion (cat):
if cat == b'Cat-lion':
return 1
else:
return 0
filename = 'cat.data'
data = np.loadtxt(filename,delimiter=',',converters={4:lion})
np.random.shuffle(data)
epochs = 30
learning_rate = 0.1
target_accuracy = 100
train_Tiger.py文件:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import Perceptron as nn
def Tiger (cat):
if cat == b'Cat-tiger':
return 1
else:
return 0
filename = 'cat.data'
data = np.loadtxt(filename,delimiter=',',converters={4:tiger})
np.random.shuffle(data)
epochs = 30
learning_rate = 0.2
target_accuracy = 95
等学习率和目标准确性在各班之间有所不同,因此我不确定是否必须将这些作为参数传递?任何建议将不胜感激!
答案 0 :(得分:0)
我可以这样设置:
一个类,用于执行您使用纪元,learning_rate,target_accuracy和数据启动的感知器学习和绘图。然后,各个模块可以定义其特定值并实例化该类的实例。
这是您的实现的基本包装。我返回了(权重,准确性,precision_progression)元组,以便您可以根据需要在各个模块中对其进行进一步处理。您当然可以进一步重构:
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import Perceptron as nn
class PPlotter:
def __init__(self, epochs, learning_rate, target_accuracy, data):
self.epochs = epochs
self.learning_rate = learning_rate
self.target_accuracy = target_accuracy
self.data = data
def plot_accuracy_progression(self):
(weights, accuracy, accuracy_progression) = nn.perceptronLearning(self.data, self.epochs, self.learning_rate, self.target_accuracy)
(tp,tn,fp,fn) = p.confusionMatrix(weigths,self.data)
print('weights: ', weights)
print('accuracy: ', accuracy)
print('true positive: %d true negative: %d',(tp,tn))
print('false positive: %d false negative: %d',(fp,fn))
title = "%d_iterations_lambda=%f" % (len(accuracy_progression),learning_rate)
path = "./Plots/%s.png" %(title)
plt.title(title)
plt.ylabel('accuracy (%)')
plt.xlabel('iteration')
plt.plot(accuracy_progression)
plt.show()
return (weights, accuracy, accuracy_progression)
然后是一个train_Lion.py外观的示例:
import numpy as np
import PPlotter
def Lion (cat):
if cat == b'Cat-lion':
return 1
else:
return 0
filename = 'cat.data'
data = np.loadtxt(filename,delimiter=',',converters={4:lion})
np.random.shuffle(data)
epochs = 30
learning_rate = 0.1
target_accuracy = 100
plotter = PPlotter(epochs, learning_rate, target_accuracy, data)
(weights, accuracy, accuracy_progression) = plotter.plot_accuracy_progression()