Module 2: Python Libraries for AI
20 Slides with Descriptions, Examples
& Outputs
Introduction
• Python libraries provide powerful tools for AI.
• We focus on NumPy, Pandas, Matplotlib,
Seaborn, Scikit-learn.
NumPy - Arrays
• NumPy provides efficient arrays.
• Example:
• import numpy as np
• arr = [Link]([1,2,3])
• print(arr)
• Output:
• [1 2 3]
NumPy - Array Operations
• Supports element-wise operations.
• Example:
• arr = [Link]([1,2,3])
• print(arr*2)
• Output:
• [2 4 6]
NumPy - Matrix
• Matrices using 2D arrays.
• Example:
• mat = [Link]([[1,2],[3,4]])
• print(mat)
• Output:
• [[1 2]
• [3 4]]
NumPy - Matrix Multiplication
• Matrix multiplication using dot.
• Example:
• a=[Link]([[1,2],[3,4]])
• b=[Link]([[5,6],[7,8]])
• print([Link](a,b))
• Output:
• [[19 22]
Pandas - DataFrame
• DataFrames store tabular data.
• Example:
• import pandas as pd
• df = [Link]({'A':[1,2],'B':[3,4]})
• print(df)
• Output:
• A B
Pandas - CSV Handling
• Read CSV files.
• Example:
• df=pd.read_csv('[Link]')
• print([Link]())
• Output:
• Displays top 5 rows.
Pandas - Excel Handling
• Read Excel files.
• Example:
• df=pd.read_excel('[Link]')
• print([Link]())
• Output:
• Displays top 5 rows.
Pandas - Data Cleaning
• Handle missing values.
• Example:
• [Link](0)
• Output:
• NaN replaced with 0.
Pandas - Data Selection
• Select rows/columns.
• Example:
• print(df['A'])
• Output:
• Column A values.
Matplotlib - Line Plot
• Plotting data.
• Example:
• import [Link] as plt
• [Link]([1,2,3],[4,5,6])
• [Link]()
• Output:
• Line graph.
Matplotlib - Bar Plot
• Bar chart.
• Example:
• [Link](['A','B'],[10,20])
• [Link]()
• Output:
• Bar graph.
Matplotlib - Histogram
• Histogram visualization.
• Example:
• [Link]([1,2,2,3,3,3,4])
• [Link]()
• Output:
• Histogram.
Seaborn - Scatterplot
• Seaborn scatterplot.
• Example:
• import seaborn as sns
• [Link](x=[1,2,3],y=[4,5,6])
• Output:
• Scatter plot.
Seaborn - Heatmap
• Heatmap visualization.
• Example:
• import numpy as np
• import seaborn as sns
• mat=[Link](3,3)
• [Link](mat)
• Output:
Scikit-learn - Introduction
• Scikit-learn provides ML models and
preprocessing tools.
Scikit-learn - Train/Test Split
• Split data.
• Example:
• from sklearn.model_selection import
train_test_split
• X_train,X_test,y_train,y_test=train_test_split([
1,2,3,4],[0,1,0,1],test_size=0.5)
• Output:
Scikit-learn - Linear Regression
• Linear regression example.
• Example:
• from sklearn.linear_model import
LinearRegression
• model=LinearRegression()
• X=[[1],[2],[3]]
• y=[2,4,6]
• [Link](X,y)
Scikit-learn - Classification
• Classification example.
• Example:
• from [Link] import
KNeighborsClassifier
• model=KNeighborsClassifier()
• X=[[0],[1],[2],[3]]
• y=[0,0,1,1]
• [Link](X,y)
Lab Exercise
• Task: Import dataset, clean data, and visualize
graphs.
• Steps:
• 1. Import CSV using Pandas
• 2. Clean missing values
• 3. Plot bar/line graphs using
Matplotlib/Seaborn