You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
NumPy is the fundamental package for scientific computing with Python.
This cheat sheet acts as a intro to Python for data science. Contact me here for typos or suggestions, and - of course - fork and tune it to your taste!
One of the most commonly used functions of NumPy are NumPy arrays: The essential difference between lists and NumPy arrays is functionality and speed. lists give you basic operation, but NumPy adds FFTs, convolutions, fast searching, basic statistics, linear algebra, histograms, etc.
The most important difference for data science is the ability to do element-wise calculations with NumPy arrays.
axis 0 always refers to row axis 1 always refers to column
Remember: NumPy array operations work element-wise.
Example
# If a 1d array is added to a 2d array (or the other way), NumPy# chooses the array with smaller dimension and adds it to the one# with bigger dimensiona=np.array([1, 2, 3])
b=np.array([(1, 2, 3), (4, 5, 6)])
print(np.add(a, b))
>>> [[246]
[579]]
b=np.array([(1, 2, 3), (4, 5, 6)])
# The index *before* the comma refers to *rows*,# the index *after* the comma refers to *columns*print(b[0:1, 2])
>>> [3]
print(b[:len(b), 2])
>>> [36]
print(b[0, :])
>>> [123]
print(b[0, 2:])
>>> [3]
print(b[:, 0])
>>> [14]
c=np.array([(1, 2, 3), (4, 5, 6)])
d=c[1:2, 0:2]
print(d)
>>> [[45]]
Tricks
This is a growing list of examples. Know a good trick? Let me know here or fork it and create a pull request.
boolean indexing (available as separate .py file here
# Index trick when working with two np-arraysa=np.array([1,2,3,6,1,4,1])
b=np.array([5,6,7,8,3,1,2])
# Only saves a at index where b == 1other_a=a[b==1]
#Saves every spot in a except at index where b != 1other_other_a=a[b!=1]