I have two different arrays b0 and b1 where:
b0=[1,2]
b1=[3,4]
I want list[1st element of b0, 1st element of b1] to appended into new array B
and similarly:
list[2nd element of b0, 2nd element of b1] to appended into new array B
and so on......
that is my new array should be something like:
array([1,3],[2,4])
Below is my code:
b0=np.array([1,2])
b1=np.array([3,4])
for val in range(len(b1)):
L=[b0[val],b1[val]]
B=np.append(L,axis=0)
print(B)
I am getting missing on positional argument values error. Kindly help me to fix it.
If you insist to use numpy array, this is what I would do.
new = []
for x, y in zip(b0, b1):
new.append([x, y])
new = np.array(new)
Or list comprehension
new = np.array([[x,y] for x, y in zip(b0, b1)])
Result:
array([[1, 3],
[2, 4]])
Using np.append here isn't the most convenient way in my opinion. You can always cast python list into np.array and it's much easier to just use zip in this case.
b0=np.array([1,2])
b1=np.array([3,4])
B=np.array(list(zip(b0,b1)))
output:
>>> B
array([[1, 3],
[2, 4]])
In [51]: b0=np.array([1,2])
...: b1=np.array([3,4])
Order's wrong:
In [56]: np.vstack((b0,b1))
Out[56]:
array([[1, 2],
[3, 4]])
but you can transpose it:
In [57]: np.vstack((b0,b1)).T
Out[57]:
array([[1, 3],
[2, 4]])
stack is a more general purpose concatenator
In [58]: np.stack((b0,b1), axis=1)
Out[58]:
array([[1, 3],
[2, 4]])
or with:
In [59]: np.column_stack((b0,b1))
Out[59]:
array([[1, 3],
[2, 4]])
More details on combining arrays in my other recent answer: https://stackoverflow.com/a/56159553/901925
All these, including np.append use np.concatenate, just tweaking the dimensions in different ways first. np.append is often misused. It isn't a list append clone. None should be used repeatedly in a loop. They make a new array each time, which isn't very efficient.
Related
what is difference between np.copy and np.copy() ? I think np.copy() creates copy of numpy array object and np.copy is attribute of the object. But when i try
print(np.copy) or print(someObject.copy()) it shows message
'<built-in method copy of numpy.ndarray object at 0x00000214E46468D0>'
or
<function copy at 0x00000214E2B3BD30>
I'm expecting some value or object. Can you explain to me what is happening here?
edit: What is difference between
SomeObject.copy()
and
SomeObject.copy
?
Your print just displays the function or method identity. It doesn't run/evaluate either.
If x is an numpy array, np.copy(x) and x.copy() do the same thing.
More generally np.copy(x) will return an array regardless of what x is, while x.copy() uses the copy method defined for the x class. Those may be different.
In [175]: x = np.array([1, 2, 3])
In [176]: np.copy(x)
Out[176]: array([1, 2, 3])
In [177]: x.copy()
Out[177]: array([1, 2, 3])
In [178]: y = [1, 2, 3]
In [179]: np.copy(y)
Out[179]: array([1, 2, 3])
In [180]: y.copy()
Out[180]: [1, 2, 3]
They are two different things. print(np.copy) copies numpy. print(someObject.copy) copies the object.
I have 2 2d numpy arrays A and B
I want to remove all the rows in A which appear in B.
I tried something like this:
A[~np.isin(A, B)]
but isin keeps the dimensions of A, I need one boolean value per row to filter it.
EDIT: something like this
A = np.array([[3, 0, 4],
[3, 1, 1],
[0, 5, 9]])
B = np.array([[1, 1, 1],
[3, 1, 1]])
.....
A = np.array([[3, 0, 4],
[0, 5, 9]])
Probably not the most performant solution, but does exactly what you want. You can change the dtype of A and B to be a unit consisting of one row. You need to ensure that the arrays are contiguous first, e.g. with ascontiguousarray:
Av = np.ascontiguousarray(A).view(np.dtype([('', A.dtype, A.shape[1])])).ravel()
Bv = np.ascontiguousarray(B).view(Av.dtype).ravel()
Now you can apply np.isin directly:
>>> np.isin(Av, Bv)
array([False, True, False])
According to the docs, invert=True is faster than negating the output of isin, so you can do
A[np.isin(Av, Bv, invert=True)]
Try the following - it uses matrix multiplication for dimensionality reduction:
import numpy as np
A = np.array([[3, 0, 4],
[3, 1, 1],
[0, 5, 9]])
B = np.array([[1, 1, 1],
[3, 1, 1]])
arr_max = np.maximum(A.max(0) + 1, B.max(0) + 1)
print (A[~np.isin(A.dot(arr_max), B.dot(arr_max))])
Output:
[[3 0 4]
[0 5 9]]
This is certainly not the most performant solution but it is relatively easy to read:
A = np.array([row for row in A if row not in B])
Edit:
I found that the code does not correctly work, but this does:
A = [row for row in A if not any(np.equal(B, row).all(1))]
Could someone explain me why the second assertion below fails? I do not understand why using a slice or a range for indexing would make a difference in this case.
import numpy as np
d = np.zeros(shape = (1,2,3))
assert d[:, 0, slice(0,2)].shape == d[:, 0, range(0,2)].shape #This doesn't trigger an exception as both operands return (1,2)
assert d[0, :, slice(0,2)].shape == d[0, :, range(0,2)].shape #This does because (1,2) != (2,1)...
Make the array more diagnostic:
In [66]: d = np.arange(6).reshape(1,2,3)
In [67]: d
Out[67]:
array([[[0, 1, 2],
[3, 4, 5]]])
scalar index in the middle:
In [68]: d[:,0,:2]
Out[68]: array([[0, 1]])
In [69]: d[:,0,range(2)]
Out[69]: array([[0, 1]])
Shape is (1,2) for both, though the 2nd is a copy because of the advanced indexing of the last dimension.
Shape is the same in the 2nd set, but the order actually differs:
In [70]: d[0,:,:2]
Out[70]:
array([[0, 1],
[3, 4]])
In [71]: d[0,:,range(2)]
Out[71]:
array([[0, 3],
[1, 4]])
[71] is a case of mixed basic and advanced indexing, which is documented as doing the unexpected. The middle sliced dimension is put last.
https://numpy.org/doc/stable/reference/arrays.indexing.html#combining-advanced-and-basic-indexing
This question is similar to that already answered here, but that question does not address how to retrieve the indices of multiple elements.
I have a 2D tensor points with many rows and a small number of columns, and would like to get a tensor containing the row indices of all the elements in that tensor. I know what elements are present in points beforehand; It contains integer elements ranging from 0 to 999, and I can make a tensor using the range function to reflect the set of possible elements. The elements may be in any of the columns.
How can I retrieve the row indices where each element appears in my tensor in a way that avoids looping or using numpy, so I can do this quickly on a GPU?
I am looking for something like (points == elements).nonzero()[:,1]
Thanks!
try torch.cat([(t == i).nonzero() for i in elements_to_compare])
>>> import torch
>>> t = torch.empty((15,4)).random_(0, 999)
>>> t
tensor([[429., 833., 393., 828.],
[555., 893., 846., 909.],
[ 11., 861., 586., 222.],
[232., 92., 576., 452.],
[171., 341., 851., 953.],
[ 94., 46., 130., 413.],
[243., 251., 545., 331.],
[620., 29., 194., 176.],
[303., 905., 771., 149.],
[482., 225., 7., 315.],
[ 44., 547., 206., 299.],
[695., 7., 645., 385.],
[225., 898., 677., 693.],
[746., 21., 505., 875.],
[591., 254., 84., 888.]])
>>> torch.cat([(t == i).nonzero() for i in [7,385]])
tensor([[ 9, 2],
[11, 1],
[11, 3]])
>>> torch.cat([(t == i).nonzero()[:,1] for i in [7,385]])
tensor([2, 1, 3])
Numpy:
>>> np.nonzero(np.isin(t, [7,385]))
(array([ 9, 11, 11], dtype=int64), array([2, 1, 3], dtype=int64))
>>> np.nonzero(np.isin(t, [7,385]))[1]
array([2, 1, 3], dtype=int64)
I'm not sure if I'm correctly understanding what you're looking for, but if you want the indices of a certain value you could try using where and the sparse representation of the result.
E.g. in the below tensor points the value 998 is present at indices [0,0] and [2,0]. To get those indices one could:
In [34]: points=torch.tensor([ [998, 6], [1, 3], [998, 999], [2, 3] ] )
In [35]: torch.where(points==998, points, torch.tensor(0)).to_sparse().indices()
Out[35]:
tensor([[0, 2],
[0, 0]])
I have a numpy.ndarray like this -
[[1,2,3],[-7,7,2],[2,-3,4]]
I want remove the first element of each array and convert it into
[[2,3],[7,2],[-3,4]]
Is there any function that can do this, rather than having to use an explcit for loop?
It can be solved using list comprehensions as well -
my_list = [[1,2,3],[-7,7,2],[2,-3,4]]
my_list_out = [i[1:] for i in my_list]
print(my_list_out)
[[2, 3], [7, 2], [-3, 4]]