what's the mean of "reshape(-1,1,2)" - python

x = np.linspace(0,10, 5)
y = 2*x
points = np.array([x, y]).T.reshape(-1, 1, 2)
What's the mean of the third line?I know the mean of reshape(m,n), but what does reshape(-1, 1, 2) means?

Your question is not entirely clear, so I'm guessing the -1 part is what troubles you.
From the documantaion:
The new shape should be compatible with the original shape. If an integer, then the result will be a 1-D array of that length. One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions.
The whole line meaning is this (breaking it down for simplicity):
points = np.array([x, y]) -> create a 2 X 5 np.array consisting of x,y
.T -> transpose
.reshape(-1, 1, 2) -> reshape it, in this case to a 5X1X2 array (as can seen by the output of points.shape [(5L, 1L, 2L)])

vertices = np.array([[100,300],[200,200],[400,300],[200,400]],np.int32)
vertices.shape
pts = vertices.reshape((-1,1,2))
refer this image
Consider the above code
here we have created set of vertices for to be plotted on a image using opencv but opencv expects 3d array but we only have vertices in 2d array.So the .reshape((-1,1,2)) allows us to keep the original array intact while adding the 3rd dimension to the array(Notice the extra brackets added to the list).This third dimension coontains the details for colors i.e RGB

Related

Why is np.pad not working the way I expect it to?

