Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
99 views
in Technique[技术] by (71.8m points)

python - "Value Error" when passing a list into a function

import random
import numpy as np
import sklearn.model_selection as sk

def XY_Data(Samples):

    Data = []
    for i in range(Samples):
        x = random.uniform(0,100)
        y = random.uniform(0,100)
        Data.append([x, y])

    Y = []
    for x, y in Data:
        if y - x > 0:
            Y.append(1)
        else:
            Y.append(0)

    Data_Train, Data_Test, Y_Train, Y_Test = sk.train_test_split(Data, Y)
    return Data_Train, Data_Test, Y_Train, Y_Test


class Perceptron:
    def __init__(self, XTrain, YTrain, XTest, YTest, lr=0.1, Epochs=10):
        self.X_Train = np.hstack((np.ones((len(XTrain), 1)), XTrain))
        self.Y_Train = YTrain
        self.X_Test =  np.hstack((np.ones((len(XTest), 1)), XTest))
        self.Y_Test =  YTest
        self.Weights = [random.uniform(-1,1) for i in range(np.shape(XTrain)[1]+1)]
        self.lr = lr
        self.Epochs = Epochs

    def ActivationFunction(self, *Sums):
        Evaluations = []
        for Sum in Sums:
            if Sum > 0:
                Evaluations.append(1)
            else:
                Evaluations.append(0)
        return Evaluations

    def Predict(self, X_Train):
        PreActivation = np.dot(self.Weights, X_Train)
        Activation = self.ActivationFunction(PreActivation)
        return Activation

    def Fit(self):
        for _ in range(self.Epochs):
            for i in range(len(self.Y_Train)):
                Y_Estimate = self.Predict(self.X_Train[i])
                Error = np.array(self.Y_Train[i]) - Y_Estimate
                self.Weights += self.lr*Error*self.X_Train[i]

    def Test(self):
        PreActivation = np.dot(self.Weights, self.X_Test.T)
        Y_Estimate = self.ActivationFunction(PreActivation)

    def main():

        XTrain, XTest, Ytrain, YTest = XY_Data(100)
        NN = Perceptron(XTrain, Ytrain, XTest, YTest)
        NN.Fit()
        NN.Test()

    if __name__ == "__main__":
        main()

I'm trying to write a simple Perceptron program, and I'm trying to add a Testing function. Therefore, I'm trying to pass all of the Test Data at once as a list. The Test Data is just X and Y co-ordinates and desired output Y determined by:

def XY_Data(Samples):

    Data = []
    for i in range(Samples):
        x = random.uniform(0,100)
        y = random.uniform(0,100)
        Data.append([x, y])

    Y = []
    for x, y in Data:
        if y - x > 0:
            Y.append(1)
        else:
            Y.append(0)

    Data_Train, Data_Test, Y_Train, Y_Test = sk.train_test_split(Data, Y)
    return Data_Train, Data_Test, Y_Train, Y_Test

The error is found when I execute:

Y_Estimate = self.ActivationFunction(PreActivation)

Then line 18, in ActivationFunction

if Sum > 0: ValueError: The truth value of an array with more than one element is ambiguous

The *Sums output is similar to, (array([ -322.08170851, 703.01418603,...]),)

It had no issues when only a single value was passed into ActivationFunction. I'm not sure what is flagging the error when I'm trying to pass a list.

Any other issues to be noted would be appreciated.

question from:https://stackoverflow.com/questions/65947202/value-error-when-passing-a-list-into-a-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...