NumPy is a Python library that provides ndarray
(a multi-dimensional array structure) and a wide range of functions to perform operations on ndarray
and mathematical operations. NumPy Documentation.
In [1]:
import numpy as np
In [2]:
my_vector = np.array([1, 2, 3, 4])
my_vector
Out[2]:
In [3]:
my_vector.shape
Out[3]:
In [4]:
my_vector.dtype
Out[4]:
In [5]:
my_matrix = np.array([[1, 2], [3, 4]])
my_matrix
Out[5]:
In [6]:
my_matrix.shape
Out[6]:
In [7]:
# Find the length of each element in bytes
my_matrix.itemsize
Out[7]:
In [8]:
my_matrix2 = np.array([[1, 2], [3, 4]], dtype=np.int8)
my_matrix2.itemsize
Out[8]:
In [9]:
# Create an uninitialised array of specified shape and dtype
np.empty(shape=(4,4),dtype=np.int8)
Out[9]:
In [10]:
np.zeros(4)
Out[10]:
In [11]:
np.zeros((4,4))
Out[11]:
In [12]:
np.zeros((4,4)) + 42
Out[12]:
In [13]:
# Create a new zero matrix of the same shape as another matrix.
np.zeros_like(my_matrix)
Out[13]:
In [14]:
np.ones(4)
Out[14]:
In [15]:
np.ones((4,4))
Out[15]:
In [16]:
# Similar to Python's built-in range() function
np.arange(start=0, stop=10, step=2)
Out[16]:
In [17]:
# Like arange() but instead of a step size, we specify the
# number of values that we need. It generates lineary-spaced
# numbers in the given interval
np.linspace(start=10, stop=20, num=5)
Out[17]:
In [18]:
# Generate numbers that are evenly spaced on a logarithmic scale
np.logspace(start=1, stop=2, num=10)
Out[18]:
In [19]:
arr1 = np.array([10, 87, 86, 5, 4, 38, 94, 76, 12, 17])
arr1
Out[19]:
In [20]:
arr1.max(), arr1.argmax()
Out[20]:
In [21]:
arr1.min(), arr1.argmin()
Out[21]:
In [22]:
arr1_copy = arr1.copy()
arr1_copy
Out[22]:
In [23]:
matrix1 = np.arange(1,26).reshape(5,5)
matrix1
Out[23]:
In [24]:
# Sum values in the matrix
matrix1.sum()
Out[24]:
In [25]:
# Sum values by column
matrix1.sum(0)
Out[25]:
In [26]:
prime_numbers = np.array([2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97])
prime_numbers
Out[26]:
In [27]:
prime_numbers.reshape(5, 5)
Out[27]:
In [28]:
np.eye(4)
Out[28]:
In [29]:
# Use NumPy to generate four random numbers between 0 and 1
np.random.rand(4)
Out[29]:
In [30]:
np.random.rand(4, 4)
Out[30]:
In [31]:
np.random.randint(1, 101)
Out[31]:
In [32]:
# Generates 10 random integers between 1 and 100
np.random.randint(1, 101, 10)
Out[32]:
In [33]:
# Generate four numbers from the normal distribution centred around zero
np.random.randn(4)
Out[33]:
In [34]:
np.random.randn(4, 4)
Out[34]:
In [35]:
arr2 = np.arange(0, 101, 10)
arr2
Out[35]:
In [36]:
arr2[2]
Out[36]:
In [37]:
# Use Python's slice notation to fetch elements from the array
arr2[3:6]
Out[37]:
In [38]:
arr2[3:]
Out[38]:
In [39]:
arr2[:4]
Out[39]:
In [40]:
# Boolean indexing
arr2[arr2 > 5]
Out[40]:
In [41]:
arr2 > 5
Out[41]:
In [42]:
arr_with_nans = np.array([np.nan, 1,2,np.nan,3,4,5])
arr_with_nans
Out[42]:
In [43]:
# Get an array where NaN elements are omitted
arr_with_nans[~np.isnan(arr_with_nans)]
Out[43]:
In [44]:
matrix2 = np.arange(1, 26).reshape(5,5)
matrix2
Out[44]:
In [45]:
matrix2[1]
Out[45]:
In [46]:
matrix2[1,2] # same as matrix[1][2]
Out[46]:
In [47]:
matrix2[1:4,1:4]
Out[47]:
In [48]:
# Use ellipsis to get elements from the third column
matrix2[...,2]
Out[48]:
In [49]:
# Fetch elements placed at corners of the 5x5 array
rows = np.array([[0,0],[4,4]])
cols = np.array([[0,4],[0,4]])
matrix2[rows, cols]
Out[49]:
In [50]:
arr3 = np.array([9, 4, 4])
arr3
Out[50]:
In [51]:
np.tile(arr3, (4, 1))
Out[51]:
In [52]:
np.tile(arr3, (5, 2))
Out[52]:
Broadcasting is Numpy's terminology for performing mathematical operations between arrays of different shapes. If certain assumptions hold, the smaller of the two arrays is said to be broadcast to the size of the larger array in order to make the two arrays compatible so element-to-element operations can be performed. Read more.
In [53]:
macro_nutrients = np.array([[0.3, 2.5, 3.5],
[2.9, 27.5, 0],
[0.4, 1.3, 23.9],
[14.4, 6, 2.3]])
calories_per_macro = np.array([9, 4, 4])
macro_nutrients * calories_per_macro
Out[53]:
In [54]:
arr4 = np.arange(0, 10)
arr4
Out[54]:
In [55]:
arr4[0:4] = 10
arr4
Out[55]:
In [56]:
arr5 = np.arange(0, 10)
arr6 = np.arange(10, 20)
In [57]:
arr5
Out[57]:
In [58]:
arr6
Out[58]:
In [59]:
arr5 + arr6
Out[59]:
In [60]:
arr6 - arr5
Out[60]:
In [61]:
arr5 * arr6
Out[61]:
In [62]:
arr5 + 10 # broadcasting
Out[62]:
In [63]:
arr6 - 10
Out[63]:
In [64]:
arr5 ** 2
Out[64]:
In [65]:
# NumPy generates a warning if we attempt to divide by zero.
arr5 / arr5
Out[65]:
In [66]:
1 / arr5
Out[66]:
A universal function (ufunc) is a mathematical function that operates on arrays in an element-by-element fashion supporting array broadcasting, type casting, and several other standard features. For more information read the ufunc documentation.
In [67]:
arr7 = np.array([2, 6, 7, 10, 45, 200])
arr7
Out[67]:
In [68]:
# Computes the square root of each element of the array
np.sqrt(arr7)
Out[68]:
In [69]:
np.exp(arr7)
Out[69]:
In [70]:
np.log(arr7)
Out[70]: