NumPy Exercises for Data Analysis in Python (machinelearningplus.com)
I compiled a list of numpy practice exercises related to data analysis. Might be helpful if you want to practice some data munging problems. Feedback welcome!
https://www.machinelearningplus.com/101-numpy-exercises-python/
33 comments
[ 1.7 ms ] story [ 81.2 ms ] threadhttp://rosalind.info/problems/locations/
For question 48 it might be simpler to just write
instead of using argsort() and then using fancy indexing. Better yet, use which scales linearly with the size of the array.Also, the one-hot encoding puzzle (51) would be more efficiently solved using
In general, `for` loops over NumPy arrays should be avoided where at all possible.I've found the following to be quite helpful but would love to know if anyone knows of other resources in a similar vein: https://pandas.pydata.org/pandas-docs/stable/cookbook.html
There's also pandas_exercises by Guilherme Samora (https://github.com/guipsamora/pandas_exercises) which is very good - it's split across multiple notebooks and is more extensive than my repo.
For #3, you can make a boolean array with np.ones/np.zeros with the same dtype arg, saves a little bit of space.
ie np.ones((3,3), dtype=bool)
For #14, you can make use of the same compound boolean statements as you can in pandas to make it a bit simpler.
ie a[(a > 5) & (a < 10)]
For #15, this is a built in numpy function.
np.maximum(a,b).
That's as far as I've made it, but I'm really enjoying them.
However, for No. 15, that is not the point of the exercise.
For example from std::vector::insert [1]:
[1][http://en.cppreference.com/w/cpp/container/vector/insert]edit: formatting
For example, np.einsum for all its greatness in the past wasn't faster than np.tensordot, but it was more flexible. One can tell einsum to try and use the same underlying BLAS functions that tensordot uses (which can parallelise the computation) if applicable, and it will likely be default for einsum to perform this optimisation automatically once the devs iron out some bugs. But for now, it pays to know how the two methods are different.
One downside is that unless you're doing BLAS-style operations, writing non-trivial transformations of StaticArrays always seems to require generated functions.
Anyway, I think this is a feature that numpy doesn't provide.
[1] https://github.com/JuliaArrays/StaticArrays.jl
np.unique??
the suggestion that jumps out is to just learn about algorithms.
(Also numpy has some really nice features over Matlab, like [None,:] broadcasting and being able to index a parenthesized expression or function output without naming it. Ok, the latter is not really a feature, more of an example of how Matlab is broken as a language)