using python to calculate Vector Projection

Is there an easier command to compute vector projection? I am instead using the following: Answer Maybe this is what you really want: This should give the projection of vector x onto vector y – see Alternatively, if you want to compute the projection of y onto x, then replace y with x in the denominator (norm) of the above equation. EDIT: As @VaidAbhishek commented, the above formula is for the scalar projection. To obtain vector projection multiply scalar projection by a unit vector in the direction of the vector onto which the first vector is projected. The formula

How to move a column in a pandas dataframe

I want to take a column indexed ‘length’ and make it my second column. It currently exists as the 5th column. I have tried: I see the following error: TypeError: must be str, not list I’m not sure how to interpret this error because it actually should be a list, right? Also, is there a general method to move any column by label to a specified position? My columns only have one level, i.e. no MultiIndex involved. Answer Correcting your error I’m not sure how to interpret this error because it actually should be a list, right? No: colnames[0] and

loading EMNIST-letters dataset

I have been trying to find a way to load the EMNIST-letters dataset but without much success. I have found interesting stuff in the structure and can’t wrap my head around what is happening. Here is what I mean: I downloaded the .mat format in here I can load the data using it is a dictionnary with the keys as follow: the only key with interest is dataset, which I havent been able to gather data from. printing the shape of it give this: I dug deeper and deeper to find a shape that looks somewhat like a real dataset

Passing C++ vector to Numpy through Cython without copying and taking care of memory management automatically

Dealing with processing large matrices (NxM with 1K <= N <= 20K & 10K <= M <= 200K), I often need to pass Numpy matrices to C++ through Cython to get the job done and this works as expected & without copying. However, there are times when I need to initiate and preprocess a matrix in C++ and pass it to Numpy (Python 3.6). Let’s assume the matrices are linearized (so the size is N*M and it’s a 1D matrix – col/row major doesn’t matter here). Following the information in here: exposing C-computed arrays in Python without data copies &

Convert a 2d matrix to a 3d one hot matrix numpy

I have np matrix and I want to convert it to a 3d array with one hot encoding of the elements as third dimension. Is there a way to do with without looping over each row eg should be made into Answer Approach #1 Here’s a cheeky one-liner that abuses broadcasted comparison – Sample run – For 0-based indexing, it would be – If the one-hot enconding is to cover for the range of values ranging from the minimum to the maximum values, then offset by the minimum value and then feed it to the proposed method for 0-based indexing.

Type hinting / annotation (PEP 484) for numpy.ndarray

Has anyone implemented type hinting for the specific numpy.ndarray class? Right now, I’m using typing.Any, but it would be nice to have something more specific. For instance if the NumPy people added a type alias for their array_like object class. Better yet, implement support at the dtype level, so that other objects would be supported, as well as ufunc. Answer It looks like typing module was developed at: The main numpy repository is at Python bugs and commits can be tracked at The usual way of adding a feature is to fork the main repository, develop the

AttributeError: ‘module’ object has no attribute ‘percentile’

I use this function to calculate percentile from here: But I get this error : I also tried but it didn’t I got the same error. my numpy version is 1.3.0 I tried to upgrade but it seems like it won’t I used : [sudo pip install –upgrade scipy][2] but I found that there’s no upgrade. my ubuntu version 9.10 my python version is : 2.6.4 i also tried to go arround the numpy.percentile module and I found this here: but when I tried to find 0.5 percentile manually I found 5 Can anyone help explain to me why this

sliding window of M-by-N shape numpy.ndarray

I have a numpy array of shape (6,2) I need a sliding window with step size 1 and window size 3 likes this: I’m looking for a numpy solution. If your solution could parametrize the the shape of the original array as well as the window size and step size, that’d great. I found this related answer Using strides for an efficient moving average filter but I don’t see how to specify the stepsize there and how to collapse the window from the 3d to a continuous 2d array. Also this Rolling or sliding window iterator in Python but that’s

NumPy: function for simultaneous max() and min()

numpy.amax() will find the max value in an array, and numpy.amin() does the same for the min value. If I want to find both max and min, I have to call both functions, which requires passing over the (very big) array twice, which seems slow. Is there a function in the numpy API that finds both max and min with only a single pass through the data? Answer Is there a function in the numpy API that finds both max and min with only a single pass through the data? No. At the time of this writing, there is no