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.
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).
Let's say I have this numpy matrix:
>>> mat = np.matrix([[3,4,5,2,1], [1,2,7,6,5], [8,9,4,5,2]])
>>> mat
matrix([[3, 4, 5, 2, 1],
[1, 2, 7, 6, 5],
[8, 9, 4, 5, 2]])
Now let's say I have some indexes in this form:
>>> ind = np.matrix([[0,2,3], [0,4,2], [3,1,2]])
>>> ind
matrix([[0, 2, 3],
[0, 4, 2],
[3, 1, 2]])
What I would like to do is to get three values from each row of the matrix, specifically values at columns 0, 2, and 3 for the first row, values at columns 0, 4 and 2 for the second row, etc. This is the expected output:
matrix([[3, 5, 2],
[1, 5, 7],
[5, 9, 4]])
I've tried using np.take but it doesn't seem to work. Any suggestion?
This is take_along_axis.
>>> np.take_along_axis(mat, ind, axis=1)
matrix([[3, 5, 2],
[1, 5, 7],
[5, 9, 4]])
This will do it: mat[np.arange(3).reshape(-1, 1), ind]
In [245]: mat[np.arange(3).reshape(-1, 1), ind]
Out[245]:
matrix([[3, 5, 2],
[1, 5, 7],
[5, 9, 4]])
(but take_along_axis in #user3483203's answer is simpler).
I am new to python and I need some help in getting this task done.
How can I implement following matrix in python, using numPy?
9 x 5 matrix F where each column of F is [1.....9]^T .
import numpy as np
matrix = np.array([[i]*5 for i in range(1, 10)])
This basically creates a matrix whose first row is filled with 1s, second row with 2s, etc.
Your 1d array:
In [25]: np.arange(1,10)
Out[25]: array([1, 2, 3, 4, 5, 6, 7, 8, 9])
Make a 2d column vector:
In [26]: np.arange(1,10)[:,None]
Out[26]:
array([[1],
[2],
[3],
[4],
[5],
[6],
[7],
[8],
[9]])
Use repeat method to replicate the columns:
In [27]: np.arange(1,10)[:,None].repeat(5,1)
Out[27]:
array([[1, 1, 1, 1, 1],
[2, 2, 2, 2, 2],
[3, 3, 3, 3, 3],
[4, 4, 4, 4, 4],
[5, 5, 5, 5, 5],
[6, 6, 6, 6, 6],
[7, 7, 7, 7, 7],
[8, 8, 8, 8, 8],
[9, 9, 9, 9, 9]])
let's say I have the following 3x4 array
master_array = [[1, 3, 4, 5],
[6, 5, 4, 1],
[7, 8, 4, 1]]
Then, I want to delete number 4 from each of the 3 1x4 subarrays. Would I use the following?
for i in range(master_array.shape[0]):
np.delete(master_array[i], 3)
Then, when I print the master_array, would I get?
[[1, 3, 5],
[6, 5, 1],
[7, 8, 1]]
In case master_array is a list of lists, like in your example, you could do
master_array = [[1, 3, 4, 5],
[6, 5, 4, 1],
[7, 8, 4, 1]]
for row in master_array:
del row[2]
In case master_array is indeed a numpy array, you would simply do
master_array = np.array([[1, 3, 4, 5],
[6, 5, 4, 1],
[7, 8, 4, 1]])
np.delete(master_array, 2, axis=1)