ECE3025 AI With Python
ECE3025 AI With Python
Sekar
Associate Professor
Department of Electronics and Communication Engineering
1
2
3
There are TWELVE program outcomes, which are the course requirements that are met in the particular
program outcomes.
4
5
6
7
There are FOUR program-specific outcomes, which are the course requirements that are met in the
particular program-specific outcomes.
8
TABLE 1: COURSE OUTCOMES
CO CO Expected
Number BLOOMS LEVEL
1 Explain the basic principles of AI and the Python programming language Understand
2 Understand the mathematical formulation and computational models of Understand
classification and regression using supervised learning and predictive
analytics with ensemble learning.
TensorFlow
Flexible Architecture: TensorFlow allows you to deploy computations to one or more CPUs or GPUs in a
desktop, server, or mobile device with a single API.
Wide Range of Applications: TensorFlow is versatile and can be used for various tasks such as speech
recognition, image recognition, neural networks, and more.
TensorFlow Serving: Ideal for deploying machine learning models in production environments.
Keras
User-Friendly: Keras is designed with user experience in mind, offering a high-level, user-friendly interface for
building and training deep learning models.
Runs on Top of TensorFlow: Keras originally could run on various backends like Theano, TensorFlow, and
CNTK(Microsoft Cognitive Toolkit), but it has been tightly integrated with TensorFlow as its official high-level API.
Modularity and Extensibility: Keras is modular, allowing users to create neural layers, cost functions,
optimizers, and activation functions easily.
Some important Libraries
PyTorch: Developed by Facebook's AI Research lab, PyTorch is another open-source machine learning framework
that’s particularly popular for its dynamic computational graph and ease of use.
Scikit-learn: A versatile library for machine learning in Python, Scikit-learn is ideal for classical machine learning
algorithms like regression, clustering, and more. It’s not as powerful for deep learning but is perfect for many
general-purpose ML tasks.
MXNet: An open-source deep learning framework developed by the Apache Software Foundation, MXNet supports
flexible and efficient operations, making it suitable for both research and production environments.
Mathematics for Machine Learning
Linear Algebra: Vectors, matrices, and operations like dot products and eigenvectors.
Probability and Statistics: Understanding distributions, probability theory, and statistical measures (mean,
variance, etc.).
Calculus: Differentiation and integration, particularly in the context of optimization problems.
Problem-Solving Skills
Algorithm Design: Structuring and breaking down complex problems into smaller, manageable tasks.
Debugging and Testing: Writing test cases, using debugging tools, and ensuring code reliability.
Module-1 Introduction to Artificial Intelligence Classifier
AI Vs ML Vs DL (probabilistic
AI Applications machine
learning)
Introduction Algorithms algorithm)
to AI Logistic
Classifier/ regression
Regression Classifier
Support Vector
Machine(SVM))
Binarization Confusion
Matrix
Preprocessing Mean removal
Data Analysis
ScNormalization
Regression
Types of supervised Machine learning Algorithms:
Classification
Unsupervised Learning
Unsupervised Learning
Types of Unsupervised Machine learning Algorithms:
Unsupervised learning Algorithms
Supervised Vs Unsupervised learning
Supervised Learning Unsupervised Learning
Supervised learning algorithms are trained using labeled data. Unsupervised learning algorithms are trained using unlabeled data.
Supervised learning model takes direct feedback to check if it is predicting Unsupervised learning model does not take any feedback.
correct output or not.
Supervised learning model predicts the output. Unsupervised learning model finds the hidden patterns in data.
In supervised learning, input data is provided to the model along with the In unsupervised learning, only input data is provided to the model.
output.
The goal of supervised learning is to train the model so that it can predict The goal of unsupervised learning is to find the hidden patterns and useful
the output when it is given new data. insights from the unknown dataset.
Supervised learning needs supervision to train the model. Unsupervised learning does not need any supervision to train the model.
Supervised learning can be used for those cases where we know the input Unsupervised learning can be used for those cases where we have only
as well as corresponding outputs. input data and no corresponding output data.
Supervised learning model produces an accurate result. Unsupervised learning model may give less accurate result as compared to
supervised learning.
Supervised learning is not close to true Artificial intelligence as in this, we Unsupervised learning is more close to the true Artificial Intelligence as it
first train the model for each data, and then only it can predict the correct learns similarly as a child learns daily routine things by his experiences.
output.
It includes various algorithms such as Linear Regression, Logistic It includes various algorithms such as Clustering, KNN, and Apriori
Regression, Support Vector Machine, Multi-class Classification, Decision algorithm.
tree, Bayesian Logic, etc.
Semi supervised Learning
Semi supervised Learning- Assumptions
Extensive Standard Library: Python's extensive standard library offers a wide range of built-in modules and
functions that you can use interactively. You can import and utilize these modules in your code without
needing to write everything from scratch.
Third-Party Packages: Python has a vast ecosystem of third-party packages and libraries that can be
easily installed and integrated into your interactive coding sessions. This allows you to leverage specialized
tools and functionality without reinventing the wheel.
Why Is Python?
OOP is a way of structuring your code to model real-world entities and their interactions in a more intuitive and
organized manner.
Objects: Objects are the fundamental building blocks of OOP. An object is a self-contained unit that combines
both data (attributes) and behavior (methods). Think of objects as real-world entities or nouns, such as a car, a
person, or a bank account.
Classes: A class is a blueprint or template for creating objects. It defines the attributes (data members) and
methods (functions) that objects of that class will have. Classes act as a way to organize and encapsulate
related data and behavior. You can think of a class as a description or specification for an object.
For example, if we're modeling cars in a car rental system, we might have a Car class that defines attributes
like make, model, and year, and methods like start engine and accelerate.
Attributes: Attributes are the data members or properties of an object. They represent the state or
characteristics of an object. For instance, in a Person class, attributes might include name, age, and address.
These attributes store information about the object.
Methods: Methods are functions defined within a class that define the behavior or actions that an object can
perform. They operate on the attributes of the object and can be used to change the object's state or perform
specific actions.
Why Is Python?
Encapsulation: Encapsulation is the concept of bundling data (attributes) and the methods that operate on that
data into a single unit (a class). It hides the internal details of how a class works and exposes a clean interface
for interacting with objects. Encapsulation helps maintain data integrity and makes code more modular and
maintainable.
Inheritance: Inheritance is a mechanism that allows you to create a new class (a subclass or derived class)
based on an existing class (a parent class or base class). The subclass inherits the attributes and methods of
the parent class. It promotes code reuse and the creation of class hierarchies.
For example, you can have a Vehicle class as a base class, and then create subclasses like Car and
Motorcycle that inherit attributes and methods from the Vehicle class.
Polymorphism: Polymorphism means "many shapes" and refers to the ability of different objects to respond to
the same method or function call in a way that is appropriate for their specific class. It allows you to write code
that can work with objects of different classes as long as they share a common interface.
For instance, you can have a Shape class with a method called area, and then create subclasses like Circle
and Rectangle that implement their own area method. You can call area on any shape object, and the
appropriate method will be executed.
Python for Datascience
Data science is an interdisciplinary field that combines various techniques, algorithms, processes, and systems to
extract meaningful insights and knowledge from structured and unstructured data. It encompasses a wide range of
activities, from data collection and cleaning to data analysis and visualization
Structured data refers to data that is organized in a predefined manner, usually in tabular formats like
spreadsheets or databases. It includes data that can be easily stored, accessed, and analyzed, such as sales
records, customer information, and financial data.
Unstructured data, on the other hand, does not have a specific format or organization. This type of data includes
text documents, images, videos, social media posts, and emails. Unstructured data is more complex to process
and analyze but often contains valuable information that can be uncovered through advanced techniques like
natural language processing (NLP) and machine learning
Python is considered one of the best programming languages for artificial intelligence (AI). Python boasts a vast
and mature ecosystem of libraries and frameworks specifically tailored for AI and machine learning (ML)
development.
Normally, a library is a collection of books or is a room or place where many books are stored to be used later.
Similarly, in the programming world, a library is a collection of precompiled codes that can be used later on in a
program for some specific well-defined operations. Other than pre-compiled codes, a library may contain
documentation, configuration data, message templates, classes, and values, etc.
A Python library is a collection of related modules. It contains bundles of code that can be used repeatedly in
different programs. It makes Python Programming simpler and convenient for the programmer. As we don’t
need to write the same code again and again for different programs. Python libraries play a very vital role in
fields of Machine Learning, Data Science, Data Visualization, etc.
Most of the Python Libraries are written in the C programming language. The Python standard library consists
of more than 200 core modules.
Data Prepocessing
Data preprocessing is a crucial step in data analysis and machine learning. It involves cleaning, transforming, and
organizing raw data into a format suitable for analysis or modeling
1.Data quality assessment
2.Data cleaning
3.Data transformation
4.Data reduction
1.Data quality assessment
It's important to check the quality and consistency of data, as it often contains anomalies and inherent issues that
can affect analysis.
Mismatched data types: When you collect data from many different sources, it may come to you in different
formats. While the ultimate goal of this entire process is to reformat your data for machines, you still need to begin
with similarly formatted data. For example, if part of your analysis involves family income from multiple countries,
you’ll have to convert each income amount into a single currency.
Data Prepocessing
Mixed data values: Perhaps different sources use different descriptors for features – for example, man or male.
These value descriptors should all be made uniform.
Data outliers: Outliers can have a huge impact on data analysis results. For example if you're averaging test
scores for a class, and one student didn’t respond to any of the questions, their 0% could greatly skew the results.
Missing data: Take a look for missing data fields, blank spaces in text, or unanswered survey questions. This
could be due to human error or incomplete data. To take care of missing data, you’ll have to perform data cleaning.
2. Data cleaning
Data cleaning is the process of adding missing data and correcting, repairing, or removing incorrect or irrelevant
data from a data set. Data cleaning is the most important step of preprocessing because it will ensure that your
data is ready to go for your downstream needs.
Data cleaning will correct all of the inconsistent data you uncovered in your data quality assessment.
Depending on the kind of data you’re working with, there are a number of possible cleaners you’ll need to run your
data through.
Data Prepocessing
Ignore the tuples: A tuple is an ordered list or sequence of numbers or entities. If multiple values are missing
within tuples, you may simply discard the tuples with that missing information. This is only recommended for
large data sets, when a few ignored tuples won’t harm further analysis.
Manually fill in missing data: This can be tedious, but is definitely necessary when working with smaller
data sets.
Noisy data
Data cleaning also includes fixing “noisy” data. This is data that includes unnecessary data points, irrelevant data,
and data that’s more difficult to group together.
Binning: Binning sorts data of a wide data set into smaller groups of more similar data. It’s often used
when analyzing demographics. Income, for example, could be grouped: $35,000-$50,000, $50,000-$75,000, etc.
Regression: Regression is used to decide which variables will actually apply to your analysis.
Regression analysis is used to smooth large amounts of data. This will help you get a handle on your data, so
you’re not overburdened with unnecessary data.
Clustering: Clustering algorithms are used to properly group data, so that it can be analyzed with like
data. They’re generally used in unsupervised learning, when not a lot is known about the relationships within your
data.
Data Prepocessing
Noisy data
Data cleaning also includes fixing “noisy” data. This is data that includes unnecessary data points, irrelevant data,
and data that’s more difficult to group together.
Binning: Binning is a technique used in data cleaning to group continuous numerical data into smaller,
more manageable categories or "bins." This process helps to reduce noise, handle outliers, and make the data
easier to analyze. Binning is especially useful when dealing with large datasets where minor variations in data
can obscure meaningful patterns.
Regression: Regression is used to decide which variables will actually apply to your analysis.
Regression analysis is used to smooth large amounts of data. This will help you get a handle on your data, so
you’re not overburdened with unnecessary data.
Clustering: Clustering algorithms are used to properly group data, so that it can be analyzed with like
data. They’re generally used in unsupervised learning, when not a lot is known about the relationships within your
data.
Data Prepocessing
Noisy data
Data cleaning also includes fixing “noisy” data. This is data that includes unnecessary data points, irrelevant data,
and data that’s more difficult to group together.
Binning: Binning is a technique used in data cleaning to group continuous numerical data into smaller,
more manageable categories or "bins." This process helps to reduce noise, handle outliers, and make the data
easier to analyze. Binning is especially useful when dealing with large datasets where minor variations in data
can obscure meaningful patterns.
Grouping Data:
Binning involves dividing continuous data into a set number of intervals or ranges. Each data point is then
assigned to one of these bins based on its value.
Smoothing Data:
By grouping similar data points together, binning smooths out minor variations, making trends and patterns more
apparent.
Handling Outliers:
Extreme values that might skew the analysis can be grouped into broader categories, reducing their impact on
the overall analysis.
Data Prepocessing
3. Data transformation
With data cleaning, we’ve already begun to modify our data, but data transformation will begin the process of
turning the data into the proper format(s) you’ll need for analysis and other downstream processes.
This generally happens in one or more of the below:
Aggregation:Data aggregation combines all of your data together in a uniform format.
Normalization: Normalization scales your data into a regularized range so that you can compare it more
accurately.
Feature selection: Feature selection is the process of deciding which variables (features, characteristics,
categories, etc.) are most important to your analysis. These features will be used to train ML models.
Discreditization:Discreditiization pools data into smaller intervals. It’s somewhat similar to binning, Example,
0-15 minutes, 15-30, etc.
Concept hierarchy generation : Concept hierarchy generation can add a hierarchy within and between your
features that wasn’t present in the original data.
Data Prepocessing
Data reduction
The more data you’re working with, the harder it will be to analyze, even after cleaning and transforming it.
Depending on your task at hand, you may actually have more data than you need. Especially when working
with text analysis, much of regular human speech is superfluous or irrelevant to the needs of the researcher.
Data reduction not only makes the analysis easier and more accurate, but cuts down on data storage.
Attribute selection: Similar to discreditization, attribute selection can fit your data into smaller pools. It,
essentially, combines tags or features, so that tags like male/female and professor could be combined
into male professor/female professor.
Numerosity reduction: This will help with data storage and transmission. You can use a regression model,
for example, to use only the data and variables that are relevant to your analysis.
Dimensionality reduction: This, again, reduces the amount of data used to help facilitate analysis and
downstream processes. Algorithms like K-nearest neighbors use pattern recognition to combine similar data
and make it more manageable.
Less Code: Implementing AI involves tons and tons of algorithms. Thanks to Pythons
support for pre-defined packages, we don’t have to code algorithms. And to make things
easier, Python provides “check as you code” methodology that reduces the burden of
testing the code.
Prebuilt Libraries: Python has 100s of pre-built libraries to implement various Machine
Learning and Deep Learning algorithms. So every time you want to run an algorithm on a
data set, all you have to do is install and load the necessary packages with a single
command. Examples of pre-built libraries include NumPy, Keras, Tensorflow, Pytorch,
and so on.
There are several Pre-processing techniques
1.Binarization
Convert our numerical values into Boolean values. Let us use in-built method to binaries the input data using
threshold ( the values are above the threshold is ‘1’ and below the threshold is ‘0’)
Simple Example of Binarization
Suppose you have a dataset of exam scores, and you want to categorize students as "Pass" or "Fail" based on
a threshold score.
Original Data:
Scores: [45, 75, 60, 82, 39, 55]
Threshold: 50
After Binarization:
Binary Scores: [0, 1, 1, 1, 0, 1]
Here, 1 represents "Pass" (scores ≥ 50), and 0 represents "Fail" (scores < 50).
import numpy as np
from sklearn.preprocessing import Binarizer
# Apply binarization
binary_scores = binarizer.fit_transform(scores)
# Sample data: Heights (in cm) and Incomes (in thousands of dollars)
data = np.array([
[150, 50],
[160, 60],
[170, 70],
[180, 80],
[190, 90]
])
print("Original Data:")
print(data)
# Applying L2 normalization
l2_normalizer = Normalizer(norm='l2')
l2_normalized_data = l2_normalizer.fit_transform(data)
In statistics and machine learning, parametric and non-parametric estimation methods refer to different
approaches for modeling data and making predictions. The distinction between these two lies primarily in the
assumptions they make about the underlying data distribution and the number of parameters involved.
Parametric :Assumes a specific form (mapping function between input and output)for the data distribution
(e.g., normal distribution, linear relationship).
Fixed Number of Parameters: The model is defined by a fixed number of parameters, regardless of the size
of the data set.
Examples: Linear regression, logistic regression, Gaussian distribution, and ARIMA models in time series.
Advantages:
Efficiency: Often computationally efficient because of the fixed number of parameters.
Interpretability: The models are usually easier to interpret because of the simplicity in the functional form.
Less Data Needed: Since the model form is specified, parametric methods may require less data to estimate the
parameters accurately.
Disadvantages:
Model Bias: If the assumptions about the data distribution are incorrect, the model may not fit the data well,
leading to biased estimates.
Inflexibility: Less flexible in modeling complex relationships or distributions that don't fit the assumed form.
Non-Parametric Estimation
Definition: Non-parametric estimation does not assume any specific form for the data distribution. Instead, it uses
the data to model the underlying distribution directly, without assuming a predetermined number of parameters.
Key Characteristics:
No Assumptions: Makes fewer assumptions about the underlying data distribution.
Flexible: The complexity of the model can grow with the size of the data, as more data allows for capturing
more complex patterns.
Examples: K-nearest neighbors (KNN), kernel density estimation (KDE), decision trees, and support vector
machines (SVM).
Advantages:
Flexibility: Can model a wide range of data distributions and relationships because it doesn't assume a specific
form.
Adaptability: The model can adapt to the data's complexity, potentially leading to better performance when the
underlying relationship is unknown or complex.
Disadvantages:
Computational Cost: Often more computationally intensive, especially with large datasets, because the model
may need to consider many parameters.
Overfitting: There's a higher risk of overfitting, especially with small datasets, as the model may become too
complex.
More Data Needed: Non-parametric methods often require more data to achieve reliable estimates, especially as
the model's complexity increases.
Model Parameters are the internal coefficients or Hyperparameters are the external settings or configurations
weights that a model learns from the training data that must be set before the training process begins. Unlike
during the learning process. These parameters are model parameters, they are not learned from the data but
used to make predictions on new data are set by the user to control the training process.
Example: In linear regression, the model tries to Example: In linear regression:
find the best-fit line for the data. The equation of a If you add L2 regularization (Ridge regression), a
line is y=mx+b hyperparameter called alpha (or λ) controls the strength of
where: the regularization. This hyperparameter is not learned from
m (slope) and b(intercept) are the model the data but is set before training.
parameters. In k-nearest neighbors (KNN), the number of neighbors k
These parameters are learned by the model during is a hyperparameter that defines how many neighboring data
training by minimizing the error between the points are considered when making a prediction.
predicted values and the actual values.
.
Example :The weight of the person is linearly related to their height. So, this shows a linear relationship
between the height and weight of the person. According to this, as we increase the height, the weight of the
person will also increase.
It is not necessary that one variable is dependent on others, or one causes the other, but there is some critical
relationship between the two variables.
In such cases, we use a scatter plot to simplify the strength of the relationship between the variables. If there
is no relation or linking between the variables then the scatter plot does not indicate any increasing or
decreasing pattern
In such cases, the linear regression design is not beneficial to the given data. And
another example is predicting house prices based on features such as square
footage, number of bedrooms, and location.
Correlation Coefficients:
The measure of the relationship between two variables is shown by the correlation coefficient. The range of the
coefficient lies between -1 to +1. This coefficient shows the strength of the association of the observed data
between two variables.
Statistical measure that quantifies the strength and direction of the linear relationship between two variables.
If a point rests on the fitted line accurately, then the value of its perpendicular deviation is 0. It is 0 because the
variations are first squared, then added, so their positive and negative values will not be cancelled. Linear
regression determines the straight line, known as the Least-Squares Regression Line or LSRL
# Dataset
X = np.array([2, 4, 6, 8]).reshape(-1, 1) # Independent variable
Y = np.array([3, 7, 5, 10]) # Dependent variable
Linear regression
• Linear regression and logistic regression are both data analytics that use independent variables to predict the
dependent variables. However, it differs in how it will handle the dependent variable. In linear regression, the
dependent variable is continuous, For example, predicting the price of the house.
• Logistic regression is used for binary classification problems. It predicts the probability that a given input
belongs to a certain class. The output is transformed using the logistic (sigmoid) function, which maps any real-
valued number into a value between 0 and 1.
Modeling logistic regression from linear regression involves transforming the linear regression model to handle
binary outcomes (0 or 1) instead of continuous outcomes
# Part I: Calculate the probability of passing for a student who studied 33 hours
hours_studied = 33
prob_pass_33_hours = calculate_probability(hours_studied)
print(f"Probability of passing for a student who studied {hours_studied} hours: {prob_pass_33_hours:.4f}")
Note:
In logistic regression, the cost function (often referred to as
the log-likelihood function) and its gradient provide a way
to evaluate and optimize the model parameters.
Cost Function (Log-Likelihood Function)
The cost function in logistic regression measures how well
the model's predicted probabilities align with the actual
class labels. It's derived from the likelihood of the observed
data given the model parameters. Specifically, for logistic
regression, we often use the negative log-likelihood as the
cost function, which we aim to minimize.