Tutorial 3: Introduction to Numpy
A tutorial of some commonly used function in Numpy
Estimated time: 15 minutes
Numpy
NumPy (Numerical Python) is an open source Python library that’s used in almost every field of science and engineering. It’s the universal standard for working with numerical data in Python, and it’s at the core of the scientific Python and PyData ecosystems. NumPy users include everyone from beginning coders to experienced researchers doing state-of-the-art scientific and industrial research and development. The NumPy API is used extensively in Pandas, SciPy, Matplotlib, scikit-learn, scikit-image and most other data science and scientific Python packages.
Basic of Numpy
NumPy’s main object is the homogeneous multidimensional array. It is a table of elements (usually numbers), all of the same type, indexed by a tuple of non-negative integers. In NumPy dimensions are called axes
.
NumPy’s array class is called ndarray
. It is also known by the alias array.
ndarray attributes |
Function |
---|---|
ndim |
Number of axes (dimensions) of the array. |
shape |
The dimensions of the array. For a matrix with n rows and m columns, shape will be (n,m) . |
size |
Total number of elements of the array. |
dtype |
Describing the type of the elements in the array. |
Let's look an example. Let's create a evenly spaced values from 0 to 15 with arange
and reshape to (3,5).
import numpy as np
a = np.arange(15).reshape(3, 5)
a
a.shape
a.ndim
a.shape
a.size
a.dtype
type(a)
Let's create an array with random number of ours.
b = np.array([(1.5,2,3), (4,5,6)])
b
The type of the array can also be explicitly specified at creation time.
c = np.array( [ [1,2], [3,4] ], dtype=complex )
c
Arrays of 0's and 1's
The function zeros
creates an array full of zeros, the function ones
creates an array full of ones, and the function empty
creates an array whose initial content is random and depends on the state of the memory. By default, the dtype of the created array is float64
. We could create random arrays with random.random()
.
one = np.ones((3,4))
one
one_int = np.ones((3,4),dtype=int)
one_int
zero = np.zeros((3,4))
zero
empty = np.empty((3,4))
empty #result may differ
rand = np.random.random((3,4))
rand
x = np.array([10,20,30,40])
y = np.array([1,2,4,5])
z = x-y
z
y**2
y*2
x<30
The product operator *
operates elementwise in NumPy arrays. The matrix product can be performed using the @
operator (in python >=3.5
) or the dot
function or method
f = np.array([[1,2],[3,4]])
g = np.array([[3,4],[5,6]])
f*g
f@g
f.dot(g)
a = np.array([1,2,4,6])
a[0]
a[1:3]
Multidimensional arrays can have one index per axis. These indices are given in a tuple separated by commas:
b = np.array([[1,2,3,4],[3,4,5,6],[7,8,9,10],[8,9,10,11]])
b[0][0]
b[0,0]
b[0:4,0]
b[-1]
for row in b:
print(row)
However, if one wants to perform an operation on each element in the array, one can use the flat
attribute which is an iterator over all the elements of the array:
for elem in b.flat:
print(elem)
b = np.array([[3,4,5,6],[89,2,4,39]])
b.max()
b.min()
b.mean()
b.sum()
b.std()
c = np.array([[1,2,3,4],[5,6,7,5]])
c
c.T
c.reshape(1,8)
b = np.zeros((2,3))
c = np.array([[4,5,6],[3,2,1]])
np.hstack((b,c))
np.vstack((b,c))
m = np.array([[[1,2,3],[5,6,7]],[[3,4,5],[7,8,9]]])
m
m*2
m+2
np.zeros((2,3,2))