Python
Fundamentals
07/23/2025 09:37 PM 1
Agenda
• Recap
• Important Libiraries
• Numpy
• Matplotlib
• Pandas
• Seaborn
• Scipy
2
NumPy
• It’s the main library in python that deals with scientific computing, it
provides high performance multi-dimensional array
• So mainly it deals with arrays and metrices
1. How to create an array
• First: import the library
• np.array which takes the numbers
• Ex: np.array([1,2,3,4])
Getting the shape and type
• Arrays can be one dimensional or multidimensional so in order to
know that to know that you have to get the shape
• Array. Shape
• Type(array)
• This is for getting the type of an array
Change the values in the array
• Ex: array = np.array([1,2,3,4])
• The index of the values starts from the zeroth element
• In order to access number 3 for example, then it is located at index 2
• To change it: array[2] = 7
• The new array now is [1,2,7,4]
Multi dimensional array
• Multi dimensional array means that it has more than one row and
Colum
• To create it: np.array([ [10,20,30,40], [50,60,70,80] ])
• Now it is a 2d array
• To get the shape: array. Shape
• (2, 4) which means 2 rows and 4 columns
Indexing and slicing
• Ex: array = np.array([[1,2,3], [7,8,9], [3,5,7]])
• For slicing: array[rows , columns] so
• array[0:2 , 1:3] ??
• Note: the first one is included, the second is excluded
Creating automatic arrays
• 1. zeros array
• 2. ones array
• 3. identity array
• 4. constant array
• 5. random array
Zeros & ones & constant
• Zeros: np.zeros((shape of the array))
• Ones: NP.ONES((SHAPE OF THE ARRAY))
• Constant number: NP.FULL((SHAPE OF THE ARRAY),
(THE NUMBER) )
Identity Metrix
• To create it: np.eye(the number of ones)
Random Metrix
• The most important one for machine and deep learning.
• np.random.random((shape of the array))
• The numbers are randomly chosen and is between 0 and 1
• Used for initializing the weights in ml & dl
Argmax
• Argmax is a method that brings you the index of the highest values
• Ex: arr = np.array([0.2, 0.4, 0.8])
print(np.argmax (arr) ) index 2 is the output
Note: this function is used mostly in deep learning (activation function
outputs)
Arithmetic operations
• Np.add (x, y) where x and y are arrays
• Np.subtract (x, y)
• Np.multiply (x, y)
• Np.divide (x, y)
• Np.sqrt (x)
• Np.sin (x)
• Np.cos (x)
2. matplotlib
• Used for data visualization and plotting graphs
•
Steps for data visualization
• 1. create the data (array of x & array of y)
• 2. plot the graph
• 3. give title
• 4. label the axis
• 5. name each plot
• 6. show the graph
Methods of matplotlib
• 1. plt.plot
• 2. plt.scatter
• 3. plt.title
• 4. plt.legend
• 5. plt.xlable
• 6. plt.ylable
• 7. plt.subplot
• 8. plt.show
PLOT & SCATTER & TITLE
Legends & subplot
subplots
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(1, 10, 20)
y = np.linspace(2, 30, 20)
z = np.array([1,3,4,2,2,2,2,2,5,6,7,8,9,0,9,9,8,7,8,7])
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(15,15))
ax1.plot(x, y, color = 'r')
ax2.plot(x, z)
ax4.plot(x, y)
ax3.plot(x, z)
plt.show()
3. pandas
• Excel sheet for python.
• The most essential library for ml because it is where the data gets pre
processed.
• Clean the data by doing things like removing missing values and
filtering rows or columns by some criteria.
Primary Components of pandas
• 1. Series
• 2. Data frame
Creating data frame from
scratch
output
Index of the data frame
• The Index of this Data Frame was given to us on creation as the
numbers 0-3, but we could also create our own when we initialize
the Data Frame.
Access a specific index
Reading files with pandas
• Here comes the most important part in pandas
• 60% of The data which we are going to use through out the course are
coming from an excel sheet
• So in order to read it use the method pd.readcsv(‘path’)
Convert the data frame to an
array
• Dataframe.values
Any Questions ?
30
Thank You ♥♥
31