Related
Let's say I have a 2D array:
L = np.array([[1,2,3],
[4,5,6],
[7,8,9]])
I would like to make a 3D array from this, using a parameter N, such that (in this example, let's say N=4)
L2 = np.array([[[1,1,1,1],[2,2,2,2],[3,3,3,3]],
[[4,4,4,4],[5,5,5,5],[6,6,6,6]],
[[7,7,7,7],[8,8,8,8],[9,9,9,9]]])
Is there a nice way of doing this?
One option is to add another dimension, then repeat along the new dimension.
N = 4
out = L[..., None].repeat(N, axis=-1)
Output:
array([[[1, 1, 1, 1],
[2, 2, 2, 2],
[3, 3, 3, 3]],
[[4, 4, 4, 4],
[5, 5, 5, 5],
[6, 6, 6, 6]],
[[7, 7, 7, 7],
[8, 8, 8, 8],
[9, 9, 9, 9]]])
You can use a combination of swapaxes and broadcast_to:
N = 4
L2 = np.broadcast_to(L.swapaxes(0, 1), (N, *reversed(L.shape))).swapaxes(0, 2)
Output will be as desired.
Let's say I have data structured in a 2D array like this:
[[1, 3, 4, 6],
[1, 4, 8, 2],
[1, 3, 2, 9],
[2, 2, 4, 8],
[2, 4, 9, 1],
[2, 2, 9, 3]]
The first column denotes a third dimension, so I want to convert this to the following 3D array:
[[[3, 4, 6],
[4, 8, 2],
[3, 2, 9]],
[[2, 4, 8],
[4, 9, 1],
[2, 9, 3]]]
Is there a built-in numpy function to do this?
You can try code below:
import numpy as np
array = np.array([[1, 3, 4, 6],
[1, 4, 8, 2],
[1, 3, 2, 9],
[2, 2, 4, 8],
[2, 4, 9, 1],
[2, 2, 9, 3]])
array = np.delete(array, 0, 1)
array.reshape(2,3,-1)
Output
array([[[3, 4, 6],
[4, 8, 2],
[3, 2, 9]],
[[2, 4, 8],
[4, 9, 1],
[2, 9, 3]]])
However, this code can be used when you are aware of the array's shape. But if you are sure that the number of columns in the array is a multiple of 3, you can simply use code below to show the array in the desired format.
array.reshape(array.shape[0]//3,3,-3)
Use numpy array slicing with reshape function.
import numpy as np
arr = [[1, 3, 4, 6],
[1, 4, 8, 2],
[1, 3, 2, 9],
[2, 2, 4, 8],
[2, 4, 9, 1],
[2, 2, 9, 3]]
# convert the list to numpy array
arr = np.array(arr)
# remove first column from numpy array
arr = arr[:,1:]
# reshape the remaining array to desired shape
arr = arr.reshape(len(arr)//3,3,-1)
print(arr)
Output:
[[[3 4 6]
[4 8 2]
[3 2 9]]
[[2 4 8]
[4 9 1]
[2 9 3]]]
You list a non numpy array. I am unsure if you are just suggesting numpy as a means to get a non numpy result, or you are actually looking for a numpy array as result. If you don't actually need numpy, you could do something like this:
arr = [[1, 3, 4, 6],
[1, 4, 8, 2],
[1, 3, 2, 9],
[2, 2, 4, 8],
[2, 4, 9, 1],
[2, 2, 9, 3]]
# Length of the 3rd and 2nd dimension.
nz = arr[-1][0] + (arr[0][0]==0)
ny = int(len(arr)/nz)
res = [[arr[ny*z_idx+y_idx][1:] for y_idx in range(ny)] for z_idx in range(nz)]
OUTPUT:
[[[3, 4, 6], [4, 8, 2], [3, 2, 9]], [[2, 4, 8], [4, 9, 1], [2, 9, 3]]]
Note that the calculation of nz takes into account that the 3rd dimension index in your array is either 0-based (as python is per default) or 1-based (as you show in your example).
Please help me here, I am new to python. I am trying to add an array of one column to another array of shape 10000*17. Below are the samples. Please help me to concatenate two arrays:
[[ 0.02061183, -0.90794402, 0.92005926, 0.5405426 , 4.85191978, 3.65479782],
[-0.52203821, .50416184, -0.87750086, -0.625578 , -0.08879011, -0.57718519]]
I need to add a new row of array [1,2,3,4,5,6] to the above array.
You can use numpy.insert(arr, i, the_object_to_be_added, axis):
import numpy as np
my_array = [[1, 2, 3],
4, 5, 6]]
np.insert(my_array, 2, [7, 8, 9], axis=0)
# output my_array = [[1, 2, 3],
# [4, 5, 6],
# [7, 8, 9]]
or
my_array = [[1, 2, 3],
[4, 5, 6]]
np.insert(my_array, 1, [7, 8], axis=1)
# output my_array = [[1, 7, 2, 3],
# [4, 8, 5, 6]]
Quite straightforward question, I have the following array:
x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
I want to repeat this array over columns, having something like this:
array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[5, 5, 5],
[6, 6, 6],
[7, 7, 7],
[8, 8, 8]])
So, in order to do so I have been trying:
repeat_x = np.repeat(x, 3, axis = 1)
However, I get the following error:
AxisError: axis 1 is out of bounds for array of dimension 1
So, is there a way/trick to achieve my goal without having to use any sort of reshape?
Try this code:
np.array([x] * 3).T
Here 3 is the number of times you want to repeat those values
To do it purely in numpy without resorting back to python lists you need to use expand_dims followed by a transpose or use reshape to convert the vector into a matrix before using repeat.
x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
# array([1, 2, 3, 4, 5, 6, 7, 8])
x = x.reshape(-1, 1)
# array([[1],
# [2],
# [3],
# [4],
# [5],
# [6],
# [7],
# [8]])
np.repeat(x.reshape(-1, 1), 3, 1)
# array([[1, 1, 1],
# [2, 2, 2],
# [3, 3, 3],
# [4, 4, 4],
# [5, 5, 5],
# [6, 6, 6],
# [7, 7, 7],
# [8, 8, 8]])
Using expand dims and a transpose will be like
np.repeat(np.expand_dims(x, 0).T, 3, 1)
Same result.
Assume I have the following numpy array :
a = np.array([[4, 5, 8],
[7, 2, 9],
[1, 5, 3]])
and I want to extract points from the array 'a' to have this array :
b = array([[4, 8],
[1, 3]])
How can I do this ?
PS : In my real case I have 13*13 matrix and I want to create a 3*3 matrix from the first one
You can use np.ix_() for this to create a map of which values you want by location.
>>> a = np.arange(1,10).reshape(3,3)
>>> a
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
>>> b=np.ix_([0,2],[0, 2])
>>> a[b]
array([[1, 3],
[7, 9]])