Importing the NumPy module
import numpy as np
Populating Arrays
array = np.array([1.2, 2.4, 3.5, 4.7, 6.1, 7.2, 8.3, 9.5]) // create 1D array
matrix = np.array([[6, 5], [11, 7], [4, 8]]) //create 2D array
seq = np.arange(5, 12) // create an array consisting of integers 5, 6, 7, 8, 9, 10, 11 - the range is [a, b)
randints = np.random.randint(low=50, high=101, size=(6)) // create an array of 6 with random integers from 50 - 100
randfloats = np.random.random([6]) //default is to create random floats between 0 and 1
array_zeros = np.zeros([6]) // populate an array with 6 zeros
array_ones = np.ones([6]) // populate an array with 6 ones
Broadcasting:
A feature of NumPy that when two matrices are not the same size for the intended operation, it will expand the smaller operand to dimensions that can be operated on for linear algebra.
E.g. randfloats = np.random.random([6]) + 2.0
- broadcasting will expand 2.0 to [2.0, 2.0, 2.0, 2.0, 2.0, 2.0]