ValueError: Found input variables with inconsistent numbers of samples: [13, 26] - python

This is my predictions outcome
array([[1., 0., 0.],
[0., 1., 0.],
[0., 1., 0.],
[0., 1., 0.],
[0., 1., 0.],
[0., 0., 1.],
[0., 1., 0.],
[0., 0., 1.],
[0., 1., 0.],
[0., 0., 1.],
[1., 0., 0.],
[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.],
[1., 0., 0.],
[0., 0., 1.],
[1., 0., 0.],
[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.],
[0., 1., 0.],
[0., 1., 0.],
[0., 1., 0.],
[0., 1., 0.],
[0., 0., 1.],
[1., 0., 0.]], dtype=float32)
and this is the confusion_matrix() function
cm = confusion_matrix(test_labels, predictions[:,0])
My query is how this confusion_matrix() functions works and how to solve this issue?
As I am a novice it will be really helpful if anyone can give me a little explanation.
Thank you.

This is primarily due to the shape of the arrays not being similar. Please check the arrays with test_labels.shape etc. Then use the reshape method or split them properly so that the shapes match.

Related

Create an identity matrix with scalar in python

This creates an identity matrix.
np.identity(3)
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
But what if I want to multiply with 3I like this?
array([[3., 0., 0.],
[0., 3., 0.],
[0., 0., 3.]])

Numpy - set first max value over multiple axes

I have a multidimensional array with the last two dimensions corresponding a "window". For each "window" in the array my goal is to set the first max value in the window to 1 and the rest of the values to 0. For example, for one window, we have this assignment:
[[0., 0., 0.], [[0., 0., 0.],
[0., 1., 1.], -> [0., 1., 0.],
[0., 1., 1.]], [0., 0., 0.]]
Now, i want to do this for all windows, where each window is on the last two dimensions, i.e. axis=(-1,-2). This is what I have tried so far:
# windows
x = np.array([[[[[[0., 0., 0.],
[0., 1., 1.],
[0., 1., 1.]],
[[0., 0., 0.],
[1., 1., 0.],
[1., 1., 0.]]],
[[[0., 1., 1.],
[0., 1., 1.],
[0., 1., 1.]],
[[1., 1., 0.],
[1., 1., 0.],
[1., 1., 0.]]],
[[[0., 1., 1.],
[0., 1., 1.],
[0., 1., 0.]],
[[1., 1., 0.],
[1., 1., 0.],
[1., 0., 1.]]]]]])
max_indices = np.argwhere(x == x.max()) # all indicies of max values in x
mask = np.zeros_like(x) # start of with everything set to 0
# I can set the first window by doing the following
mask[tuple(max_indices[0])] = 1.0
How can i do the same for all other windows? Preferably without looping.
My idea was to find unique values in max_indices where the last two dimensions are not taken into account, but i am not sure how to go about this.
EDIT
I have a case where x in the scenario above is taken from another
array using np.lib.stride_tricks.as_strided, which means that the strides will differ.
Here is the case:
# The image or whatever i want to modify
x = np.array([[[[0., 0., 0., 0.],
[0., 1., 1., 0.],
[0., 1., 1., 0.],
[0., 1., 1., 0.],
[0., 1., 0., 1.]]]])
# the windows of that image
x = np.lib.stride_tricks.as_strided(x, shape=(1, 1, 3, 2, 3, 3), strides=(160, 160, 32, 8, 32, 8))
Using the solution given by Felipe:
y = x[0][0] # Just unnesting it a bit
yr = y.reshape(-1, 9)
idx = yr.argmax(1)
y0 = np.zeros_like(yr)
np.put_along_axis(y0, idx[:, None], 1, axis=1)
y = y0.reshape(y.shape)
# plug it back
x[0][0] = y
x
array([[[[[[0., 0., 0.],
[0., 1., 0.],
[0., 1., 0.]],
[[0., 0., 0.],
[1., 0., 0.],
[1., 0., 0.]]],
[[[0., 1., 0.],
[0., 1., 0.],
[0., 0., 0.]],
[[1., 0., 0.],
[1., 0., 0.],
[0., 0., 0.]]],
[[[0., 1., 0.],
[0., 0., 0.],
[0., 0., 0.]],
[[1., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]]]]]])
The shape is the same but with different strides in x and y it seems as if "plugging it back" causes x and y to not be identical, which I would expect.
Here's one way using np.put_along_axis (though this is really just a somewhat faster loop).
y = x[0][0] # Just unnesting it a bit
yr = y.reshape(-1, 9)
idx = yr.argmax(1)
y0 = np.zeros_like(yr)
np.put_along_axis(y0, idx[:, None], 1, axis=1)
y = y0.reshape(y.shape)
Output:
array([[[[0., 0., 0.],
[0., 1., 0.],
[0., 0., 0.]],
[[0., 0., 0.],
[1., 0., 0.],
[0., 0., 0.]]],
[[[0., 1., 0.],
[0., 0., 0.],
[0., 0., 0.]],
[[1., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]]],
[[[0., 1., 0.],
[0., 0., 0.],
[0., 0., 0.]],
[[1., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]]]])
EDIT
Not the brightest idea but what I meant is you could reassign it first to a copy of it and then get your strided view again, something like:
x0 = x.copy() # Get a normal view
# [ Run the previous snippet ]
x0[0][0] = y # Assign the modified version
# Then if you really want another strided view you can recreate it
x0 = x0.reshape(saved_x.shape) # Reshape it to the original version pre-stride
x = np.lib.stride_tricks.as_strided(x0, shape=(1, 1, 3, 2, 3, 3), strides=(160, 160, 32, 8, 32, 8))

