0.1 Modules and Libraries
First, what are modules and libraries?
Definition 0.1.1
Modules in Python are files that consists of definitions and statements for organization and reusability. Libraries are collection of modules and pre-written packages that can later be imported and distributed.
One of the reason why Python is so popular in ML and data science is because of the extensive libraries. Having the pre-written libraries specifically for ML and tensors like PyTorch simply makes our lives easier.
0.1.1 NumPy
NumPy is a Python package that helps handle
multidimensional arrays. Let’s get started by importing NumPy to our
code. We could import it as whatever alias we want, but np is the standard for
ML, so let’s stick with that.
Assuming that you have read the previous note or
have strong foundations in linear algebra, we can implement the
mathematical properties in Python with NumPy. Below is an example of
how we can define vectors in different dimensions. Note that
.ndim can
be used to find the dimension of a vector.
1arr_1d = np.array([1, 2, 3]) 2arr_2d = np.array([[1, 2, 3], 3 [4, 5, 6]]) 4arr_3d = np.array([[[1, 2], [3, 4]], 5 [[5, 6], [7, 8]], 6 [[9, 10], [11, 12]]]) 7 8print(f"Dimension: {arr_1d.ndim}, Shape: {arr_1d.shape}") 9print(f"Dimension: {arr_2d.ndim}, Shape: {arr_2d.shape}") 10print(f"Dimension: {arr_3d.ndim}, Shape: {arr_3d.shape}")
Dimension: 1, Shape: (3,) Dimension: 2, Shape: (2, 3) Dimension: 3, Shape: (3, 2, 2)
A simple way to understand multidimensional array
in NumPy is stacking previous layers. For instance, arr_2d has the shape
\((2, 3)\). We can think of it as
stacking two 1D array with three elements. Similarly, arr_3d has the shape
\((3, 2, 2)\). We could think of
it as stacking three layers of 2D arrays that consists of two 1D
arrays. It is quite hard to visualize higher dimensional arrays, so
let’s take a look at different things that NumPy can do.
Below is an example where we utilize arange, reshape, dtype, and slicing techniques in NumPy. Let’s create an array first.
1# Making an array with numbers 0-17 and 2# rerranging it to shape (3, 3, 2) 3a = np.arange(18).reshape(3, 3, 2) 4a
array([[[ 0, 1], [ 2, 3], [ 4, 5]], [[ 6, 7], [ 8, 9], [10, 11]], [[12, 13], [14, 15], [16, 17]]])
We can also find the data type with the attribute
.dtype.
This means that the individual elements in array
a are 8
bytes integers. Just as we index and slice 1D array, we could do the
same with NumPy. Consider the following example for a matrix.
1a = np.arange(9).reshape(3, 3) 2print(f"{a}\n") 3 4# First row, second column element 5print(a[0, 1]) 6# Entire second row 7print(a[1, :]) 8# Entire third column 9print(a[:, 2])
[[0 1 2] [3 4 5] [6 7 8]] 1 [3 4 5] [2 5 8]
This was the basic introduction to NumPy. Please check out the documentation for further information.
0.1.2 Matplotlib
Do you want to visualize something with Python? Matplotlib is here for your need! Matplotlib is a Python library for visualization. Let’s first import the library to take a look at an example.
If you are using Jupyter Notebook like the
examples in these notes, it is highly recommended to keep
%matplotlib inline to make our lives easier
by plotting directly in the notebook without an external window.
Although Matplotlib is an essential library for visualizing model
performance and analyzing, let’s stick with basic plotting for now as
I believe more information would be an overkill for this note.
For this brief introduction, let’s plot Sigmoid function, which we will discuss in later notes.
1# Define domain and the function 2x = np.linspace(-10, 10, 100) 3y = 1 / (1 + np.exp(-x)) 4 5plt.figure() 6plt.plot(x, y, label=’Sigmoid’) 7plt.grid(True) 8plt.xlabel(’x’) 9plt.ylabel(’sigmoid(x)’) 10plt.title(’Sigmoid Function’) 11plt.legend() 12plt.show()
![]()
I know this intro was quite brief, but we will continue using amazing features in Matplotlib! For more information, please visit the official tutorial page.
In the next note, we will discuss foundational terms and concepts in ML. It will also include a high level introduction to large language model (LLM). Understanding the high level overview of how LLMs operate under the hood really helped me grasp the intuition. Without further ado, let’s get started!