In: Computer Science
I need the following problem to be coded in python without using Numpy:
Given input features x1, x2,..., xi and corresponding weights w0, w1, w2,...wi. Write a function, called 'Perceptron', that returns the output value yi. For your function, use the following perceptron activation function:
g(X,W)={1 if wTx>0;0 otherwise}
The inputs X and W will be arrays of the input feature and weight values, respectively. All values will be integers. Your function will return gp(X,W), which is a single integer value:
def perceptron(X, W):
Code is very simple. We have to first import module array in python and then we will perform the task.
Below is the screenshot of python code.
Below is the screenshot of
output
Below is the same code in text format.
import array
def perceptron(X, W):
result = 0 #This will store the
value of W^T*X
for i in range(0,len(X)):
result =result + X[i]*W[i]
#storing value of W^T*X
if result > 0: #if
result > 0 then return 1 else return 0
return 1
else :
return 0
X=array.array('i',[3,4,-5,3]) #defining array X of type
inereger
W=array.array('i',[1,1,3,-3]) #defining array W of type inereger
Y = perceptron(X, W) #storing the output in Y
print("The result is " + str(Y)) #printing the result
Please comment for any clarification.