Suppose i have a (list | np.array) a of size (N,) and a (list | np.array) l of integers between 0 and N-1.
Is there a way i could write more efficiently sum([a[x] for x in l]) ?
Four different conditions:
a is a numpy array, l is a numpy array
a is a numpy array, l is a list
a is a list, l is a numpy array
a is a list, l is a list
a is a numpy array, l is a numpy array
a is a numpy array, l is a list
For both of the above you can do a[l].sum()
a is a list, l is a numpy array
a is a list, l is a list
For these last two, your options are either to cast a to numpy and then do as above:
np.asarray(a)[l].sum()
or if you are going to use something like your list comprehension, then at least use a generator expression instead - there is no need to build a list simply to add up the values:
sum(a[x] for x in l)
If you are looking for a single solution that you can use regardless of the type, then np.asarray(a)[l].sum() (as suggested above) will work, because if the argument to np.asarray is an array anyway, then it will simply use it as-is -- but be aware that if a is a list then this will need to create an array version of a, so use of the generator expression will be more economical on memory.
import numpy as np
a_list = [10, 11, 12]
l_list = [2, 2]
a_array = np.array(a_list)
l_array = np.array(l_list)
for a in a_list, a_array:
for l in l_list, l_array:
print(np.asarray(a)[l].sum())
gives:
24
24
24
24
Suppose I have 2D array, for simplicity, a=np.array([[1,2],[3,4]]). I want to convert it to list of arrays, so that the result would be:
b=[np.array([1,2]), np.array([3,4])]
I found out that there is np.ndarray.tolist() function, but it converts N-D array into nested list. I could have done in a for loop (using append method), but it is not efficient/elegant.
In my real example I am working with 2D arrays of approximately 10000 x 50 elements and I want list that contains 50 one dimensional arrays, each of the shape (10000,).
How about using list:
a=np.array([[1,2],[3,4]])
b = list(a)
Why don't you use list comprehension as follows without using any append:
a=np.array([[1,2],[3,4]])
b = [i for i in a]
print (b)
Output
[array([1, 2]), array([3, 4])]
I would like to have the index like function :np.searchsorted([1,2,3,4,5], 3) returns 2 but in case 2-D dimension: np.searchsorted([[1,2],[3,4],[5,6]], [5,6]) should return 2. How can I do that?
a = np.array([[1,2],[3,4],[5,6]])
b = np.array([5,6])
np.where(np.all(a==b,axis=1))
The documentation for searchsorted indicates that it only works on 1-D arrays.
You can find the index location in a list using built-in methds:
>>> a = [[1,2],[3,4],[5,6]]
>>> a.index([5,6])
2
>>> a = [[1,2],[5,6],[3,4]]
>>> print(a.index([5,6]))
>>> a.sort()
>>> print(a.index([5,6]))
1
2
I'm trying to do a simple task, but can't figure out a quick solution.
I have two 2D arrays of the same size. One array is a mask array that simply contains either 0s or 1s; I want to retain values in another array wherever the mask array index is 1.
The mask array could look like:
mask = [[0,0,0,0],[0,0,1,1],[0,0,0,1],[0,0,0,1]]
And say I have a second array of:
testarr = [[1,1,1,1],[2,2,2,2],[3,3,3,3],[4,4,4,4]]
I want to extract the values [2,2,3,4] from testarr.
Thanks for your help!
If you convert your mask to bools, you can use numpy to do this. First convert your arrays to numpy arrays using maskl = np.array(mask, dtype=bool) and estarray = np.array(estarray). Then:
>>> estarr[mask]
array([2, 2, 3, 4])
You could use zip.
for mask_list,val_list in zip(arr1, arr2):
for m,v in zip(mask_list, val_list):
if m:
# put in results
You could flatten the lists with itertools.chain and then simply iterate over the zipped lists, e.g. like the following:
mask = [[0,0,0,0],[0,0,1,1],[0,0,0,1],[0,0,0,1]]
testarr = [[1,1,1,1],[2,2,2,2],[3,3,3,3],[4,4,4,4]]
from itertools import chain, izip
print [x for m,x in izip(chain(*mask), chain(*testarr)) if m]
# output:
# [2, 2, 3, 4]
mask = [[0,0,0,0],[0,0,1,1],[0,0,0,1],[0,0,0,1]]
testarr = [[1,1,1,1],[2,2,2,2],[3,3,3,3],[4,4,4,4]]
n = len(mask)
m = len(mask[0])
filtered = []
for i in xrange(n):
for j in xrange(m):
if mask[i][j]:
filtered.append(testarr[i][j])
This is how I'd do it. maybe you can write this as a separate function, and pass in the two lists.
There might be more "elegant" ways, but I don' think there's more "efficient" ways to do it. Personally I like the readability.
You might also check out the numpy.ma module. http://docs.scipy.org/doc/numpy/reference/maskedarray.html
import numpy as np
mask = np.array([[0,0,0,0],[0,0,1,1],[0,0,0,1],[0,0,0,1]],dtype=np.bool)
testarr = np.array([[1,1,1,1],[2,2,2,2],[3,3,3,3],[4,4,4,4]])
ma = np.ma.array(testarr, mask=np.invert(mask))
print ma[~ma.mask]
output
[2 2 3 4]
I have a numpy array and I want to delete the first 3 elements of the array. I tried this solution:
a = np.arange(0,10)
i=0
while(i<3):
del a[0]
i=i+1
This gives me an error that "ValueError: cannot delete array elements". I do not understand why this is the case. i'd appreciate the help thanks!
Numpy arrays have a fixed size, hence you cannot simply delete an element from them. The simplest way to achieve what you want is to use slicing:
a = a[3:]
This will create a new array starting with the 4th element of the original array.
For certain scenarios, slicing is just not enough. If you want to create a subarray consisting of specific elements from the original array, you can use another array to select the indices:
>>> a = arange(10, 20)
>>> a[[1, 4, 5]]
array([11, 14, 15])
So basically, a[[1,4,5]] will return an array that consists of the elements 1,4 and 5 of the original array.
It works for me:
import numpy as np
a = np.delete(a, k)
where "a" is your numpy arrays and k is the index position you want delete.
Hope it helps.
numpy arrays don't support element deletion. Why don't you just use slicing to achieve what you want?
a = a[3:]
You can convert it into a list and then try regular delete commands like pop, del, eg.
a = np.array([1,2,3,4,5])
l = list(a)
l.pop(3)
l
>>[1, 2, 3, 5]