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
Related
I want to create an array with dtype=np.object, where each element is an array with a numerical type, e.g int or float. For example:
>>> a = np.array([1,2,3])
>>> b = np.empty(3,dtype=np.object)
>>> b[0] = a
>>> b[1] = a
>>> b[2] = a
Creates what I want:
>>> print b.dtype
object
>>> print b.shape
(3,)
>>> print b[0].dtype
int64
but I am wondering whether there isn't a way to write lines 3 to 6 in one line (especially since I might want to concatenate 100 arrays). I tried
>>> b = np.array([a,a,a],dtype=np.object)
but this actually converts all the elements to np.object:
>>> print b.dtype
object
>>> print b.shape
(3,)
>>> print b[0].dtype
object
Does anyone have any ideas how to avoid this?
It's not exactly pretty, but...
import numpy as np
a = np.array([1,2,3])
b = np.array([None, a, a, a])[1:]
print b.dtype, b[0].dtype, b[1].dtype
# object int32 int32
a = np.array([1,2,3])
b = np.empty(3, dtype='O')
b[:] = [a] * 3
should suffice.
I can't find any elegant solution, but at least a more general solution to doing everything by hand is to declare a function of the form:
def object_array(*args):
array = np.empty(len(args), dtype=np.object)
for i in range(len(args)):
array[i] = args[i]
return array
I can then do:
a = np.array([1,2,3])
b = object_array(a,a,a)
I then get:
>>> a = np.array([1,2,3])
>>> b = object_array(a,a,a)
>>> print b.dtype
object
>>> print b.shape
(3,)
>>> print b[0].dtype
int64
I think anyarray is what you need here:
b = np.asanyarray([a,a,a])
>>> b[0].dtype
dtype('int32')
not sure what happened to the other 32bits of the ints though.
Not sure if it helps but if you add another array of a different shape, it converts back to the types you want:
import numpy as np
a = np.array([1,2,3])
b = np.array([1,2,3,4])
b = np.asarray([a,b,a], dtype=np.object)
print(b.dtype)
>>> object
print(b[0].dtype)
>>> int32
This question already has answers here:
Numpy sum elements in array based on its value
(2 answers)
Closed 4 years ago.
Maybe has been asked before, but I can't find it.
Sometimes I have an index I, and I want to add successively accordingly to this index to an numpy array, from another array. For example:
A = np.array([1,2,3])
B = np.array([10,20,30])
I = np.array([0,1,1])
for i in range(len(I)):
A[I[i]] += B[i]
print(A)
prints the expected (correct) value:
[11 52 3]
while
A[I] += B
print(A)
results in the expected (wrong) answer
[11 32 3].
Is there any way to do what I want in a vectorized way, without the loop?
If not, which is the fastest way to do this?
Use numpy.add.at:
>>> import numpy as np
>>> A = np.array([1,2,3])
>>> B = np.array([10,20,30])
>>> I = np.array([0,1,1])
>>>
>>> np.add.at(A, I, B)
>>> A
array([11, 52, 3])
Alternatively, np.bincount:
>>> A = np.array([1,2,3])
>>> B = np.array([10,20,30])
>>> I = np.array([0,1,1])
>>>
>>> A += np.bincount(I, B, minlength=A.size).astype(int)
>>> A
array([11, 52, 3])
Which is faster?
Depends. In this concrete example add.at seems marginally faster, presumably because we need to convert types in the bincount solution.
If OTOH A and B were float dtype then bincount would be faster.
You need to use np.add.at:
A = np.array([1,2,3])
B = np.array([10,20,30])
I = np.array([0,1,1])
np.add.at(A, I, B)
print(A)
prints
array([11, 52, 3])
This is noted in the doc:
ufunc.at(a, indices, b=None)
Performs unbuffered in place operation on operand ‘a’ for elements specified by ‘indices’. For addition ufunc, this method is equivalent to a[indices] += b, except that results are accumulated for elements that are indexed more than once. For example, a[[0,0]] += 1 will only increment the first element once because of buffering, whereas add.at(a, [0,0], 1) will increment the first element twice.
How to avoid returning true index while comparing 10.5 and 10?
A = np.array([1,2,3,4,5,6,7,8,9,10.5])
B = np.array([1,7,10])
i = np.searchsorted(A,B)
print i # [0 6 9]
I want to get the places of the exact matches: [0 6]
You could use np.searchsorted with left and right and only keep those who don't return the same index for both:
>>> import numpy as np
>>> A = np.array([1,2,3,4,5,6,7,8,9,10.5])
>>> B = np.array([1,7,10])
>>> i = np.searchsorted(A, B, 'left')
>>> j = np.searchsorted(A, B, 'right')
>>> i[i!=j]
array([0, 6], dtype=int64)
That works because searchsorted returns the index where the element needs to be inserted if you want to keep the other array sorted. So when the value is present in the other array it returns the index before the match (left) and the index after the matches(right). So if the index differs there's an exact match and if the index is the same there's no exact match
First you can help searchsorted, since A is sorted, for better complexity:
A = np.array([1,2,3,4,5,6,7,8,9,10.5])
B = np.array([1,7,10])
i = np.searchsorted(A,B,sorter=range(len(A)))
then in1d can find the exact correspondence :
j = i[np.in1d(A[i],B,True)]
# [0,6]
I have a 2D array size(2,3) and a list
Input:
a=['airplane','automobile','bird','cat','deer','dog','frog','horse','ship','truck']
b=[[4 2 8][1 7 0]] #2D numpy array shape (2,3) containing indices of list a
Output:
c = [['deer','bird','ship'],['automobile','horse','airplane']]
Is there any pythonic way or shortcut to achieve the output without iterating over each index value?
If you make your list a np.array as well, all you need is a[b]:
>>> import numpy as np
>>> keys = np.array(['airplane','automobile','bird','cat','deer','dog','frog','horse','ship','truck'])
>>> indices = np.array([[4,2,8],[1,7,0]])
>>> keys[indices]
array([['deer', 'bird', 'ship'],
['automobile', 'horse', 'airplane']],
dtype='<U10')
This does the job:
a=['airplane','automobile','bird','cat','deer','dog','frog','horse','ship','truck']
b=numpy.array([[4,2,8],[1,7,0]])
c = [[a[idx] for idx in row] for row in b]
I want to get the tangent inverse of a set of array
import numpy as np
import math
For example (this is an array)
x_value=[1 2 3 4 5 6]
a= abs(x_value-125)
This still works fine, but when I get the tangent inverse of a:
b=math.atan(a)
I got this error: TypeError: only length-1 arrays can be converted to Python scalars
How should I solve this error where I can get the tangent inverse of the elements of array a?
Just use np.arctan:
>>> import numpy as np
>>> a = np.array([1,2,3,4,5,6])
>>> a = abs(a - 125) # could use np.abs. It does the same thing, but might be more clear that you expect to get an ndarray instance as a result.
>>> a
array([124, 123, 122, 121, 120, 119])
>>> np.arctan(a)
array([ 1.56273199, 1.56266642, 1.56259979, 1.56253205, 1.56246319,
1.56239316])
you could use a list comprehension to apply the atan function to each element of the array:
a = np.abs(np.array([1,2,3,4,5,6]) - 125)
b = [np.math.atan(x) for x in a]
You can use a list comprehension:
b = [math.atan(ele) for ele in a]