How do I calculate percentiles with python/numpy?

Is there a convenient way to calculate percentiles for a sequence or single-dimensional numpy array? I am looking for something similar to Excel’s percentile function. I looked in NumPy’s statistics reference, and couldn’t find this. All I could find is the median (50th percentile), but not something more specific. 12 Answers 12

Better way to shuffle two numpy arrays in unison

I have two numpy arrays of different shapes, but with the same length (leading dimension). I want to shuffle each of them, such that corresponding elements continue to correspond — i.e. shuffle them in unison with respect to their leading indices. This code works, and illustrates my goals: def shuffle_in_unison(a, b): assert len(a) == len(b) … Read more

How does numpy.newaxis work and when to use it?

When I try numpy.newaxis the result gives me a 2-d plot frame with x-axis from 0 to 1. However, when I try using numpy.newaxis to slice a vector, vector[0:4,] [ 0.04965172 0.04979645 0.04994022 0.05008303] vector[:, np.newaxis][0:4,] [[ 0.04965172] [ 0.04979645] [ 0.04994022] [ 0.05008303]] Is it the same thing except that it changes a row … Read more

Concatenating two one-dimensional NumPy arrays

I have two simple one-dimensional arrays in NumPy. I should be able to concatenate them using numpy.concatenate. But I get this error for the code below: TypeError: only length-1 arrays can be converted to Python scalars Code import numpy a = numpy.array([1, 2, 3]) b = numpy.array([5, 6]) numpy.concatenate(a, b) Why? 6 Answers 6

How to convert a PIL Image into a numpy array?

Alright, I’m toying around with converting a PIL image object back and forth to a numpy array so I can do some faster pixel by pixel transformations than PIL’s PixelAccess object would allow. I’ve figured out how to place the pixel information in a useful 3D numpy array by way of: pic = Image.open(“foo.jpg”) pix … Read more

What is the purpose of meshgrid in Python / NumPy?

Can someone explain to me what is the purpose of meshgrid function in Numpy? I know it creates some kind of grid of coordinates for plotting, but I can’t really see the direct benefit of it. I am studying “Python Machine Learning” from Sebastian Raschka, and he is using it for plotting the decision borders. … Read more