I’m using numpy.random.shuffle in order to compute a statistic on randomized columns of a 2D array. The Python code is as follows: The speed I get is something like this: 1 loops, best of 3: 391 ms per loop I tried to Cythonize this function but I wasn’t sure how to replace the call to np.random.shuffle and the function was
Tag: numpy
How to return 0 with divide by zero
I’m trying to perform an element wise divide in python, but if a zero is encountered, I need the quotient to just be zero. For example: I could always just use a for-loop through my data, but to really utilize numpy’s optimizations, I need the divide function to return 0 upon divide by zero errors instead of ignoring the error.
Display numpy array in a for loop using matplotlib imshow
I have a numpy array whose elements are updated in a for loop: I want to display the array at each iteration, but imshow() doesn’t work, it just displays the image once the loop terminates. ps. I’m using an Ipython notebook I found different things on the web but none of them work on my computer (for example I tried
Custom transformer for sklearn Pipeline that alters both X and y
I want to create my own transformer for use with the sklearn Pipeline. I am creating a class that implements both fit and transform methods. The purpose of the transformer will be to remove rows from the matrix that have more than a specified number of NaNs. The issue I am facing is how can I change both the X
python pandas flatten a dataframe to a list
I have a df like so: I want to flatten the df so it is one continuous list like so: [‘1/2/2014’, ‘a’, ‘6’, ‘z1’, ‘1/2/2014’, ‘a’, ‘3’, ‘z1′,’1/3/2014’, ‘c’, ‘1’, ‘x3’] I can loop through the rows and extend to a list, but is a much easier way to do it? Answer You can use .flatten() on the DataFrame converted
How to make the elements of a NumPy array property settable?
I have a property of a Python object that returns an array. Now, I can set the setter of that property such that the whole array is settable. However, I’m missing how to make the elements by themselves settable through the property. I would expect from a user perspective (given an empty SomeClass class): Now, suppose that SomeClass.array is a
Test if an array is broadcastable to a shape?
What is the best way to test whether an array can be broadcast to a given shape? The “pythonic” approach of trying doesn’t work for my case, because the intent is to have lazy evaluation of the operation. I’m asking how to implement is_broadcastable below: or better yet: Answer I really think you guys are over thinking this, why not
Extending numpy.digitize to multi-dimensional data
I have a set of large arrays (about 6 million elements each) that I want to basically perform a np.digitize but over multiple axes. I am looking for some suggestions on both how to effectively do this but also on how to store the results. I need all the indices (or all the values, or a mask) of array A
numpy genfromtxt collapse recarray when data file has only one row
I am using genfromtxt function to read data from a csv file. Then I can access the array columns with e.g.: which then returns a 1-dimensional vector. Only when the source file has exactly one data row, the array is collapsed – its shape==() and therefore the vector returned by data[“My column name”] is not a vector but just a
All possible points of (latitude,longitude) from latitude/longitude coordinates in separate arrays
I have latitude and longitude coordinates in two separate arrays: How can I easily find all possible points made up of these coordinates? I’ve been messing around with itertools.combinations: but this doesn’t work for me because the points (71,75) and (43,42) are latitude/latitude and longitude/longitude pairs. What I would like to have is this: The a and b arrays will