Numpy arrays#

Another very popular library is NumPy, which allows users to work with vectors and matrices and perform mathematical operations with them. It offers comprehensive mathematical functions, random number generations, data distributions and more.

For more information on what is an array and why NumPy is so widely used, read here.

Create a NumPy array#

Let’s now try to see how to create arrays!

import numpy as np
from math import pi

# create a one-dimensional array
array_1d = np.array([pi, pi*2, pi*4])
array_1d
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[1], line 1
----> 1 import numpy as np
      2 from math import pi
      4 # create a one-dimensional array

ModuleNotFoundError: No module named 'numpy'

Investigate the array

# creck the size of the array
array_1d.size
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[2], line 2
      1 # creck the size of the array
----> 2 array_1d.size

NameError: name 'array_1d' is not defined
# creck the shape of the array
array_1d.shape
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[3], line 2
      1 # creck the shape of the array
----> 2 array_1d.shape

NameError: name 'array_1d' is not defined
# creck the dimension of the array
array_1d.ndim
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[4], line 2
      1 # creck the dimension of the array
----> 2 array_1d.ndim

NameError: name 'array_1d' is not defined
# creck the type of the array
array_1d.dtype
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[5], line 2
      1 # creck the type of the array
----> 2 array_1d.dtype

NameError: name 'array_1d' is not defined
# create a one-dimensional array
array_2d = np.array([[pi, pi*2, pi*4], [5.9, 7.0, 3], [pi*1.4, pi*0.2, pi*6.4]])
array_2d
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[6], line 2
      1 # create a one-dimensional array
----> 2 array_2d = np.array([[pi, pi*2, pi*4], [5.9, 7.0, 3], [pi*1.4, pi*0.2, pi*6.4]])
      3 array_2d

NameError: name 'np' is not defined
print('Size of the array: ', array_2d.size)
print('Shape of the array: ', array_2d.shape)
print('Dimensionality of the array: ', array_2d.ndim)
print('Type of the array: ', array_2d.dtype)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[7], line 1
----> 1 print('Size of the array: ', array_2d.size)
      2 print('Shape of the array: ', array_2d.shape)
      3 print('Dimensionality of the array: ', array_2d.ndim)

NameError: name 'array_2d' is not defined

Create an array according to a distribution#

np.random.seed(42)  # fix random seed to get the same set of values every time
uniform = np.random.uniform(1, 3, 200) # arguments are the lower boundary, upper boundary, output shape
uniform
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[8], line 1
----> 1 np.random.seed(42)  # fix random seed to get the same set of values every time
      2 uniform = np.random.uniform(1, 3, 200) # arguments are the lower boundary, upper boundary, output shape
      3 uniform

NameError: name 'np' is not defined
%matplotlib inline
import matplotlib.pyplot as plt

plt.hist(uniform, bins=50)
plt.show()
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[9], line 1
----> 1 get_ipython().run_line_magic('matplotlib', 'inline')
      2 import matplotlib.pyplot as plt
      4 plt.hist(uniform, bins=50)

File ~\AppData\Local\miniconda3\envs\dig4bio\Lib\site-packages\IPython\core\interactiveshell.py:2480, in InteractiveShell.run_line_magic(self, magic_name, line, _stack_depth)
   2478     kwargs['local_ns'] = self.get_local_scope(stack_depth)
   2479 with self.builtin_trap:
-> 2480     result = fn(*args, **kwargs)
   2482 # The code below prevents the output from being displayed
   2483 # when using magics with decorator @output_can_be_silenced
   2484 # when the last Python token in the expression is a ';'.
   2485 if getattr(fn, magic.MAGIC_OUTPUT_CAN_BE_SILENCED, False):

File ~\AppData\Local\miniconda3\envs\dig4bio\Lib\site-packages\IPython\core\magics\pylab.py:103, in PylabMagics.matplotlib(self, line)
     98     print(
     99         "Available matplotlib backends: %s"
    100         % _list_matplotlib_backends_and_gui_loops()
    101     )
    102 else:
--> 103     gui, backend = self.shell.enable_matplotlib(args.gui)
    104     self._show_matplotlib_backend(args.gui, backend)

File ~\AppData\Local\miniconda3\envs\dig4bio\Lib\site-packages\IPython\core\interactiveshell.py:3665, in InteractiveShell.enable_matplotlib(self, gui)
   3662     import matplotlib_inline.backend_inline
   3664 from IPython.core import pylabtools as pt
