在另一个Python中调用一个def

2024-09-28 19:03:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用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()

列车_狮子.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

列车_老虎.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

学习率和目标准确度在不同的课程中有所不同,因此我不确定这些是否必须作为参数传递?任何建议都将不胜感激!你知道吗


Tags: 文件importdataratetitlematplotlibdefas
1条回答
网友
1楼 · 发布于 2024-09-28 19:03:55

我可以这样设置:

一个做感知机学习和绘制的类,你用历元、学习率、目标精确度和数据来开始。然后各个模块可以定义它们的特定值并实例化类的实例。你知道吗

下面是实现的基本包装。我返回(weights,accuracity,accuracity\u 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)

下面是一个如何训练的例子_狮子.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()

相关问题 更多 >