Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficent way of constructing a matrix with all elements zero except one in numpy

I want to compute the output error for a neural network for each input by compare output signal and its true output value so I need two matrix to compute this task.

I have output matrix in shape of (n*1) but in the label I just have the index of neuron that should be activated, so I need a matrix in the same shape with all element equal to zero except the one which it's index is equal to the label. I could do that with a function but I wonder is there a built in method in numpy python that can do that for me?

like image 309
MJ Sameri Avatar asked Apr 11 '17 07:04

MJ Sameri


People also ask

How do you create a NumPy array with a given shape containing all zeros?

The zeros() function is used to get a new array of given shape and type, filled with zeros. Shape of the new array, e.g., (2, 3) or 2. The desired data-type for the array, e.g., numpy. int8.

How do you make a matrix with zeros in Python?

In order to create a zero matrix using Python and NumPy, we can use the Numpy . zeros() function.

Which is the method used in NumPy to print a NumPy array with zeros?

zeros() function is one of the most significant functions which is used in machine learning programs widely. This function is used to generate an array containing zeros. The numpy. zeros() function provide a new array of given shape and type, which is filled with zeros.

How do you get nonzero elements in NumPy array?

nonzero() function is used to Compute the indices of the elements that are non-zero. It returns a tuple of arrays, one for each dimension of arr, containing the indices of the non-zero elements in that dimension. The corresponding non-zero values in the array can be obtained with arr[nonzero(arr)] .


1 Answers

You can do that multiple ways using numpy or standard libraries, one way is to create an array of zeros, and set the value corresponding to index as 1.

n = len(result)

a = np.zeros((n,)); 
a[id] = 1

It probably is going to be the fastest one as well:

>> %timeit a = np.zeros((n,)); a[id] = 1
1000000 loops, best of 3: 634 ns per loop

Alternatively you can use numpy.pad to pad [ 1 ] array with zeros. But this will almost definitely will be slower due to padding logic.

np.lib.pad([1],(id,n-id),'constant', constant_values=(0))

As expected order of magnitude slower:

>> %timeit np.lib.pad([1],(id,n-id),'constant', constant_values=(0))
10000 loops, best of 3: 47.4 µs per loop

And you can try list comprehension as suggested by the comments:

results = [7]

np.matrix([1 if x == id else 0 for x in results])

But it is much slower than the first method as well:

>> %timeit np.matrix([1 if x == id else 0 for x in results])
100000 loops, best of 3: 7.25 µs per loop

Edit: But in my opinion, if you want to compute the neural networks error. You should just use np.argmax and compute whether it was successful or not. That error calculation may give you more noise than it is useful. You can make a confusion matrix if you feel your network is prone to similarities.

like image 51
umutto Avatar answered Sep 21 '22 20:09

umutto