For now, you may consider NumPy as Python lists on steroids. You can use it to create multidimensional arrays and matrices.
The convention is to import it as follows:
import numpy as np
As you know, to create an list of numbers between 0 and 9 in Python, you use the following command:
x = range(10)
To convert that list into a NumPy array, you can write:
x = np.array(range(10))
And to make you life easier, there is a shorthand for the above command:
x = np.arange(10)
So far, we have been creating one dimensional array. However, there are ways to reshape the arrays. The reshape() method when applied on an array, it returns a reshaped version of it without changing the original object.
y = x.reshape(2,5)
To reshape the original object itself, then use resize() instead.
The above command create a 2-dimensional array of 2 rows and 5 columns. You can create arrays with as high dimensions as you want. See the command below for a 3*4*5 array.
y = np.arange(3*4*5).reshape(3,4,5)
Similar to reshape() and resize(), ravel() converts a multidimensional array into a one-dimensional array, while transpose() turns rows into columns and vice versa.
The mathematical operations '+', '-', '/' and '*' are applied elementwise.
x = np.arange(10) # To multiply each element of x by 10 y = x + 10 # To multiply each element of x by itself y = x + x
To do a Matrix Multiplication though:
# Create a 3 * 5 Matrix A = np.arange(15).reshape(3,5) # Create a 5 * 2 Matrix B = np.arange(10).reshape(5,2) # Dot product gives you a 3 * 2 Matrix y = y = np.dot(A, B)
Just like in lists, you can get parts of an array
For Python lists:
A = range(10) A[2:5] #=> [2, 3, 4]
For NumPy Arrays:
B = arange(10) B[2:5] #=> array([2, 3, 4])
However, you can set some elements of the array as follows:
B[2:5] = 007
But, you cannot do the same to lists!
A[2:5] = 007 #=> TypeError: can only assign an iterable
You can also access elements of the array using start, stop and a step:
x = np.arange(10) x[2:7:2] # array([2, 4, 6])
Or access specific elements, let's say elements 1, 5 and 6
x[[1,5,6]] # array([1, 5, 6])
For statisticians, here are some methods for analyzing your data
x = np.arange(5) + 1 x.mean() # 3.0 x.max() # 5 x.min() # 1 x.std() # 1.414
If you are having an array of elements that are either True or False.
x = np.array([True, False, True, True]) x.all() # Only True if all elements are True x.any() # Only True if any elements are True
If you are used to R Programming Language, you will not miss its way of accessing elements of array that meet a certain condition.
x = np.arange(10) x[x>4] # array([5, 6, 7, 8, 9]) x[x%2 == 1] # array([1, 3, 5, 7, 9])
Finally, there is a repeat() that repeats each element of the array n times.
x = np.array([1, 2]) x.repeat(3) # array([1, 1, 1, 2, 2, 2])
That's all folks!