Concatenating 3 multidimensional arrays in Python - python

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.

Related

Reshaping a list containing an array in Python

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])

Argsort issue in multi-dimensional array in Python

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.

Generate a cross dimensional 2-d array from given arrays of ranges

Suppose I have a list of lists like this:
list = [[1, 2, 3],
[4, 5, 6, 7],
[3, 4]]
I wish to create a 2-d array like this using the above input in Python:
result = [[1,4,3],[1,4,4],[1,5,3],[1,5,4],[1,6,3],[1,6,4],[1,7,3],[1,7,4],
[2,4,3],[2,4,4],[2,5,3],[2,5,4],[2,6,3],[2,6,4],[2,7,3],[2,7,4],
[3,4,3],[3,4,4],[3,5,3],[3,5,4],[3,6,3],[3,6,4],[3,7,3],[3,7,4]]
This is kind of a truth table.
Note - The length of list can vary (it can contain more lists) and also the length of inner lists can vary.
You can use itertools.product(). l is your original list (avoid using "list" as name, due to the existing built-in data structure in python)
res=[list(i) for i in itertools.product(*l)]
>>> print(res)
[[1, 4, 3], [1, 4, 4], [1, 5, 3], [1, 5, 4], [1, 6, 3], [1, 6, 4], [1, 7, 3], [1, 7, 4], [2, 4, 3], [2, 4, 4], [2, 5, 3], [2, 5, 4], [2, 6, 3], [2, 6, 4], [2, 7, 3], [2, 7, 4], [3, 4, 3], [3, 4, 4], [3, 5, 3], [3, 5, 4], [3, 6, 3], [3, 6, 4], [3, 7, 3], [3, 7, 4]]

Filling an array with arrays or vectors in python using numpy without a loop

I'm trying to find a way to fill an array with rows of values. It's much easier to express my desired output with an example. Given the input of an N x M matrix, array1,
array1 = np.array([[2, 3, 4],
[4, 8, 3],
[7, 6, 3]])
I would like to output an array of arrays in which each row is an N x N consisting of the values from the respective row. The output would be
[[[2, 3, 4],
[2, 3, 4],
[2, 3, 4]],
[[4, 8, 3],
[4, 8, 3],
[4, 8, 3]],
[[7, 6, 3],
[7, 6, 3],
[7, 6, 3]]]
You can reshape the array from 2d to 3d, then use numpy.repeat() along the desired axis:
np.repeat(array1[:, None, :], 3, axis=1)
#array([[[2, 3, 4],
# [2, 3, 4],
# [2, 3, 4]],
# [[4, 8, 3],
# [4, 8, 3],
# [4, 8, 3]],
# [[7, 6, 3],
# [7, 6, 3],
# [7, 6, 3]]])
Or equivalently you can use numpy.tile:
np.tile(array1[:, None, :], (1,3,1))
Another solution which is sometimes useful is the following
out = np.empty((3,3,3), dtype=array1.dtype)
out[...] = array1[:, None, :]

declaring and initialising 2d array in python

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.

Categories

Resources