How to get the dictionary output from a generator, which outputs an array with a dictionary for custom keras image generator

I have a custom made generator to output multiple values to predict against. I'm trying to get the values to correspond to a given image, without success. Here is my output:
e(array([[[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
...,
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]],
[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
...,
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]],
[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
...,
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]],
...,
[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
...,
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]],
[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
...,
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]],
[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
...,
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]]],
[[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
...,
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]],
[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
...,
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]],
[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
...,
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]],
...,
[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
...,
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]],
[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
...,
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]],
[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
...,
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]]]], dtype=float16),
{'HourTime': array([0.3848, 0.2375], dtype=float16),
'MinTime': array([0.633, 0.862], dtype=float16),
'SecTime': array([0.967, 0.717], dtype=float16)})
My output comes from my generator here:
input_Size = 128
x,y,xrange,yrange = [136,150,47,47]
def ImGenA(directory, files_Loc, Hour, Min, Sec, batch_size):
while True:
batch_paths = imgId = np.random.choice(a = files_Loc.index, size=batch_size)
batch_input = []
batch_Hr = []
batch_Min = []
batch_Sec = []
for i in batch_paths:
img1 = cv2.imread(os.path.join(directory,files_Loc[i]))
img1 = ndimage.rotate(img1, 210)
img1 = cv2.resize (img1, (input_Size,input_Size))
batch_input+=[img1/255]
batch_Hr += [Hour[i]]
batch_Min += [Min[i]]
batch_Sec += [Sec[i]]
batch_x = np.array(batch_input, dtype='float16')
batch_y1 = np.array(batch_Hr, dtype='float16')
batch_y2 = np.array(batch_Min, dtype='float16')
batch_y3 = np.array(batch_Sec, dtype='float16')
yield( batch_x, {'HourTime' : batch_y1, 'MinTime': batch_y2, 'SecTime': batch_y3})
genA = ImGenA(directory=folder, files_Loc= train['ImageLoc'], Hour = train['HrPer'], Min = train['MinPer'], Sec = train['SecPer'],batch_size=2)
b=next(genA)
b[0][0] #provides image at position 0, but how do I find the Y output 'HourTime' at the same position?
I'm having difficulty extracting 'HourTime' from the saved output from a generator run. Sorry, I would assume it has been asked before, but I'm not sure how I can't find the answer.
It is simple, once you get it to work.
b[1]['HourTime'][0]
Provides the 'HourTime' from the dictionary for position 0.
IE. 0.3848

Reduce 3D volume mask by uniform margin in python

I'm working with 3D boolean arrays that mask a volume. My goal is to take a mask and reduce the area of the mask by some margin, m, in all dimensions.
Is there an easy way to do this using some common libraries (numpy, scipy, pandas, etc..)?
I found some code online that uses multiple for loops to expand a mask by one dimension. This works for the expansion case but I feel like there is a more compact way out there.
Here is a minimum example of what I am looking for in 2D.
Original
array([[0., 0., 1., 0., 0.],
[0., 1., 1., 1., 0.],
[1., 1., 1., 1., 1.],
[0., 1., 1., 1., 0.],
[0., 0., 1., 0., 0.]])
Uniform reduction by 1 pixel
array([[0., 0., 0., 0., 0.],
[0., 0., 1., 0., 0.],
[0., 1., 1., 1., 0.],
[0., 0., 1., 0., 0.],
[0., 0., 0., 0., 0.]])
But I want this to be in 3D space. Thank you for any input.
You might be looking for scipy.ndimage.binary_erosion(a):
a = np.array([
[0., 0., 1., 0., 0.],
[0., 1., 1., 1., 0.],
[1., 1., 1., 1., 1.],
[0., 1., 1., 1., 0.],
[0., 0., 1., 0., 0.]
])
b = scipy.ndimage.binary_erosion(a) # returns an array of bool
Note that this will erode internal surfaces too

Is it possible to Copy Subregion of One 2D Array to Another Using numpy.ndarray

I have some 2d arrays using numpy, and I want to copy subregions from one into another. For example, if I start with:
dest = numpy.zeros((4, 4))
# array([[0., 0., 0., 0.],
# [0., 0., 0., 0.],
# [0., 0., 0., 0.],
# [0., 0., 0., 0.]])
src = numpy.ones((4, 4))
# array([[1., 1., 1., 1.],
# [1., 1., 1., 1.],
# [1., 1., 1., 1.],
# [1., 1., 1., 1.]])
I want to somehow say that the src should be copied into dest at (2,1), such that source would then look like:
array([[0., 0., 0., 0.],
[0., 0., 1., 1.],
[0., 0., 1., 1.],
[0., 0., 1., 1.]])
Or if (-3, 0), then:
array([[1., 0., 0., 0.],
[1., 0., 0., 0.],
[1., 0., 0., 0.],
[1., 0., 0., 0.]])
I can do the good old fashioned double index loop to do this, but I was hoping numpy had some clever magic that did it. I looked at take, but couldn't see how to make that the tool for this job.
Both of these can be accomplished with numpy indexing. To understand how this works, the documentation is always your friend.
Your first case:
dest[1: ,2:] = src[1: ,2:]
array([[0., 0., 0., 0.],
[0., 0., 1., 1.],
[0., 0., 1., 1.],
[0., 0., 1., 1.]])
Your second case: (You indicated column -3 but your results indicate -4)
dest[:, -4] = src[:, -4]
array([[1., 0., 0., 0.],
[1., 0., 0., 0.],
[1., 0., 0., 0.],
[1., 0., 0., 0.]])

Categories

Resources