pythonalgorithmperceptron

Perceptron Algorithm Confusion


I have this problem that I'm trying to solve:

def phi(x):
    # DO NOTHING ON THIS FUNCTION
    if x<=0:
        return -1.0
    else:
        return 1.0
    
phi = np.vectorize(phi)

This is where I need to implement the function:

def predictOne(x, w):
    z =  
    return phi(z)

But when I try to use my formula but with my input code:

def predictOne(x, w):
    z =  0 + x * w
    return phi(z)

And run my asserts:

assert predictOne(np.array([0.0,0.0]) , np.array([0.1,3.2,7.4])) == 1.0
assert predictOne(np.array([0.0,0.0]), np.array([-0.1,3.2,7.4])) == -1.0
assert predictOne(np.array([0.3,-0.7]), np.array([0.1,3.2,7.4])) == -1.0
assert predictOne(np.array([0.3,0.7]), np.array([0.1,3.2,7.4])) == 1.0

I get an (operands could not be broadcast together with shapes (2,) (3,)

Apparently, the assertions are correct so I'm doing somethin wrong in my predictOne function. Can anyone help?


Solution

  • The problem is in the formula:

    def predictOne(x, w):
        z = w[0] + np.sum(x * w[1:])
        return phi(z)