I wonder what is the meaning of .T in the function np.vstack(array1, array2, array3).T?
ndarray.T: The transposed array.
.T implies transpose of the stacked arrays.
Related
I have a 2d array s and I want to calculate differences elementwise, i.e.:
Since it cannot be written as a single matrix multiplication, I was wondering what is the proper way to vectorize it?
You can use broadcasting for that: d = s[:, None, :] - s[None, :, :]. Note the None enable you to create a new dimension. Numpy implicitly perform the broadcasting operation between the two arrays.
Let's suppose I have an image such that
image.shape=(280,280,3)
If I do img[[[1,2],[1,2],[1,2]]].shape, I obtain (2,). But I expect to obtain (2,2,2)...
How can slicing be performed simultaneously on several dimensions in numpy?
You are using the Array of Indices syntax, when you probably want slices.
Try something like this:
img[1:3, 1:3, 1:3]
I have initialized this empty 2d np.array
inputs = np.empty((300, 2), int)
And I am attempting to append a 2d row to it as such
inputs = np.append(inputs, np.array([1,2]), axis=0)
But Im getting
ValueError: all the input arrays must have same number of dimensions
And Numpy thinks it's a 2 row 0 dimensional object (transpose of 2d)
np.array([1, 2]).shape
(2,)
Where have I gone wrong?
To add a row to a (300,2) shape array, you need a (1,2) shape array. Note the matching 2nd dimension.
np.array([[1,2]]) works. So does np.array([1,2])[None, :] and np.atleast_2d([1,2]).
I encourage the use of np.concatenate. It forces you to think more carefully about the dimensions.
Do you really want to start with np.empty? Look at its values. They are random, and probably large.
#Divakar suggests np.row_stack. That puzzled me a bit, until I checked and found that it is just another name for np.vstack. That function passes all inputs through np.atleast_2d before doing np.concatenate. So ultimately the same solution - turn the (2,) array into a (1,2)
Numpy requires double brackets to declare an array literal, so
np.array([1,2])
needs to be
np.array([[1,2]])
If you intend to append that as the last row into inputs, you can just simply use np.row_stack -
np.row_stack((inputs,np.array([1,2])))
Please note this np.array([1,2]) is a 1D array.
You can even pass it a 2D row version for the same result -
np.row_stack((inputs,np.array([[1,2]])))
I have a tensor T (shape:300) and a array A(shape:300), what i want to do is combine them into a new array [T,A] with the shape (600). I tried the solutiona below:
1 combine directly,use function: np.concatenate((T,A)), the result is:zero-dimensional arrays cannot be concatenated
2 switch one type to another, try to switch the T to the type of numpy.array: i use: a=np.array(T), but when print a.shape, it is (), nothing in the bracket.
Besides, when i print T.shape and A.shape, T.shape is ([300]) and A.shape is (300,)what is the difference?
when we want to get a numpy.array from a tensor T, it can be done by T.eval(), i tried a lot and found this way. But i haven't found the way switched from numpy.array to tensor T yet. Anyone can help?
I have a pandas dataframe and passing df[list_of_columns] as X and df[[single_column]] as Y to a Random Forest regressor.
What does the following warnning mean and what should be done to resolve it?
DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel(). probas = cfr.fit(trainset_X, trainset_Y).predict(testset_X)
Simply check the shape of your Y variable, it should be a one-dimensional object, and you are probably passing something with more (possibly trivial) dimensions. Reshape it to the form of list/1d array.
You can use df.single_column.values or df['single_column'].values to get the underlying numpy array of your series (which, in this case, should also have the correct 1D-shape as mentioned by lejlot).
Actually the warning tells you exactly what is the problem:
You pass a 2d array which happened to be in the form (X, 1), but the method expects a 1d array and has to be in the form (X, ).
Moreover the warning tells you what to do to transform to the form you need: y.values.ravel().
Use Y = df[[single_column]].values.ravel() solves DataConversionWarning for me.