Understand how to learn and use NumPy.
Please watch the following video to learn more about Numpy
and understand how it is related to pandas
.
An array is a data structure that defines the NumPy library1. Similar to a list, a NumPy array contains variables that can be indexed in various ways. However, unlike a list, the elements are all of the same type, referred to as the array dtype. To create a NumPy array, we first create a list (using []
) and then convert the list into a NumPy array as shown below.
import numpy as np
#initialize a numpy array
a = np.array([1, 2, 3, 4, 5, 6])
Similar to Python lists, we can access elements of a NumPy array using indexes. To print the first element of a
, we use the following array indexing which will return 1.
import numpy as np
#initialize a numpy array
a = np.array([1, 2, 3, 4, 5, 6])
print(a[0])
To initialize a matrix (or a multidimensional ndarray), we can use nested lists (using [[..],[..]]
).
import numpy as np
#initialize a multidimensional numpy array
a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
print(a.shape)
To read more about NumPy arrays, please refer to the official documentation which you can find here.
INTRODUCTION TO NUMPY
module in Code Academy which you can find here.1 https://numpy.org/doc/stable/user/absolute_beginners.html
2 https://www.codecademy.com/learn/intro-statistics-numpy/modules/dspath-intro-numpy
We will recap today's concepts and discuss the assignments at the Q&A, see you at 4pm!