enter image description hereI am writing script to realize a perceptron learning algorithm. However, I am having trouble picking up an element randomly in a numpy array. And I don't know whether there is a built-in function in numpy to do that.
def error_rate(w1, w2):
W = error(w1, w2)
return((W.sum())/W.size)
def error(w1, w2):
W = w1!= w2
#print(W)
return W
#test of the function 'error rate'
a = np.array([0,0,0,0,1])
b = np.array([0,1,0,0,1])
print (error_rate(a, b))
print(np.random.choice(np.nonzero(error(a, b)), 1))
In the code above, I actually want to check whether the number in a is the same with the number with the same index in b. And pick up randomly from the index k which satisfies a[k]!=b[k]. But it doesn't work.