I have an array with shape (64,64) in Python and I want to repeat these elements (three times) in the way that I could have an array with the shape (64,64,3). Any idea?
Probably the most simplest way to accomplish this here is by using numpy.dstack:
import numpy as np
b = np.dstack((a, a, a))
where a was the original array (shape 64×64), and b is the new array (shape 64×64×3).
Related
I use numpy to do image processing, I wanted to switch the image to black and white and for that I did the calculation in each cell to see the luminosity, but if i want to show it i have to transform a 2d array into 2d array with 3 times the same value
for exemple i have this:
a = np.array([[255,0][0,255]])
#into
b = np.array([[[255,255,255],[0,0,0]],[[0,0,0],[255,255,255]]])
I've been searching for a while but i don't find anything to help
PS: sorry if i have made some mistake with my English.
You'll want to us an explicit broadcast: https://numpy.org/doc/stable/reference/generated/numpy.broadcast_to.html#numpy.broadcast_to
b = np.broadcast_to(a[..., np.newaxis], (2, 2, 3))
Usually you don't need to do it explicitly, maybe try and see if just a[..., np.newaxis] and the standard broadcasting rules are enough.
Another way to do it
np.einsum('ij,k->ijk', a, [1,1,1])
It's a way to create a 3 dimensional array (hence the ijk), from a 2d array (ij) and a 1d array (k). Whose result is for all i,j,k being indices of a and of [1,1,1], the 3d matrix of a[i,j]×[1,1,1][k].
I have an np array n and the shape is (250,250). after that I converted (final array w)it into (250,250,3) because I need to do some operation on it.
is it possible to convert the shape of w to (250,250)?
thanks in advance.
I tried some reshaping operation of Numpy but it does not work!
Numpy reshaping array
Comparing two NumPy arrays for equality, element-wise
Numpy reshaping array
numpy.reshape
Gives a new shape to an array without changing its data.
so this is not right to convert array with shape of (250,250,3) into array with shape of (250,250) as 1st does have 187500 cells and 2nd does have 62500 cells.
You probably should use slicing, consider following example
import numpy as np
arr = np.array([[[0,1],[2,3]],[[4,5],[6,7]]]) # has shape (2,2,2)
arr2 = arr[:,:,0] # get certain cross-section, check what will happend if you use 1 inplace of 0 and 2 inplace of 0
print("arr2")
print(arr2)
print("arr2.shape",arr2.shape)
output
arr2
[[0 2]
[4 6]]
arr2.shape (2, 2)
This question here was useful, but mine is slightly different.
I am trying to do something simple here, I have a numpy matrix A, and I simply want to create another numpy matrix B, of the same shape as A, but I want B to be created from numpy.random.randn() How can this be done? Thanks.
np.random.randn takes the shape of the array as its input which you can get directly from the shape property of the first array. You have to unpack a.shape with the * operator in order to get the proper input for np.random.randn.
a = np.zeros([2, 3])
print(a.shape)
# outputs: (2, 3)
b = np.random.randn(*a.shape)
print(b.shape)
# outputs: (2, 3)
I am trying to create 3D array in python using Numpy and by multiplying 2D array in to 3rd dimension. I am quite new in Numpy multidimensional arrays and basically I am missing something important here.
In this example I am trying to make 10x10x20 3D array using base 2D array(10x10) by copying it 20 times.
My starting 2D array:
a = zeros(10,10)
for i in range(0,9):
a[i+1, i] = 1
What I tried to create 3D array:
b = zeros(20)
for i in range(0,19):
b[i]=a
This approach is probably stupid. So what is correct way to approach construction of 3D arrays from base 2D arrays?
Cheers.
Edit
Well I was doing things wrongly probably because of my R background.
Here is how I did it finally
b = zeros(20*10*10)
b = b.reshape((20,10,10))
for i in b:
for m in range(0, 9):
i[m+1, m] = 1
Are there any other ways to do the same?
There are many ways how to construct multidimensional arrays.
If you want to construct a 3D array from given 2D arrays you can do something like
import numpy
# just some 2D arrays with shape (10,20)
a1 = numpy.ones((10,20))
a2 = 2* numpy.ones((10,20))
a3 = 3* numpy.ones((10,20))
# creating 3D array with shape (3,10,20)
b = numpy.array((a1,a2,a3))
Depending on the situation there are other ways which are faster. However, as long as you use built-in constructors instead of loops you are on the fast side.
For your concrete example in Edit I would use numpy.tri
c = numpy.zeros((20,10,10))
c[:] = numpy.tri(10,10,-1) - numpy.tri(10,10,-2)
Came across similar problem...
I needed to modify 2D array into 3D array like so:
(y, x) -> (y, x, 3).
Here is couple solutions for this problem.
Solution 1
Using python tool set
array_3d = numpy.zeros(list(array_2d.shape) + [3], 'f')
for z in range(3):
array_3d[:, :, z] = array_2d.copy()
Solution 2
Using numpy tool set
array_3d = numpy.stack([array_2d.copy(), ]*3, axis=2)
That is what I came up with. If someone knows numpy to give a better solution I would love to see it! This works but I suspect there is a better way performance-wise.
I have two arrays
>>> array1.shape
(97, 195)
>>> array2.shape
(195,)
>>> array1 = numpy.concatenate((array1, array2), axis=0)
when I perform concatenate operation it shows an error
ValueError: all the input arrays must have same number of dimensions
is that the second array shape (195,) creating problem?
Just make both have the same dimensions and the same size except along the axis to be concatenated:
np.concatenate((array1, array2[np.newaxis,...]), axis=0)
In order for this to work, you need array2 to actually be 2d.
array1 = numpy.concatenate((array1, array2.reshape((1,195)))
should work
Another easy way to achieve the array concatenation that you’re looking for is to use Numpy’s vstack function as follows:
array1 = np.vstack([array1, array2])