-> 3665 gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select)
   3667 if gui != None:
   3668     # If we have our first gui selection, store it
   3669     if self.pylab_gui_select is None:

File ~\AppData\Local\miniconda3\envs\dig4bio\Lib\site-packages\IPython\core\pylabtools.py:338, in find_gui_and_backend(gui, gui_select)
    321 def find_gui_and_backend(gui=None, gui_select=None):
    322     """Given a gui string return the gui and mpl backend.
    323 
    324     Parameters
   (...)
    335     'WXAgg','Qt4Agg','module://matplotlib_inline.backend_inline','agg').
    336     """
--> 338     import matplotlib
    340     if _matplotlib_manages_backends():
    341         backend_registry = matplotlib.backends.registry.backend_registry

ModuleNotFoundError: No module named 'matplotlib'
# create a normal distribution
normal = np.random.normal(1, 3, 200) # arguments are the lower boundary, upper boundary, output shape
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[10], line 2
      1 # create a normal distribution
----> 2 normal = np.random.normal(1, 3, 200) # arguments are the lower boundary, upper boundary, output shape

NameError: name 'np' is not defined
plt.hist(normal, bins=50)
plt.show()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[11], line 1
----> 1 plt.hist(normal, bins=50)
      2 plt.show()

NameError: name 'plt' is not defined

Operations#

uniform = np.random.uniform(0, 1, 20)
normal = np.random.normal(0, 1, 20)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[12], line 1
----> 1 uniform = np.random.uniform(0, 1, 20)
      2 normal = np.random.normal(0, 1, 20)

NameError: name 'np' is not defined
# sum
print(uniform + normal)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[13], line 2
      1 # sum
----> 2 print(uniform + normal)

NameError: name 'uniform' is not defined
# product
print(uniform * normal)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[14], line 2
      1 # product
----> 2 print(uniform * normal)

NameError: name 'uniform' is not defined
# difference
print(uniform - normal)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[15], line 2
      1 # difference
----> 2 print(uniform - normal)

NameError: name 'uniform' is not defined
# ratio
print(uniform / normal)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[16], line 2
      1 # ratio
----> 2 print(uniform / normal)

NameError: name 'uniform' is not defined
# inner product of vectors
print(np.dot(uniform, normal))
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[17], line 2
      1 # inner product of vectors
----> 2 print(np.dot(uniform, normal))

NameError: name 'np' is not defined
# matrix multiplication
matrix_1 = np.array([[3, 8], [2, 9]])
matrix_2 = np.array([[4, 5], [7, 2]])
np.dot(matrix_1, matrix_2)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[18], line 2
      1 # matrix multiplication
----> 2 matrix_1 = np.array([[3, 8], [2, 9]])
      3 matrix_2 = np.array([[4, 5], [7, 2]])
      4 np.dot(matrix_1, matrix_2)

NameError: name 'np' is not defined
# More on the sum
print(matrix_1)
print('Summing over rows', np.sum(matrix_1, axis=0))
print('Summing over columns', np.sum(matrix_1, axis=1))
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[19], line 2
      1 # More on the sum
----> 2 print(matrix_1)
      3 print('Summing over rows', np.sum(matrix_1, axis=0))
      4 print('Summing over columns', np.sum(matrix_1, axis=1))

NameError: name 'matrix_1' is not defined
# square root 
np.sqrt(matrix_1)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[20], line 2
      1 # square root 
----> 2 np.sqrt(matrix_1)

NameError: name 'np' is not defined
# exponential
np.exp(matrix_1)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[21], line 2
      1 # exponential
----> 2 np.exp(matrix_1)

NameError: name 'np' is not defined
# matrix to the power of 2
matrix_1**2
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[22], line 2
      1 # matrix to the power of 2
----> 2 matrix_1**2

NameError: name 'matrix_1' is not defined
# flatten array
print(matrix_1)
matrix_1.flatten()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[23], line 2
      1 # flatten array
----> 2 print(matrix_1)
      3 matrix_1.flatten()

NameError: name 'matrix_1' is not defined
# indexing a matrix
matrix_1[0, 0]
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[24], line 2
      1 # indexing a matrix
----> 2 matrix_1[0, 0]

NameError: name 'matrix_1' is not defined
# transposing a matrix
matrix_1.T
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[25], line 2
      1 # transposing a matrix
----> 2 matrix_1.T

NameError: name 'matrix_1' is not defined