Add a specifc value as a new dimension of a numpy array - python

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)

Related

Can't reshape color palette tuples using numpy

I have the following code which gets the color palette from series of images and try to reshape the output using numpy reshape. but when I try reshaping the output I get the error can't reshape array of size 27 into shape (3,3).
The output of Colours array print out is like this
[(256,256,265),(256,256,265),(256,256,265),(256,256,265),(256,256,265),(256,256,265),(256,256,265),(256,256,265),(256,256,265)]
Which are 9 tuples containing the colour palette which supposedly can be reshaped into 3 * 3
But numpy.reshape keeps saying it is 27 items and can't be reshaped into 3*3 array,
My question is how can I reshape this output into 3 * 3 array
So The colour array I need after reshaping should look something like this:
colours=[
[(256,256,265),(256,256,265),(256,256,265)],
[(256,256,265),(256,256,265),(256,256,265)],
[(256,256,265),(256,256,265),(256,256,265)]
]
from PIL import Image
import numpy as np
array=[]
for row in range(1,4):
for column in range(1,4):
filename = '/storage/emulated/0/python/banana/banana_0'+str(row)+'_0'+str(column)+'.png'
img = Image.open(filename)
img.show()
colors = img.getpixel((10,10))
array.append(colors)
array=np.array(array)
box_array=array.reshape(3,3)
You need to reshape using the full destination shape. Your array contains 27 elements in total
When you do:
array = np.array(array)
you obtain a (9, 3) shaped array, so you can't reshape it in (3, 3), but in (3, 3, 3).
you can proceed like:
box_array = array.reshape(3, 3, 3)
Depending on what dimension is subject to change in your array later, you can let numpy figure it out.
If for instance your 2nd and 3rd dimensions will always be (3, 3), then you can reshape your array as follows and numpy will detect automatically the 1st dimension:
box_array = array.reshape(-1, 3, 3)
And inversely if your 1st and 2nd dimensions will always be (3, 3), then you can reshape your array as follows and numpy will detect automatically the 3rd dimension:
box_array = array.reshape(3, 3, -1)

Reshaping array of shape x into an array of shape (x,y)

I am following a tutorial to implement the K-nearest Neighbor algorithm on a dataset.
I have an array of shape (6003,) and I want to do this:
data = data.reshape((data.shape[0], 3072))
However, I am getting this error:
cannot reshape array of size 6003 into shape (6003,3072)
Any help on this, please? Thanks!
when you reshape a numpy array the total number elements shouldn't change.
e.g. a =[2,3,4,5,1,7] if you want to reshape this to a 2Darray then the dimensions multiplied should be equal to the total number elements in the original array a.
this means you can reshape array a in to dimension of (1,6) (2,3),(6,1),(3,2).
the title of your question does give away the error by the way.
Reshaping array of shape (x,) into an array of shape (x,y)
is impossible because you are trying to add more elements into your original data.
an array of shape (x,) can only be reshaped into an array of shape (x/y,y)
I hope this helps.
You are trying to reshape into an incompatible shape. Now, what do I mean by that? Look at this example:
a = np.array([[1, 2, 3],
[4, 5, 6],
])
The shape of this array is:
a.shape
>> (2, 3)
Array a has 2 x 3 = 6 elements. Let's try to reshape it into a (2, 6) array
a.reshape(2, 6)
This raises
>> ValueError: cannot reshape array of size 6 into shape (2,6)
Notice that we were trying to make an array that has 2 x 3 = 6 elements into an array that would have 2 x 6 = 12 elements. But NumPy cannot add those extra elements into your original array and give that your desired shape. So it raises ValueError.
In your case, you are trying to make an array with 6003 elements into an array that will have 6003 x 3072 = 18441216 elements!

cannot reshape array of size (x,) into shape (x,y,z,1)

I'm trying to convert a numpy ndarray with a shape of (2200,) to numpy ndarray with a shape of (2200,250,250,1). every single row contains an image (shape: 250,250,1)
This is my object:
type(x_train_left) prints numpy.ndarray
x_train_left.shape prints (2200,)
type(x_train_left[0]) prints numpy.ndarray
x_train_left[0].shape prints (250, 250, 1)
But for some reason when i try to reshape x_train_left to (2200,250,250,1) i get the following error:
ValueError: cannot reshape array of size 2200 into shape (2200,250,250,1)
Thank for any help, iv'e searched for duplicated subjects, but they all have different problems.
I found the very simple solution:
np.stack(x_train_left)
and then when i try:
x_train_left.shape prints (2200, 250, 250, 1)

How to replace particular values based on condition by using tf.where()

I would like to replace values under condition.
NumPy version would go like this
intensity=np.where(
np.abs(intensity)<1e-4,
1e-4,
intensity)
But TensorFlow has a bit different usage for tf.where()
When I tried this
intensity=tf.where(
tf.math.abs(intensity)<1e-4,
1e-4,
intensity)
I got this error
ValueError: Shapes must be equal rank, but are 0 and 4 for 'Select' (op: 'Select') with input shapes: [?,512,512,1], [], [?,512,512,1].
Does that mean I should 4 dimensional tensor for 1e-4?
Following code passed the error
# Create an array which has small value (1e-4),
# whose shape is (2,512,512,1)
small_val=np.full((2,512,512,1),1e-4).astype("float32")
# Convert numpy array to tf.constant
small_val=tf.constant(small_val)
# Use tf.where()
intensity=tf.where(
tf.math.abs(intensity)<1e-4,
small_val,
intensity)
# Error doesn't occur
print(intensity.shape)
# (2, 512, 512, 1)

Subset a 3d numpy array

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.

Categories

Resources