I have a numpy array of the following shape - (363, 640, 4),
with the following values - [ 67 219 250 255]
e.g:
Shape
I want to map this array into the same size (363,640) but the values to be an integer 127.
I have tried to use numpy.vectorize without success, it returns None on the all array.
Is there a way to do it?
Thank you.
I do not fully understand the question, so I'm gonna guess you want to create an array, which we'll call B, with the same shape as the array you are showing, which we'll call A.
So I'm assuming you want B to be of the same shape as A, but filled with 127? Correct me if that's not the case, but to do what I'm guessing should be something along the lines:
import numpy as np
A = np.zeros((363, 640, 4)) # Just to initialize the array to something with your shape
B = np.full(np.shape(A), 127) # Returns an array of shape A filled with 127
Tell me if that is what you expected, if not, please could you detail a bit more the issue?
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'm looking to find the trace of matrices (using Numpy) in a function I have defined in Python. The input parameters tensor and tensor_transpose are both matrices of size (N,2,2) and are extracted from a VTK file (N is a rather large number and varies depending on the file). So both A and B are arrays of (N,2,2). By taking the trace of each array (sum of the diagonal terms), a single value for each array should be returned. So np.trace(A)**3)-(np.trace(B)**3 should be a single numerical value, with the array being of shape (N,1). My output though does not show this, with the returned shape being (2,).
Can anyone explain why? Is it an issue with the trace function and is there a solution?
import numpy as np
A=np.array(0.5*(tensor-tensor_transpose))
B=np.array(0.5*(tensor+tensor_transpose))
C=np.array(0.5*((np.trace(A)**3)-(np.trace(B)**3)))
print(A.shape)
print(B.shape)
print(C.shape)
#Output
#(60600, 2, 2)
#(60600, 2, 2)
#(2,)
Maybe you need to specify the axes:
np.trace(A, axis1=1, axis2=2)
I have a numpy array of shape (224,224,3) after reading a image. However I would like to convert this into a shape of (4,224,224,3).
I would like to kind of repeat the same values.
I am trying to append like shown below this but it doesn't work.
np.append(image,[[[4]]],axis=1)
Instead it throws the below error
ValueError: all the input arrays must have same number of dimensions
I expect my output shape to be (4,224,224,3)
Can you guide me on how to do this?
You could use np.repeat setting axis to 0:
out = np.repeat([image], 4, axis=0)
out.shape
# (4, 224, 224, 3)
I have checked the numpy documentation but some of the indexing still eludes me. I have a numpy array such that its shape is (40000, 432) and its looks something like:
arr = [[1,2,3......431,432],
[1,2,3......431,432],
[1,2,3......431,432],
....................
[1,2,3......431,432]'
[1,2,3......431,432]]
I wanted to subset each array over a range (ie. 20-50) so that the shape will be (40000, 30) and it will look like:
subarr = [[20,21,22...48,49,50],
[20,21,22...48,49,50],
[20,21,22...48,49,50],
.....................
[20,21,22...48,49,50]]
Everything I try either returns me an error or gives me the shape (30, 432) which is not what I need. How do I subset a 2d array along the axis I want to?
You want to use numpy slicing:
arr = np.zeros((40000, 432))
subarr = arr[:, 20:50]
print(subarr.shape)
Output
(40000L, 30L)
The L in the shape output indicates that the integer is of Python type long.
This question has been asked before, but the solution only works for 1D/2D arrays, and I need a more general answer.
How do you create a repeating array without replicating the data? This strikes me as something of general use, as it would help to vectorize python operations without the memory hit.
More specifically, I have a (y,x) array, which I want to tile multiple times to create a (z,y,x) array. I can do this with numpy.tile(array, (nz,1,1)), but I run out of memory. My specific case has x=1500, y=2000, z=700.
One simple trick is to use np.broadcast_arrays to broadcast your (x, y) against a z-long vector in the first dimension:
import numpy as np
M = np.arange(1500*2000).reshape(1500, 2000)
z = np.zeros(700)
# broadcasting over the first dimension
_, M_broadcast = np.broadcast_arrays(z[:, None, None], M[None, ...])
print M_broadcast.shape, M_broadcast.flags.owndata
# (700, 1500, 2000), False
To generalize the stride_tricks method given for a 1D array in this answer, you just need to include the shape and stride length for each dimension of your output array:
M_strided = np.lib.stride_tricks.as_strided(
M, # input array
(700, M.shape[0], M.shape[1]), # output dimensions
(0, M.strides[0], M.strides[1]) # stride length in bytes
)