I have an array with leading zeros, and I want the array from the first nonzero element.
For example, I have an array
x=[0,0,0,0,0,3,2,0,0,4,5]
I want to obtain :
x=[3,2,0,0,4,5]
What you have is a list, not an array. This is a pure-Python solution, though you might consider converting your list into a NumPy array and going with the Divakar's solution for better performance (if and only if you plan on using NumPy for downstream processing)
In [1]: from itertools import dropwhile
In [2]: from operator import not_
In [3]: x = [0,0,0,0,0,3,2,0,0,4,5]
In [4]: list(dropwhile(not_, x))
Out[4]: [3, 2, 0, 0, 4, 5]
Use np.argmax on non-zeros to get the starting non-zero index and then simply slice it -
x[(x!=0).argmax():]
Sample run -
In [71]: x
Out[71]: array([0, 0, 0, 0, 0, 3, 2, 0, 0, 4, 5])
In [72]: x[(x!=0).argmax():]
Out[72]: array([3, 2, 0, 0, 4, 5])
Related
I have a 2D numpy array:
[[1,2,3,4,5],[2,4,5,6,7],[0,9,3,2,4]]
I also have a second 1D array:
[2,3,4]
I want to replace all occurences of the elements of the second array with 0
So eventually, my second array should look like
[[1,0,0,0,5],[0,0,5,6,7],[0,9,0,0,0]]
is there a way in python/numpy I can do this without using a loop.
I already checked at np.where, but the condition there is only for example where element = 1 value, and not multiple.
Thanks a lot !
Use numpy.isin.
>>> import numpy as np
>>> a = np.array([[1,2,3,4,5],[2,4,5,6,7],[0,9,3,2,4]])
>>> b = np.array([2,3,4])
>>> a[np.isin(a, b)] = 0
>>> a
array([[1, 0, 0, 0, 5],
[0, 0, 5, 6, 7],
[0, 9, 0, 0, 0]])
Suppose I have a numpy array
a = np.array([0,2,3,4,5,1,9,0,0,7,9,0,0,0]).reshape(7,2)
I want to find out the indices of all the times the minimum element (here 0) occurs in the 2nd column. Using argmin I can find out the index of when 0 is occurring for the first time. How can I do this in Python?
Using np.flatnonzero on a[:, 1]==np.min(a) is the most starightforward way:
In [3]: idxs = np.flatnonzero(a[:, 1]==np.min(a))
In [4]: idxs
Out[4]: array([3, 5, 6])
After you reshaped your array it looks like this:
array([[0, 2],
[3, 4],
[5, 1],
[9, 0],
[0, 7],
[9, 0],
[0, 0]])
You can get all elements that are of the same value by using np.where. IN your case the following would work:
np.where(a.T[-1] == a.argmin())
# This would give you (array([3, 5, 6]),)
What happens here is that you create a transposed view on the array. This means you can easily access the columns. The term view here means that the a array itself is not changed for that. This leaves you with:
a.T
array([[0, 3, 5, 9, 0, 9, 0],
[2, 4, 1, 0, 7, 0, 0]])
From this you select the last line (i.e. the last column of a) by using the index -1. Now you have the array
array([2, 4, 1, 0, 7, 0, 0])
on which you can call np.where(condititon), which gives you all indices for which the condition is true. In your case the condition is
a.T[-1] == a.argmin()
which gives you all entries in the selected line of the transposed array that have the same value as np.argmin(a) which, as you said, is 0 in your case.
I have a numpy array and I want a function that takes as an input the numpy array and a list of indices and returns as an output another array which has the following property: a zero has been added to the initial array just before the position of each of the indices of the origional array.
Let me give a couple of examples:
If indices = [1] and the initial array is array([1, 1, 2]), then the output of the function should be array([1, 0, 1, 2]).
If indices = [0, 1, 3] and the initial array is array([1, 2, 3, 4]), then the output of the function should be array([0, 1, 0, 2, 3, 0, 4]).
I would like to do it in a vectorized manner without any for loops.
Had the same issue before. Found a solution using np.insert:
import numpy as np
np.insert([1, 1, 2], [1], 0)
>>> [1, 0, 1, 2]
I see #jdehesa has commented this already but adding as a permanent answer for future visitors.
import numpy as np
a=np.array([2,3,4,6,7,4,5,3,2,1,9,8,7,6,4,2])
## a has 16 elements
b=np.array([1,5,3,7])
""" i want this:
a[:4] - b[1], a[4:8]-b[2], a[8:12]-b[3], a[12:16]-b[4] in one array
and it should look like
c=numpy.array[1,2,3,5,2,-1,0,-2....]
This is just a simple example for my problem. So can not be done with using groups. I need a loop to solve this.
You can reshape your array a, make operations on that and than flatten back.
In [24]: import numpy as np
In [25]: a=np.array([2,3,4,6,7,4,5,3,2,1,9,8,7,6,4,2])
In [26]: b=np.array([1,5,3,7])
In [27]: (a.reshape(4,4).T - b).T.ravel()
Out[27]: array([ 1, 2, 3, 5, 2, -1, 0, -2, -1, -2, 6, 5, 0, -1, -3, -5])
I have the following arrays
>>> a
array([0, 8, 0, 8, 0, 8])
>>> b
array([0, 6, 0, 6, 0, 6])
these represent the real and imaginary parts of a set of complex numbers.
I can reformat them into numpy.complex datatype using the following
>>> [x for x in itertools.imap(complex,a,b)]
[0j, (8+6j), 0j, (8+6j), 0j, (8+6j)]
However what I really want to get is only the elements with odd indexes:
[(8+6j),(8+6j),(8+6j)]
Is there an easy way to achieve this?
>>> import numpy as np
>>> a = np.array([0, 8, 0, 8, 0, 8])
>>> b = np.array([0, 6, 0, 6, 0, 6])
>>> np.vectorize(complex)(a, b)[1::2]
array([ 8.+6.j, 8.+6.j, 8.+6.j])
That would be the best way but just for completeness, the itertools solution would be:
>>> from itertools import imap, islice
>>> list(islice(imap(complex,a,b), 1, None, 2))
[(8+6j), (8+6j), (8+6j)]
[1::2] takes every other item, starting on index 1 (the second), to the end of the list. That is:
>>> [x for x in itertools.imap(complex,a,b)][1::2]
[(8+6j), (8+6j), (8+6j)]