AI Lab Manual Version 1.3
AI Lab Manual Version 1.3
3
CS4811: Artificial Intelligence
Lab Manual
Week 3 : Lab 3 Uninformed Search: Breadth First Search and Depth First 23
Search
Week 4 : Lab 4 A* Search 30
Week 10 : Lab 9 First Order Logic (FOL) Inference by using Python Libraries 74
Python is:
Semantically Dynamic: you don't have to specify types for variables and there is
nothing that makes you do it.
Object-Oriented: everything in Python is an object. But you can choose to write code
in an object-oriented, procedural, or even functional way.
High level: you don't have to deal with low-level machine details.
This lab is a course on the introduction of Python language. The lab allows you to learn
the core of the language in a matter of hours instead of weeks.
1.1. Use of Python
Python has been growing a lot recently partly because of its many uses in the following
areas:
1.2. Syntax
1.2.1. Semicolons
Python doesn't use semicolons to finish lines. A new line is enough to tell the interpreter
that a new command is beginning.
In this example, we have two commands that will display the messages inside the single
quotes.
1.2.2. Indentation
This means you have to be aware of white spaces at the beginning of each line -- they
have meaning and might break your code if misplaced.
This doesn't work because the indentation of the second line is missing and will throw
an error:
This Python is case sensitive. So the variables name and Name are not the same thing
and store different values.
As you can see, variables are easily created by just assigning values to them using the =
symbol.
1.2.4. Comments
Finally, to comment something in your code, use the hash mark #.
1.2.5. Types
To store data in Python you need to use a variable. And every variable has its type
depending on the value of the data stored. Python has dynamic typing, which means you
don't have to explicitly declare the type of your variable -- but if you want to, you can.
Lists, Tuples, Sets, and Dictionaries are all data types and have dedicated sections later
on with more details, but we'll look at them briefly here.
If you need to interact with a user when running your program in the command line (for
example, to ask for a piece of information), you can use the input () function.
1.2.7. Operators
In a programming language, operators are special symbols that you can apply to your
variables and values in order to perform operations such as arithmetic/mathematical and
comparison. Python has lots of operators that you can apply to your variables and I will
demonstrate the most used ones.
Arithmetic operators are the most common type of operators and also the most
recognizable ones. They allow you to perform simple mathematical operations. They are:
1.2.7.2. Concatenation
Concatenation is when you have two or more strings and you want to join them into one.
This is useful when you have information in multiple variables and want to combine
them.
For instance, in this next example I combine two variables that contain my first name
and my last name respectively to have my full name.
Use comparison operators to compare two values. These operators return either True or
False. They are:
==: Equal
!=: Not equal
>: Greater than
<: Less than
>=: Greater than or equal to
<=: Less than or equal to
Logical operators are used to combine statements applying Boolean algebra. They are:
Conditionals are one of the cornerstones of any programming language. They allow you
to control the program flow according to specific conditions you can check.
1.2.8.1. if statement
The way you implement a conditional is through the if statement. The general form of
an if statement is:
The expression contains some logic that returns a Boolean, and the statement is
executed only if the return is True. A simple example:
In our last example, the program only does something if the condition returns True.
In this example, we swapped Bob's and Sarah's age. The first condition will return
False since Sarah is older now, and then the program will print the phrase after the
else instead.
3. Open Pycharm and create new project named Example1 by clicking on file in menu bar
and then on new project.
4. After creating new project right click on project name and create a python file name first
program as shown in figures below.
7. After that you can see output in output box. Function returned 2.
The evaluation criteria for this lab will be based on the completion of the following tasks.
Each task is assigned the marks percentage which will be evaluated by the instructor in
the lab whether the student has finished the complete/partial task(s).
Criteria Level 0 Level 1 Level 2 Level 3 Level 4 Total
(0%) (1-50)% (51-65)% (66-80)% (81-100)% Score
Procedural Awareness
Practical Implementation
Program Correctness
Use of Software Tool
Sub Total Marks
1.8.1. Books
Artificial Intelligence: A modern approach 3rd Edition by Stuart Russell & Peter Norvig
1.8.2. Links
https://pynative.com/python-basic-exercise-for-beginners/
https://www.w3resource.com/python-exercises/
A library is a collection of pre-written code that you can use to perform specific tasks.
Libraries are often used to reduce the amount of code a programmer needs to write by
providing reusable functions or classes that can be called upon as needed.
All libraries are divided into two classifications. All libraries are either Static or
Dynamic.
Static: These are compiled into a program’s executable code, making it self-
contained and independent of external changes. Static libraries are linked with a
program at compile time, resulting in a single executable file that includes all of the
code from the library. As a result, static libraries are often used to improve the
performance of a program by avoiding the overhead of loading a library at runtime.
Dynamic: Dynamic libraries are loaded at runtime rather than linked with a program
at compile time. This allows programs to use the library without having to be re-
compiled if the library is updated. In addition, dynamic libraries are often used to
provide platform-specific functionality or to allow multiple programs to share a single
copy of the library in memory.
You can write libraries in most programming languages, and they can be used by
programs written in that same language or a different language. For example, a library
written in C++ could be used by a program written in Python.
Libraries can perform various tasks, such as file input and output, data manipulation,
network communication, and more. They can also provide access to system resources,
such as the file system or hardware devices.
Python libraries play a significant role in making AI the technology they are today. Here
are some of the well-known Python libraries that are leveraged in the development of
Artificial Intelligence applications:
The evaluation criteria for this lab will be based on the completion of the following tasks.
Each task is assigned the marks percentage which will be evaluated by the instructor in
the lab whether the student has finished the complete/partial task(s).
Criteria Level 0 Level 1 Level 2 Level 3 Level 4 Total
(0%) (1-50)% (51-65)% (66-80)% (81-100)% Score
Procedural Awareness
Practical Implementation
Program Correctness
Use of Software Tool
Sub Total Marks
2.6.1. Books
Artificial Intelligence: A modern approach 3rd Edition by Stuart Russell & Peter Norvig
2.6.2. Links
https://www.edureka.co/blog/python-libraries/
The objective of this lab is to introduce the concept of uninformed search algorithms and
their usage. Uninformed search is a class of general-purpose search algorithms which
operates in brute force-way. Uninformed search algorithms do not have additional
information about state or search space other than how to traverse the tree, so it is also
called blind search. Following are some of the types of uninformed search algorithms:
Breadth-first Search
Depth-first Search
3.1. Breadth First Search (BFS)
Depth-first search is an algorithm for traversing or searching tree or graph data structures.
The algorithm starts at the root node (selecting some arbitrary node as the root node in the
case of a graph) and explores as far as possible along each branch before backtracking. So the
basic idea is to start from the root or any arbitrary node and mark the node and move to the
adjacent unmarked node and continue this loop until there is no unmarked adjacent node.
Then backtrack and check for other unmarked nodes and traverse them. Finally print the
nodes in the path.
3.3. Relevant Lecture Readings
Revise Lecture Uninformed search
To get knowledge of implementing Breadth First Search, Depth First Search and
Uniform Cost Search
To know the difference between the above mentioned uninformed search algorithms
This concept map will help students to understand the main concepts of topics covered
in the lab. In this lab, students will be provided with a detailed content to learn that how
built in libraries can be used to implement concept of BFS, DFS and Uniform Cost Search
in python.
let Q be queue.
Q.enqueue(s) //Inserting s in queue until all its neighbor vertices are marked.
mark s as visited.
while (Q is not empty)
//Removing that vertex from queue, whose neighbor will be visited
now
v = Q.dequeue( )//processing all the neighbours of v for all neighbours w of v in
Graph G if w is not visited
Q.enqueue( w )//Stores w in Q to further visit its neighbor mark w as visited.
3.9. Tools
Pycharm
Test Case 1
You are required to implement the Depth First Search on the given graph in Python.
Implement Water Jug Problem using the DFS in Python, initial stage (0,0) and final stage
(2,Y). A Water Jug Problem: You are given two jugs, a 4-gallon one and a 3- gallon one,
a pump which has unlimited water which you can use to fill the jug, and the ground on
which water may be poured. Neither jug has any measuring markings on it. How can you
get exactly 2 gallons of water in the 4-gallon jug?
The evaluation criteria for this lab will be based on the completion of the following tasks.
Each task is assigned the marks percentage which will be evaluated by the instructor in
3.13.1. Books
Artificial Intelligence: A modern approach 3rd Edition by Stuart Russell & Peter Norvig
3.13.2. Links
https://www.geeksforgeeks.org/breadth-first-search-or-bfs-for-a-graph/
https://www.geeksforgeeks.org/depth-first-search-or-dfs-for-a-graph/
https://pythoninwonderland.wordpress.com/2017/03/18/how-to-implement-breadth-first-search-in-
python/
https://en.wikipedia.org/wiki/Depth-first_search
https://www.hackerearth.com/practice/algorithms/graphs/depth-first-search/tutorial/
Lab-04: A* Search
In this lab you will learn about the concepts of A* Search. This algorithm is one of the best
and popular technique used in path-finding and graph traversals. The idea is avoid expanding
nodes which are expansive.
This concept map will help students to understand the main concepts of topics covered in the
lab. In this lab, students will be provided with a detailed content to learn that how built in
libraries can be used to implement concept of A* search algorithm in python.
4.4. A* Search
2. Given the graph, find the cost-effective path from A to G. That is A is the source node
and G is the goal node.
4. A → B = g(B) + h(B) = 2 + 6 = 8
5. A → E = g(E) + h(E) = 3 + 7 = 10
6. Since the cost for A → B is less, we move forward with this path and compute the f(x)
for the children nodes of B.
8. A → B → C = (2 + 1) + 99= 102
9. A → B → G = (2 + 9 ) + 0 = 11
10. Here the path A → B → G has the least cost but it is still more than the cost of A → E,
thus we explore this path further.
12. A → E → D = (3 + 6) + 1 = 10
13. Comparing the cost of A → E → D with all the paths we got so far and as this cost is
least of all we move forward with this path.
15. A → E → D → G = (3 + 6 + 1) +0 = 10
16. Now comparing all the paths that lead us to the goal, we conclude that A → E → D →
G is the most cost-effective path to get from A to
Design the solution of the following 8 puzzle problem given in figure 4.2. Your solution
must contain all steps until goal state not find as described the solution design in section
4.1.
4.6.1. Tools
Pycharm
Figure 4.3
Write the given code in .py file in Pycharm and run it.
Implement the A* code in python on the following test cases (for help see the
walkthrough task). In test case 1, the starting node is 8 and the last node is 6 whereas in
test case 2 the starting node is 0 and the ending node is 4.
The puzzle consists of an area divided into a grid, 3 by 3 for the 8-puzzle. On each grid
The evaluation criteria for this lab will be based on the completion of the following tasks.
Each task is assigned the marks percentage which will be evaluated by the instructor in
the lab whether the student has finished the complete/partial task(s).
Criteria Level 0 Level 1 Level 2 Level 3 Level 4 Total
(0%) (1-50)% (51-65)% (66-80)% (81-100)% Score
Procedural Awareness
Practical Implementation
Program Correctness
Use of Software Tool
Sub Total Marks
4.10.1. Books
Artificial Intelligence: A modern approach 3rd Edition by Stuart Russell & Peter Norvig
4.10.2. Links
https://www.geeksforgeeks.org/a-search-algorithm/
https://www.annytab.com/a-star-search-algorithm-in-
python/#:~:text=The%20A*%20search%20algorithm%20uses,heuristic%20to%20gui
de%20the%20search.
Clearly understand the concept of Min-Max algorithm and able to use an optimization
technique for min-max algorithm (Alpha-Beta).
5.3. Concept map
In this lab, we’re going to discuss Minimax algorithm and its applications in Artificial
Intelligence. As it’s a game theory algorithm, we’ll implement a simple game using it.
In other words, the maximizer works to get the highest score, while the minimizer tries
get the lowest score by trying to counter moves. Examples of such games are chess,
poker, checkers, tic-tac-toe.
Maximizer starts with the root node and chooses the move with the maximum score.
Unfortunately, only leaves have evaluation scores with them, and hence the algorithm
has to reach leaf nodes recursively. In the given game tree, currently it’s the minimizer’s
turn to choose a move from the leaf nodes, so the nodes with minimum scores (here,
node 3 and 4) will get selected. It keeps picking the best nodes similarly, till it reaches
the root node.
Read and understand the concept of minmax algorithm. Try to design the solution of any
example on paper and bring it in lab.
Since this is a backtracking based algorithm, it tries all possible moves, then backtracks
and makes a decision.
Maximizer goes LEFT: It is now the minimizers turn. The minimizer now has a choice
between 3 and 5. Being the minimizer it will definitely choose the least among both, that
is 3
Maximizer goes RIGHT: It is now the minimizers turn. The minimizer now has a choice
between 2 and 9. He will choose 2 as it is the least among the two values.
Being the maximizer you would choose the larger value that is 3. Hence the optimal
move for the maximizer is to go LEFT and the optimal value is 3.
Now the game tree looks like the above figure, and the above tree shows two possible
scores when maximizer makes left and right moves.
Note: Even though there is a value of 9 on the right sub tree, the minimizer will never
pick that. We must always assume that our opponent plays optimally. Below is the
implementation for the same.
Min-Max
The evaluation criteria for this lab will be based on the completion of the following
tasks. Each task is assigned the marks percentage which will be evaluated by the
instructor in the lab whether the student has finished the complete/partial task(s).
Criteria Level 0 Level 1 Level 2 Level 3 Level 4 Total
(0%) (1-50)% (51-65)% (66-80)% (81-100)% Score
Procedural Awareness
Practical Implementation
Program Correctness
Use of Software Tool
Sub Total Marks
5.11.2. Links
https://www.geeksforgeeks.org/minimax-algorithm-in-game-theory-set-1-introduction/
http://homepage.ufp.pt/jtorres/ensino/ia/alfabeta.html
https://stackabuse.com/minimax-and-alpha-beta-pruning-in-python/
Beta: The best (lowest-value) choice we have found so far at any point along the
path of Minimizer. The initial value of beta is +∞.
The main condition which required for alpha-beta pruning is: α>=β
Let's take an example of two-player search tree to understand the working of Alpha-beta
pruning.
Step 1: At the first step the Max player will start first move from node A where α= -∞
and β= +∞, these value of alpha and beta passed down to node B where again α= -∞ and
β= +∞, and Node B passes the same value to its child D.
Step 3: Now algorithm backtrack to node B, where the value of β will change as this is
a turn of Min, Now β= +∞, will compare with the available subsequent nodes value, i.e.
min (∞, 3) = 3, hence at node B now α= -∞, and β= 3.
In the next step, algorithm traverse the next successor of Node B which is node E, and
CAPITAL UNIVERSITY OF SCIENCE & TECHNOLOGY 49
the values of α= -∞, and β= 3 will also be passed.
Step 4: At node E, Max will take its turn, and the value of alpha will change. The current
value of alpha will be compared with 5, so max (-∞, 5) = 5, hence at node E α= 5 and β=
3, where α>=β, so the right successor of E will be pruned, and algorithm will not traverse
it, and the value at node E will be 5.
Step 5: At next step, algorithm again backtrack the tree, from node B to node A. At node
A, the value of alpha will be changed the maximum available value is 3 as max (-∞, 3)=
3, and β= +∞, these two values now passes to right successor of A which is Node C.
At node C, α=3 and β= +∞, and the same values will be passed on to node F.
Step 6: At node F, again the value of α will be compared with left child which is 0, and
max(3,0)= 3, and then compared with right child which is 1, and max(3,1)= 3 still α
remains 3, but the node value of F will become 1.
Step 8: C now returns the value of 1 to A here the best value for A is max (3, 1) = 3.
Following is the final game tree which is the showing the nodes which are computed and
nodes which has never computed. Hence the optimal value for the maximizer is 3 for
this example.
Read and understand the concept of alpha beta pruning. Try to design the solution of any
example on paper and bring it in lab.
The evaluation criteria for this lab will be based on the completion of the following
tasks. Each task is assigned the marks percentage which will be evaluated by the
instructor in the lab whether the student has finished the complete/partial task(s).
Criteria Level 0 Level 1 Level 2 Level 3 Level 4 Total
(0%) (1-50)% (51-65)% (66-80)% (81-100)% Score
Procedural Awareness
Practical Implementation
Program Correctness
Use of Software Tool
Sub Total Marks
6.11.1. Books
Artificial Intelligence: A modern approach 3rd Edition by Stuart Russell & Peter Norvig
6.11.2. Links
https://www.geeksforgeeks.org/minimax-algorithm-in-game-theory-set-1-introduction/
http://homepage.ufp.pt/jtorres/ensino/ia/alfabeta.html
https://stackabuse.com/minimax-and-alpha-beta-pruning-in-python/
In the field of AI, many complex algorithms have been used. It is also important to find out
an optimal solution. Hill climbing algorithm is one such optimization algorithm used in the
field of Artificial Intelligence. It is a mathematical method which optimizes only the
neighboring points and is considered to be heuristic. A heuristic method is one of those
methods which does not guarantee the best optimal solution. This algorithm belongs to the
local search family.
Local search algorithms are used on complex optimization problems where it tries to find
out a solution that maximizes the criteria among candidate solutions. A candidate
solution is considered to be the set of all possible solutions in the entire functional region
of a problem.
Revise Lectures Hill Climbing Algorithm (Simple hill Climbing, Steepest-Ascent hill
climbing)
This concept map will help students to understand the main concepts of topics covered
in the lab. In this lab, students will be provided with a detailed content to learn that what
the concept of hill climbing is and how to implement it in python.
7.4. Overview
Hill Climbing is a heuristic search used for mathematical optimization problems in the
This algorithm works on the following steps in order to find an optimal solution.
It tries to define the current state as the state of starting or the initial state.
It generalizes the solution to the current state and tries to find an optimal solution. The
solution obtained may not be the best.
It compares the solution which is generated to the final state also known as the goal state.
It will check whether the final state is achieved or not. If not achieved, it will try to find
another solution.
7.7.1. Robotics
Hill climbing is mostly used in robotics which helps their system to work as a team and
maintain coordination.
7.7.2. Marketing
Read and under the detailed concepts of hill climbing algorithm and their usage in AI
field.
Pycharm
Implement the following goal state finding problem using hill climbing algorithm.
Consider a 2D grid with a starting point (0, 0) and a target point (5, 5). You are allowed
to move either up, down, left, or right by one unit at a time. Implement the Hill Climbing
algorithm in Python to find the shortest path from the starting point to the target point.
Remember to consider edge cases, handle obstacles if any, and optimize your
implementation for efficiency.
7.13.1. Books
Artificial Intelligence: A modern approach 3rd Edition by Stuart Russell & Peter Norvig
7.13.2. Links
https://www.mygreatlearning.com/blog/an-introduction-to-hill-climbing-algorithm/
https://www.geeksforgeeks.org/introduction-hill-climbing-artificial-intelligence/
https://www.baeldung.com/java-hill-climbing-algorithm
8.4. Overview
Formally speaking, a constraint satisfaction problem (or CSP) is defined by a set of
variables, {X1; X2; : : : ; Xn}, and a set of constraints, {C1; C2; : : : ; Cm}. Each variable
Xi has a nonempty domain Di of possible values. Each constraint Ci involves some
subset of the variables and specifies the allowable combinations of values for that subset.
A state of the problem is defined by an assignment of values to some or all of the
variables,{Xi=vi; Xj=vj; : : :}. An assignment that does not violate any constraints is
called a consistent or legal assignment. A complete assignment is one in which every
We are looking at the map of Australia showing each of its states and territories as shown
in above figure (a), we are given the task of coloring each region either red, green or blue
in such a way that no neighboring regions have the same color.
To formulate this CSP, we define the variables to the regions: WA, NT, Q, NSW, V,
SA, T.
It is helpful to visualize a CSP as a constraint graph as shown in figure (b). The nodes of
the graph correspond to variables of the problem.
But backtracking is not efficient yet. We can improve efficiency of backtracking using
some General-Purpose methods. Most Constraint Variable
Choose the variable with the most constraints on remaining variables (most edges in
graph)
LSV guides the choice of which value to assign next. Choose the least constraining value:
NT = Blue G B R G B G R,G,B
Design the solution of the following CSP also make table to show the process of coloring
on paper given in figure 5.1. Your solution must contain all steps until the whole map is
not colored as described in the solution design in section 4.2.3.
This section will provide more practice exercises which you need to finish during the
lab. You need to finish the tasks in the required time.
After completing this lab, students will to know how to deal with CSP.
8.11.1.Books
Artificial Intelligence: A modern approach 3rd Edition by Stuart Russell & Peter Norvig
8.11.2. Links
https://www.geeksforgeeks.org/m-coloring-problem-backtracking-5/
https://freecontent.manning.com/constraint-satisfaction-problems-in-python/
9.4. Libraries
2. Pyke:
o Pyke is a knowledge-based inference engine (expert system) that uses FOL for
knowledge representation and reasoning.
o It's designed for building expert systems and rule-based applications.
o Pyke can be used to define knowledge bases and rules using FOL.
3. Prover9/Mace4:
o Prover9 and Mace4 are theorem proving tools that can handle FOL.
o While they are standalone programs, you can use Python to interact with them and
automate theorem proving tasks.
4. NetworkX is a Python library for creating and analyzing complex networks.
o It can be used to model and work with graphs and networks, which can be a useful
representation for certain FOL applications.
5. Pyke Knowledge Engine:
o Pyke Knowledge Engine is a Python library that provides an integrated environment for
9.5. Tools
Prolog is a popular programming language and logic programming environment used for
implementing and working with logic-based systems. There are several Prolog
implementations (also called Prolog compilers or Prolog systems) available, each with
its own features and advantages. Here are some commonly used Prolog implementations:
1. SWI-Prolog:
o SWI-Prolog is a widely used and actively maintained Prolog system.
o It is open-source and available on multiple platforms, including Windows, macOS, and
Linux.
o SWI-Prolog has a rich set of libraries and extensions, making it suitable for various
applications.
o It includes a built-in development environment with a graphical debugger and a web-
based IDE called SWISH.
2. GNU Prolog:
o GNU Prolog is an open-source Prolog compiler designed to be compatible with the ISO
standard for Prolog.
o It is available on various platforms, including Unix-like systems, Windows, and macOS.
o GNU Prolog focuses on being a lightweight and efficient Prolog system.
This section will provide more practice exercises which you need to finish during the
lab. You need to finish the tasks in the required time.
Translate the following English sentence into FOL and implement it in Python using
SymPy:
3. "For all x, if x is a person, then there exists a y such that y is the parent of x."
To write the negation of the FOL statement "Some birds can fly" in Python using the
SymPy library, you can use the Not operator to negate the entire statement.
After completing this lab, students will know the practical implementation of FOL using
Python libraries.
9.9.1. Books
Artificial Intelligence: A modern approach 3rd Edition by Stuart Russell & Peter
Norvig
9.9.2. Links
https://avigad.github.io/lamr/implementing_first_order_logic.html
Deep learning inference refers to the process of using a trained deep learning model to
make predictions or decisions based on new, unseen data. In deep learning, models are
typically composed of multiple layers of artificial neurons (like neural networks) that
have been trained on large datasets to learn complex patterns and relationships within
the data.
1. Training: Initially, a deep learning model is trained on a labeled dataset. During training,
the model learns to recognize patterns and make predictions based on the input data and
associated labels. This involves adjusting the model's internal parameters (weights and
biases) through a process called backpropagation and optimization techniques such as
gradient descent.
2. Model Deployment: Once the model is trained and its parameters are optimized, it can be
deployed for inference. Deployment can occur on various platforms, including cloud
servers, edge devices (like smartphones or IoT devices), or even embedded systems.
3. Inference: During inference, the trained model is presented with new, unseen data (input).
The model processes this input data through its layers of neurons, and the output is generated
based on what the model has learned during training. This output can take different forms,
depending on the nature of the task. For example, in image classification, the output might
be a label identifying the object in the image.
4. Decision-Making: The output generated by the model during inference is used for decision-
making or prediction. In applications like image recognition, natural language processing,
and autonomous driving, deep learning models are used to make decisions based on the
input data. For instance, a deep learning model in a self-driving car might use inference to
detect obstacles and make decisions about steering and braking.
This concept map will help students to understand the main concepts of topics covered
in the lab. In this lab, students will be provided with a detailed content to learn that what
the concept of deep learning is and how to implement these models in python.
There are several types of deep learning models, each designed for specific tasks and
data types. Here are some common types:
3. Recurrent Neural Networks (RNN): Suited for sequence data, RNNs have connections
that loop back on themselves, allowing them to process sequences in a time-dependent
manner. Long Short-Term Memory (LSTM) and Gated Recurrent Unit (GRU) are
popular RNN variants.
5. Auto encoders: Used for unsupervised learning, auto encoders learn to encode data into
a compressed representation and then decode it back to the original data. They're used
for tasks like data denoising, dimensionality reduction, and anomaly detection.
7. Variational Auto encoders (VAE): VAEs combine auto encoders with probabilistic
modeling, enabling the generation of new data instances while exploring the underlying
data distribution.
8. Siamese Networks: These networks are used for tasks like similarity and distance
measurement. They have two or more identical subnetworks, sharing weights, and are
often used for tasks like face recognition and signature verification.
9. Graph Neural Networks (GNN): Designed to work with graph-structured data, GNNs
can capture information from nodes and their connections in graphs, making them useful
These are just a few examples of the diverse range of deep learning models. Each type
is designed to address specific types of data and tasks, and researchers continue to
develop new architectures to tackle increasingly complex problems.
Pycharm
2. We load the MNIST dataset of handwritten digits, which consists of 28x28 pixel images.
3. We preprocess the data by scaling pixel values to the range [0, 1].
4. We build a feedforward neural network model using Keras. This model has an input layer
that flattens the 28x28 images into 1D arrays, a hidden layer with ReLU activation, a
dropout layer to reduce overfitting, and an output layer with 10 neurons (one for each
digit) using softmax activation.
6. We train the model on the training data for 10 epochs with a validation split of 20%.
7. Finally, we evaluate the model's accuracy on the test data and print the result.
The output will show the test accuracy of the trained model, indicating how well it
performs on classifying handwritten digits in the MNIST dataset.
The evaluation criteria for this lab will be based on the completion of the following tasks.
Each task is assigned the marks percentage which will be evaluated by the instructor in
the lab whether the student has finished the complete/partial task(s).
Criteria Level 0 Level 1 Level 2 Level 3 Level 4 Total
(0%) (1-50)% (51-65)% (66-80)% (81-100)% Score
Procedural Awareness
Practical Implementation
Program Correctness
Use of Software Tool
Sub Total Marks
Artificial Intelligence: A modern approach 3rd Edition by Stuart Russell & Peter
Norvig
10.9.2. Links
https://colab.research.google.com/drive/19YwLLREPmE0ZFoneEKQpSkXRIIF9pPP
https://www.geeksforgeeks.org/introduction-hill-climbing-artificial-intelligence/
https://www.mathworks.com/discovery/deep-learning.html
https://www.projectpro.io/article/deep-learning-projects-for-beginners/441
Data-Driven Learning: Machine learning algorithms learn from data. You provide a
machine learning model with a dataset that includes examples (input data) and their
corresponding outcomes (labels or target values), and the model learns patterns and
relationships within the data.
Generalization: A primary goal of machine learning is to create models that can generalize
well to new, unseen data. This means that the model should be able to make accurate
predictions or decisions on data it hasn't encountered during training.
Automated Learning: Machine learning models automatically adapt and improve their
performance as they receive more data. This adaptability is a key feature, as it allows models
to handle changing and evolving environments.
Automation: Machine learning can automate repetitive tasks, allowing humans to focus on
more creative and complex aspects of their work. For example, in customer support,
chatbots powered by machine learning can handle routine inquiries, freeing up human
agents for more complex customer interactions.
In summary, machine learning is a powerful tool for solving complex problems and making
predictions or decisions based on data. Its ability to learn from data and generalize to new
situations makes it a valuable technology in a wide range of fields and applications.
This is mostly used for document classification problem, i.e. whether a document
belongs to the category of sports, politics, technology etc. The features/predictors used
by the classifier are the frequency of the words present in the document.
This is similar to the multinomial Naive Bayes but the predictors are Boolean variables.
The parameters that we use to predict the class variable take up only values yes or no, for
example if a word occurs in the text or not.
Gaussian Naive Bayes
When the predictors take up a continuous value and are not discrete, we assume that
these values are sampled from a Gaussian distribution. Naive Bayes algorithms are
mostly used in sentiment analysis, spam filtering, and recommendation systems etc.
They are fast and easy to implement.
This concept map will help students to understand the main concepts of topics covered
in the lab. In this lab, students will be provided with a detailed content to learn that what
the concept of machine learning is and how to implement different ML algorithms in
python.
Pycharm
mean that the humidity is high. Another assumption made here is that all the predictors
have an equal effect on the outcome. That is, the day being windy does not have more
importance in deciding to play golf or not. According to this example, Bayes theorem
can be rewritten as:
The variable y is the class variable (play golf), which represents if it is suitable to play
golf or not given the conditions. Variable X represent the parameters/features.
Here's a basic example of how to implement Naive Bayes classification using Scikit-
Learn:
Accuracy: 95.67
After completing this lab, students will to know how to deal with Naïve Bayes classifier.
The evaluation criteria for this lab will be based on the completion of the following tasks.
Each task is assigned the marks percentage which will be evaluated by the instructor in
the lab whether the student has finished the complete/partial task(s)
11.11.1. Books
Artificial Intelligence: A modern approach 3rd Edition by Stuart Russell & Peter Norvig
11.11.2. Links
https://www.geeksforgeeks.org/naive-bayes-classifiers/
https://towardsdatascience.com/naive-bayes-classifier-81d512f50a7c
Supervised learning and unsupervised learning are two fundamental approaches in machine
learning, and they differ in terms of their objectives and how they use labeled or unlabeled
data. Here's a comparison of the two with examples:
1. Supervised Learning:
Objective: In supervised learning, the goal is to learn a mapping from input data to known
output labels. It involves training a model to make predictions or classifications based on
labeled training data.
Labeled Data: Supervised learning requires a dataset with both input features and
corresponding output labels.
Examples:
Classification: Classifying emails as spam or not spam. Given a dataset of emails, each
labeled as spam or not spam, a supervised learning algorithm learns to classify new,
unlabeled emails.
Regression: Predicting house prices based on features like square footage, number of
bedrooms, and location. The model learns to predict the price (a continuous value) from
the input features.
2. Unsupervised Learning:
Examples:
Clustering: Grouping similar data points together. For instance, in customer segmentation,
you might have data on customer behavior but not their segment. Unsupervised learning can
cluster customers based on their behavior to identify different market segments.
Anomaly Detection: Identifying unusual or rare data points in a dataset. This can be used
in fraud detection, where unusual financial transactions need to be flagged.
Example Comparison:
Imagine you have a dataset containing customer purchase history, and you want to analyze
it:
Supervised Learning: If you have labeled data with information about which customers
made a purchase (output label) and other customer attributes (input features), you can use
supervised learning to build a model to predict which new customers are likely to make a
purchase. This would be a classification task.
Unsupervised Learning: If you only have the purchase history without labels indicating
whether each customer made a purchase or not, you can use unsupervised learning to cluster
customers based on their purchase behavior. This might reveal distinct customer segments,
even though you don't know what those segments represent in advance.
In summary, supervised learning is used when you have labeled data and want to predict or
classify based on known outcomes, while unsupervised learning is used when you want to
uncover patterns or structure within unlabeled data. Each approach is valuable in different
scenarios and serves distinct machine learning purposes.
1. Linear Regression: Linear regression is used for predicting a continuous target variable
(a real number) based on one or more input features. It fits a linear equation to the data
and is particularly useful for regression tasks.
2. Logistic Regression: Unlike linear regression, logistic regression is used for binary
classification problems, where the goal is to predict one of two classes. It models the
probability that a given input belongs to a particular class.
3. Decision Trees: Decision trees are versatile algorithms that can be used for both
classification and regression tasks. They partition the data into subsets based on the
values of input features and make decisions at each node to classify or predict the target
variable.
4. Random Forest: Random Forest is an ensemble learning method that combines multiple
decision trees to improve predictive performance and reduce overfitting. It is commonly
used for both classification and regression tasks.
5. Support Vector Machines (SVM): SVM is a classification algorithm that finds the
optimal hyperplane to separate data into different classes. It works well for linear and
non-linear classification tasks.
6. K-Nearest Neighbors (KNN): KNN is a simple yet effective algorithm for classification
and regression. It makes predictions based on the majority class or average value of its k
nearest neighbors in the training data.
8. Neural Networks: Neural networks, particularly deep learning models, have gained
popularity for their ability to handle complex data and tasks. They consist of
interconnected layers of neurons and are used for a wide range of tasks, including image
and speech recognition, natural language processing, and more.
1. Clustering Algorithms:
K-Means Clustering: This algorithm partitions the data into 'K' clusters based on
similarity. It assigns data points to clusters by minimizing the distance between data
points and the cluster centroids.
Autoencoders: Autoencoders are neural network-based models that learn to encode and
decode data, effectively reducing its dimensionality. They are used for tasks like feature
3. Generative Models:
This concept map will help students to understand the main concepts of topics covered
in the lab. In this lab, students will be provided with a detailed content to learn that what
the concept of supervised and unsupervised learning is and how to implement different
algorithms in python.
Pycharm
After completing this lab, students will to know how to deal with different supervised
and unsupervised learning algorithms.
12.10.1. Books
Artificial Intelligence: A modern approach 3rd Edition by Stuart Russell & Peter Norvig
12.10.2. Links
https://colab.research.google.com/github/RPI-DATA/course-intro-ml-
app/blob/master/content/notebooks/08-intro-modeling/04-knn.ipynb#scrollTo=nhXd2F5ewXPo
Classification Metrics:
1. Accuracy: This is one of the most basic metrics and measures the ratio of
correctly predicted instances to the total instances in the dataset. However,
it may not be suitable for imbalanced datasets.
Example: If a binary classification model correctly predicts 800 out of 1000
instances, the accuracy is 80%.
2. Precision: Precision measures the proportion of true positive predictions out
of all positive predictions. It is useful when false positives are costly.
Example: Out of 100 positive predictions, 85 were true positives, so the
precision is 85%.
𝑇𝑃
𝑃𝑟𝑒𝑐𝑖𝑠𝑖𝑜𝑛 =
𝑇𝑃 + 𝐹𝑃
3. Recall (Sensitivity or True Positive Rate): Recall calculates the proportion
of true positive predictions out of all actual positives. It is important when
false negatives are costly.
Example: There were 100 actual positive instances, and the model correctly
predicted 85 of them, so the recall is 85%.
𝑇𝑃
𝑟𝑒𝑐𝑎𝑙𝑙 =
𝑇𝑃 + 𝐹𝑁
2. You have a regression model for predicting house prices. The model has an
RMSE of 10. What does this RMSE value signify in the context of your model's
13.7.2. Task 2
Implement a Python function to compute and print the Mean Absolute Error (MAE), Mean
Squared Error (MSE), Root Mean Squared Error (RMSE), and R-squared (R^2) for a
regression model using scikit-learn. Assume you have the true target values (y_true) and
predicted values (y_pred).
13.9.2. Links
https://www.javatpoint.com/performance-metrics-in-machine-learning
Natural language toolkit (NLTK): The Natural Language Toolkit (NLTK) is the
most popular Natural Language Processing Library (NLP), written in Python, and
has very strong community support behind it. NLTK is also very easy to use and , it
is the simplest natural language processing (NLP) library.For installation NLTK
Windows/Linux/Mac, you can install NLTK using the pip command.To install
NLTK for the first time, you need to install the NLTK extension package. This will
bring up the NLTK download window to choose which packages need to be
installed:
TensorFlow and Keras: TensorFlow is a powerful deep learning library, and Keras
is a high-level neural networks API that runs on top of TensorFlow. They are
commonly used for building and training deep learning models for NLP tasks like
text classification, sentiment analysis, and machine translation.
PyTorch: PyTorch is another popular deep learning library that provides dynamic
computational graphs and a flexible framework for building neural networks. It is
widely used for NLP tasks, including language modeling, text generation, and
machine translation.
2. How can you use the CountVectorizer from the scikit-learn library to create a
BoW representation of a collection of text documents? Provide an example.
Data Preparation:
Create a word co-occurrence matrix that captures how often words co-
occur in the context of each other.
Define the loss function based on the word co-occurrence matrix and
word vectors.
Optimization:
Training:
Here's a simplified code snippet to give you an idea of how you might start
implementing GloVe using Python:
Output:
https://www.wired.com/insights/2014/02/growing-importance-natural-language-processing/
https://radimrehurek.com/gensim/models/keyedvectors.html
1. Artificial Intelligence (AI): The field of computer science that focuses on creating systems or algorithms
capable of performing tasks that typically require human intelligence, such as understanding natural
language, recognizing patterns, and making decisions.
2. Machine Learning (ML): A subfield of AI that involves the development of algorithms that enable
computers to learn and improve their performance on a specific task through experience and data.
3. Deep Learning: A subset of machine learning that uses neural networks with multiple layers (deep neural
networks) to automatically learn patterns and representations from data.
4. Neural Network: A computational model inspired by the structure and function of the human brain, used in
machine learning and deep learning for tasks such as image recognition and natural language processing.
5. Supervised Learning: A type of machine learning in which an algorithm learns from labeled training data
to make predictions or classify new, unseen data.
6. Unsupervised Learning: A type of machine learning in which an algorithm learns patterns and structures in
data without explicit labels or supervision.
7. Reinforcement Learning: A machine learning paradigm where an agent learns to make decisions by
interacting with an environment and receiving rewards or penalties based on its actions.
8. Natural Language Processing (NLP): The branch of AI that focuses on the interaction between computers
and human language, enabling machines to understand, interpret, and generate human language.
9. Feature Engineering: The process of selecting, transforming, or creating relevant features from raw data to
improve the performance of machine learning models.
10. Data Preprocessing: The tasks involved in cleaning, formatting, and transforming raw data into a suitable
format for training machine learning models.
11. Overfitting: A common issue in machine learning where a model performs well on the training data but
poorly on unseen data, indicating that it has learned noise rather than meaningful patterns.
12. Bias-Variance Trade-off: A fundamental concept in machine learning, where a model's ability to generalize
is balanced between underfitting (high bias) and overfitting (high variance).
13. TensorFlow: An open-source machine learning framework developed by Google that is widely used for
building and training deep learning models.
14. PyTorch: An open-source deep learning framework developed by Facebook's AI Research lab (FAIR) that
is known for its flexibility and dynamic computation graph.
15. Hyperparameter: A configuration setting that is not learned from data but is set prior to training a machine
learning model, such as learning rate or the number of hidden layers in a neural network.
16. Algorithm: A set of step-by-step instructions for solving a specific problem or task, often used
interchangeably with "model" in the context of machine learning.
17. Bias: Systematic errors or inaccuracies in a model's predictions or decisions, often resulting from flaws in
the data or the model's design.
18. Variance: The variability of a model's predictions when trained on different subsets of the data, indicating
how sensitive the model is to changes in the training data.
19. Cross-Validation: A technique used to assess a model's performance by splitting the data into multiple
subsets for training and testing to obtain a more robust evaluation.