I have arrays I1 (shape=(1, 10, 2)) and I2 (shape=(2,)). I am trying to sort using argsort() but I am getting an error for I2.
import numpy as np
I1=np.array([[[0, 1],
[0, 3],
[1, 2],
[1, 4],
[2, 5],
[3, 4],
[3, 6],
[4, 7],
[5, 4],
[6, 7]]])
I2=np.array([[[0, 1],
[0, 3],
[1, 2],
[1, 4],
[2, 5],
[3, 4],
[3, 6],
[4, 7],
[5, 4],
[6, 7]],
[[0, 1],
[0, 3],
[1, 2],
[1, 4],
[2, 5],
[3, 4],
[3, 6],
[4, 7]]])
order1 = I1[0,:, 1].argsort()
print("order1 =",[order1])
order2 = I2[0,:, 1].argsort()
print("order2 =",[order2])
The error is
in <module>
order2 = I2[0,:, 1].argsort()
IndexError: too many indices for array: array is 1-dimensional, but 3 were indexed
If you would print I2, you'll quickly see what is causing the problem:
array([list([[0, 1], [0, 3], [1, 2], [1, 4], [2, 5], [3, 4], [3, 6], [4, 7], [5, 4], [6, 7]]),
list([[0, 1], [0, 3], [1, 2], [1, 4], [2, 5], [3, 4], [3, 6], [4, 7]])],
dtype=object)
I2 is not a three dimensional array, but a one dimensional array of lists (each individual list consists of a list of 2-element lists).
In fact, when you create I2, with a recent NumPy, you should also see a DeprecationWarning:
VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
I2=np.array([[[0, 1],
which essentially identifies the same problem. Indeed, it states "from ragged nested sequences". Ragged is key here: your input outer list contains two lists that are not of equal length. As a result the three dimensional nested list is not of "rectangular" (box-shaped) dimensions, but it's a collection of list.
If you planned to you use your data this way with NumPy, you can't, really: NumPy is meant for (fast) operations with regular arrays, not with ragged arrays.
Related
I want to reshape the list Ii to (1,11,2) but I am getting an error. I present the expected output.
import numpy as np
Ii=[np.array([[0, 2],
[2, 4],
[2, 5],
[2, 6],
[3, 1],
[3, 7],
[4, 5],
[4, 6],
[5, 3],
[5, 7],
[6, 5]])]
Y=Ii[0].shape
Ii=Ii[0].reshape(1,Y)
The error is
in <module>
Ii=Ii[0].reshape(1,Y)
TypeError: 'tuple' object cannot be interpreted as an integer
The expected output is
array([[[0, 2],
[2, 4],
[2, 5],
[2, 6],
[3, 1],
[3, 7],
[4, 5],
[4, 6],
[5, 3],
[5, 7],
[6, 5]]])
np.newaxis can be used here to reshape the array.
Ii[0][np.newaxis,:]
or you can use reshaping after unpacking tuple.
Ii[0].reshape([1, *Y])
Try this:
Ii = Ii[0].reshape(1, Y[0], Y[1])
I have three arrays I1,I2,I3 with shape (1,9,2). I am trying to append but there is an error. The new array should have shape (3,9,2)
import numpy as np
I1 = np.array([[[0, 2],
[2, 3],
[2, 5],
[3, 4],
[3, 6],
[4, 1],
[4, 7],
[5, 6],
[6, 7]]])
I2 = np.array([[[1, 1],
[2, 3],
[2, 5],
[3, 4],
[3, 6],
[4, 1],
[4, 7],
[5, 6],
[6, 7]]])
I3 = np.array([[[2, 2],
[2, 3],
[2, 5],
[3, 4],
[3, 6],
[4, 1],
[4, 7],
[5, 6],
[6, 7]]])
I=np.append(I1,I2,I3,axis=0)
The error is
in <module>
Iit=np.append(Iit1,Iit2,Iit3,axis=0)
File "<__array_function__ internals>", line 4, in append
TypeError: _append_dispatcher() got multiple values for argument 'axis'
Use I=np.concatenate([I1,I2,I3],axis=0) rather than append. Append does what you'd expect from a list (and try not to use it in numpy, anyway, for memory allocation reasons!)`
A different way from #Dominik is
I = np.stack((I1,I2,I3))
stack creates a new axis.
I am trying to index a batch in tensorflow with a ragged tensor.
X = tf.constant([[[1,2,3], [4,5,6], [7,8,9]],
[[9,8,7], [6,5,4], [3,2,1]]])
The first dimension is the batch, the second is the sequence length.
Using gather_nd I can select the individual rows and columns.
tf.gather_nd(X, [[[0, 1], [0, 2], [0, 0]], [[1, 0], [1, 1], [1, 2]]])
But I have to use a ragged tensor as the input for the selection.
For example.
tf.gather_nd(X, [[[0, 1], [0, 2]], [[1, 0], [1, 1], [1, 2]]])
This of course does not work.
Is there a way to make the above code work?
You need to explicitly create a RaggedTensor object as tensorflow does not recognize them automatically:
>>> tf.gather_nd(X, tf.ragged.constant([[[0, 1], [0, 2]], [[1, 0], [1, 1], [1, 2]]], inner_shape=(2,)))
<tf.RaggedTensor [[[4, 5, 6], [7, 8, 9]], [[9, 8, 7], [6, 5, 4], [3, 2, 1]]]>
However, if your ultimate goal is to filter out specific batches, tf.boolean_mask (API) might be more straight-forward for this.
I have data like this:
[[[1, 2, 3]
[4, 5, 6]]
[[7, 8, 9]
[0, 1, 2]]]
and I need to get it into this "shape":
[[[1, 4], [2, 5], [3, 6]]
[[7, 0], [8, 1], [9, 2]]]
The best method I have for doing this so far is:
for i in range(2):
pairs = tuple(
array[i, :, j] for j in range(3) # ---axis-2-length---
)
print(pairs)
to produce:
([1, 4], [2, 5], [3, 6])
([7, 0], [8, 1], [9, 2])
While this gets the job done, that's a lot of interpreted Python, especially as these datasets grow. So I was wondering if I could somehow get this done with fancy indexing or transposition or something else my numpy-fu is not yet strong enough to conceive of myself.
Does anyone know how to get this done more elegantly with numpy?
Transposition with ndarray.transpose will work here.
>>> x.transpose(0, 2, 1)
array([[[1, 4],
[2, 5],
[3, 6]],
[[7, 0],
[8, 1],
[9, 2]]])
I am declaring multidimensional array in python
Nbrs[23][2] = [[1, 1], [1, 2], [2, 1],
[2, 3], [3, 2], [1, 3],
[3, 1], [1, 4], [3, 4],
[4, 3], [4, 1], [1, 5],
[2, 5], [3, 5], [4, 5],
[5, 4], [5, 3], [5, 2],
[5, 1], [1, 6], [5, 6],
[6, 5], [6, 1]
]
It gives me error as:
NameError: name 'Nbrs' is not defined
I cannot declare 2 dimensional array in python by this way?
Assignment statement:
Nbrs[23][2] = [[1, 1], [1, 2], [2
# ^ ^ you can't index Nbrs before it created
should be:
Nbrs = [[1, 1], [1, 2], [2
# now after this statement, Nbrs a list of list you can access
# its elements useng `Nbrs[i][j]` for i < len(Nbrs) and j < 2
I think you confuses because of C, C++ declarations!
You don't need to specify the dimensions when defining lists in python. When you type Nbrs[23][2] python is trying to find what's at [23][2] in Nbrs but in this case Nbrs doesn't exist because you are trying to define it for the first time here.
Instead do this:
Nbrs = [[1, 1], [1, 2], [2, 1], ....
That's not the right syntax. You don't need to include anything about the variable's type on the left-hand side; in particular, drop the dimensions.
Nbrs = [[1, 1], [1, 2], [2, 1], [2, 3], [3, 2], [1, 3], [3, 1], [1, 4], [3, 4], [4, 3], [4, 1], [1, 5], [2, 5], [3, 5], [4, 5], [5, 4], [5, 3], [5, 2], [5, 1], [1, 6], [5, 6], [6, 5], [6, 1]]
What you've written tries to assign to an element of Nbrs, which doesn't exist yet.