My code generates an array that is 4x2.
It also generates another array that is 10x6
I want to pad each array with zeros so that it is centered in an array that is 14x12 after padding.
https://docs.scipy.org/doc/numpy/reference/generated/numpy.pad.html
a = some array 4x2
b = some array 10x6
c = np.pad(a, padder=0, 2, 'pad_width', padder=0))
TypeError: pad() takes exactly 3 arguments (2 given)
Like this you get an array with shape(14,12) with the smaller array centered.
source_array = np.random.rand(10,6)
target_array_shape = (14,12)
pad_x = (target_array_shape[0]-source_array.shape[0])//2
pad_y = (target_array_shape[1]-source_array.shape[1])//2
target_array = np.pad(source_array, ((pad_x,pad_x),(pad_y,pad_y)), mode="constant")
Obviously the centering can only be correct if the source array is smaller than the target array otherwise you get a ValueError(index can't contain negative values).
Also the target dimension might not be correct if the target and source dimension, are not both even or both odd.

Numpy [...,None]

I have found myself needing to add features to existing numpy arrays which has led to a question around what the last portion of the following code is actually doing:
np.ones(shape=feature_set.shape)[...,None]
Set-up
As an example, let's say I wish to solve for linear regression parameter estimates by using numpy and solving:
Assume I have a feature set shape (50,1), a target variable of shape (50,), and I wish to use the shape of my target variable to add a column for intercept values.
It would look something like this:
# Create random target & feature set
y_train = np.random.randint(0,100, size = (50,))
feature_set = np.random.randint(0,100,size=(50,1))
# Build a set of 1s after shape of target variable
int_train = np.ones(shape=y_train.shape)[...,None]
# Able to then add int_train to feature set
X = np.concatenate((int_train, feature_set),1)
What I Think I Know
I see the difference in output when I include [...,None] vs when I leave it off. Here it is:
The second version returns an error around input arrays needing the same number of dimensions, and eventually I stumbled on the solution to use [...,None].
Main Question
While I see the output of [...,None] gives me what I want, I am struggling to find any information on what it is actually supposed to do. Can anybody walk me through what this code actually means, what the None argument is doing, etc?
Thank you!
The slice of [..., None] consists of two "shortcuts":
The ellipsis literal component:
The dots (...) represent as many colons as needed to produce a complete indexing tuple. For example, if x is a rank 5 array (i.e., it has 5 axes), then
x[1,2,...] is equivalent to x[1,2,:,:,:],
x[...,3] to x[:,:,:,:,3] and
x[4,...,5,:] to x[4,:,:,5,:].
(Source)
The None component:
numpy.newaxis
The newaxis object can be used in all slicing operations to create an axis of length one. newaxis is an alias for ‘None’, and ‘None’ can be used in place of this with the same result.
(Source)
So, arr[..., None] takes an array of dimension N and "adds" a dimension "at the end" for a resulting array of dimension N+1.
Example:
import numpy as np
x = np.array([[1,2,3],[4,5,6]])
print(x.shape) # (2, 3)
y = x[...,None]
print(y.shape) # (2, 3, 1)
z = x[:,:,np.newaxis]
print(z.shape) # (2, 3, 1)
a = np.expand_dims(x, axis=-1)
print(a.shape) # (2, 3, 1)
print((y == z).all()) # True
print((y == a).all()) # True
Consider this code:
np.ones(shape=(2,3))[...,None].shape
As you see the 'None' phrase change the (2,3) matrix to a (2,3,1) tensor. As a matter of fact it put the matrix in the LAST index of the tensor.
If you use
np.ones(shape=(2,3))[None, ...].shape
it put the matrix in the FIRST‌ index of the tensor

Reshaping array of matrices in Python

I have a Numpy array X of n 2x2 matrices, arranged so that X.shape = (2,2,n), that is, to get the first matrix I call X[:,:,0]. I would like to reshape X into an array Y such that I can get the first matrix by calling Y[0] etc., but performing X.reshape(n,2,2) messes up the matrices. How can I get it to preserve the matrices while reshaping the array?
I am essentially trying to do this:
import numpy as np
Y = np.zeros([n,2,2])
for i in range(n):
Y[i] = X[:,:,i]
but without using the for loop. How can I do this with reshape or a similar function?
(To get an example array X, try X = np.concatenate([np.identity(2)[:,:,None]] * n, axis=2) for some n.)
numpy.moveaxis can be used to take a view of an array with one axis moved to a different position in the shape:
numpy.moveaxis(X, 2, 0)
numpy.moveaxis(a, source, destination) takes a view of array a where the axis originally at position source ends up at position destination, so numpy.moveaxis(X, 2, 0) makes the original axis 2 the new axis 0 in the view.
There's also numpy.transpose, which can be used to perform arbitrary rearrangements of an array's axes in one go if you pass it the optional second argument, and numpy.rollaxis, an older version of moveaxis with a more confusing calling convention.
Use swapaxis:
Y = X.swapaxes(0,2)

Numpy remove a dimension from np array

I have some images I want to work with, the problem is that there are two kinds of images both are 106 x 106 pixels, some are in color and some are black and white.
one with only two (2) dimensions:
(106,106)
and one with three (3)
(106,106,3)
Is there a way I can strip this last dimension?
I tried np.delete, but it did not seem to work.
np.shape(np.delete(Xtrain[0], [2] , 2))
Out[67]: (106, 106, 2)
You could use numpy's fancy indexing (an extension to Python's built-in slice notation):
x = np.zeros( (106, 106, 3) )
result = x[:, :, 0]
print(result.shape)
prints
(106, 106)
A shape of (106, 106, 3) means you have 3 sets of things that have shape (106, 106). So in order to "strip" the last dimension, you just have to pick one of these (that's what the fancy indexing does).
You can keep any slice you want. I arbitrarily choose to keep the 0th, since you didn't specify what you wanted. So, result = x[:, :, 1] and result = x[:, :, 2] would give the desired shape as well: it all just depends on which slice you need to keep.
if you have multiple dimensional this might help
pred_mask[0,...] #Remove First Dim
Pred_mask[...,0] #Remove Last Dim
Just take the mean value over the colors dimension (axis=2):
Xtrain_monochrome = Xtrain.mean(axis=2)
When the shape of your array is (106, 106, 3), you can visualize it as a table with 106 rows and 106 columns filled with data points where each point is array of 3 numbers which we can represent as [x, y ,z]. Therefore, if you want to get the dimensions (106, 106), you must make the data points in your table of to not be arrays but single numbers. You can achieve this by extracting either the x-component, y-component or z-component of each data point or by applying a function that somehow aggregates the three component like the mean, sum, max etc. You can extract any component just like #matt Messersmith suggested above.
well, you should be careful when you are trying to reduce the dimensions of an image.
An Image is normally a 3-D matrix that contains data of the RGB values of each pixel. If you want to reduce it to 2-D, what you really are doing is converting a colored RGB image into a grayscale image.
And there are several ways to do this like you can take the maximum of three, min, average, sum, etc, depending on the accuracy you want in your image. The best you can do is, take a weighted average of the RGB values using the formula
Y = 0.299R + 0.587G + 0.114B
where R stands for RED, G is GREEN and B is BLUE. In numpy, this can be written as
new_image = img[:, :, 0]*0.299 + img[:, :, 1]*0.587 + img[:, :, 2]*0.114
Actually np.delete would work if you would apply it two times,
if you want to preserve the first channel for example then you could run the following:
Xtrain = np.delete(Xtrain,2,2) # this will get rid of the 3rd component of the 3 dimensions
print(Xtrain.shape) # will now output (106,106,2)
# again we apply np.delete but on the second component of the 3rd dimension
Xtrain = np.delete(Xtrain,1,2)
print(Xtrain.shape) # will now output (106,106,1)
# you may finally squeeze your output to get a 2d array
Xtrain = Xtrain.squeeze()
print(Xtrain.shape) # will now output (106,106)

Numpy columnwise subtraction; subtract (k,n) array from values in a (k,) array

Say I have
x=np.random.random((3,10))
y=np.array([1,10,100])
and I want to subtract the values in each column of x from y. I can do this like so:
np.array([y]*10).T-x
However, this involves the creation of a new array of 10 times the size of y (in this case). I could also imagine using a for loop. Can anyone suggest a better way of doing this?
Just stack y vertically and subtract x. For example:
y[:, None] - x # insert a new axis into y (creating a new view of y)
or
np.vstack(y) - x # stack y vertically (creating a copy of y)
This works because y now has shape (3, 1) and so can be broadcast with your x which has shape (3, 10).
x and y can be broadcast together if each trailing dimension (i.e. starting from the end of the shape tuple) of x is equal to the trailing dimension of y, or if one of the compared dimensions is 1. In this example, the trailing dimension of y was changed to 1, so that the two arrays are compatible.
Something like this?
>>> (y - x.T).T
array([[ 8.79354250e-01, 5.30104393e-01, 7.78342126e-01,
4.56857161e-02, 5.32181828e-01, 1.47155126e-01,
3.39654176e-01, 3.72693537e-01, 4.32737024e-01,
7.55366710e-01],
[ 9.53976069e+00, 9.51725133e+00, 9.00439583e+00,
9.65411497e+00, 9.55728110e+00, 9.35189161e+00,
9.72451832e+00, 9.20089714e+00, 9.60367043e+00,
9.41722649e+00],
[ 9.99248465e+01, 9.96932738e+01, 9.93110996e+01,
9.94116657e+01, 9.98695626e+01, 9.92118001e+01,
9.93602275e+01, 9.99518088e+01, 9.98442735e+01,
9.93865628e+01]])

Categories

Resources