0% found this document useful (0 votes)
30 views84 pages

Aiml Notes

ARTIFICIAL INTELLIGENCE

Uploaded by

pony20101998
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views84 pages

Aiml Notes

ARTIFICIAL INTELLIGENCE

Uploaded by

pony20101998
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 84

AIML

NOTES
Python Language Overview
Python is a high-level, interpreted, interactive and object-oriented scripting
language. Python is designed to be highly readable. It uses English keywords
frequently where as other languages use punctuation, and it has fewer syntactical
constructions than other languages. It is case sensitive language.
 Python is interpreted: Python is processed at runtime by the interpreter. You
do not need to compile your program before executing it. This is similar to PERL
and PHP.
 Python is Interactive: you can actually sit at a Python prompt and interact with
the interpreter directly to write your programs.

 Python is Object-Oriented: Python supports Object-Oriented style or technique


of programming that encapsulates code within objects.

 Python is a Beginner's Language: Python is a great language for the beginner-


level programmers and supports the development of a wide range of applications
from simple text processing to WWW browsers to games.
Python Features
1. Easy-to-learn: Python has few keywords, simple structure, and a clearly defined
syntax. This allows the student to pick up the language quickly.
2. Easy-to-read: Python code is more clearly defined and visible to the eyes.
3. Easy-to-maintain: Python's source code is fairly easy-to-maintain.
4. A broad standard library: Python's bulk of the library is very portable and
cross-platform compatible on UNIX, Windows, and Macintosh.
5. Interactive Mode: Python has support for an interactive mode which allows
interactive testing and debugging of snippets of code.
6. Portable: Python can run on a wide variety of hardware platforms and has the
same interface on all platforms.
7. Extendable: You can add low-level modules to the Python interpreter. These
modules enable programmers to add to or customize their tools to be more
efficient.
8. Databases: Python provides interfaces to all major commercial databases.
2
Programming Constructs
 Values:-
Values are the basic building blocks of data in Python. They can be numbers, strings,
lists, tuples, dictionaries, and more. Every piece of data in Python is represented by
a value.
Example:-
# Numeric values
number = 42
pi = 3.14

# String values
name = "John"
message = "Hello, World!"

# List value
fruits = ["apple", "banana", "orange"]

# Dictionary value
person = {"name": "John", "age": 30}

 Types:-
In Python, each value has a specific type that defines the kind of data it represents.
For example, integers, floating-point numbers, strings, lists, tuples, and dictionaries
are all different types in Python.
Example:-
# Numeric types
num_type = type(42) # int
float_type = type(3.14) # float

# String type
str_type = type("Hello, World!") # str

# List and dictionary types


list_type = type([1, 2, 3]) # list
3
dict_type = type({"name": "John", "age": 30}) # dict

 Keywords:-
Keywords are reserved words in Python that have a special meaning and cannot be
used as variable names. They are used to define the structure and flow of the code.
Example:-
if True:
# 'if' is a keyword
pass

def my_function():
# 'def' is a keyword
pass

 Statements:-
Statements are individual lines of code that perform actions or computations. They
can be simple or compound, like loops or conditionals.
Example:-
x = 10 # Assignment statement

if x > 5: # If statement
print("x is greater than 5")

for i in range(3): # For loop statement


print(i)

 Expressions:-
Expressions are combinations of values, variables, and operators that evaluate to a
single value. They can involve arithmetic, comparisons, and other operations.
Example:-
# Arithmetic expressions
result1 = 2 + 3 * 5 # Result: 17
result2 = (2 + 3) * 5 # Result: 25

4
# Comparison expressions
greater_than = 5 > 3 # Result: True
equal_to = 10 == 5 # Result: False

 Variables:-
Variables are used to store values in Python. They act as placeholders for data and
allow us to refer to values by their names.
Example:-
name = "John" # 'name' is a variable storing the value "John"

age = 30 # 'age' is a variable storing the value 30

fruits = ["apple", "banana", "orange"] # 'fruits' is a variable storing a list

Data Structures
 Lists:-

1. It is a general purpose most widely used in data structures.


2. It is a collection which is ordered and changeable and allows duplicate members.
3. To use a list, you must declare it first. Do this using square brackets and separate
values with commas.
4. Lists are ordered collections of elements that can be of different data types.
5. They are mutable, which means you can modify their elements after creation.
6. Elements in a list are indexed, and indexing starts from 0.

List Methods:-

Python provides a variety of built-in methods that you can use to manipulate lists.

Some commonly used list methods:

1. append(): Adds an element to the end of the list.

Example:-

fruits = ["apple", "banana"]

fruits.append("orange")

# fruits: ["apple", "banana", "orange"]


5
2. insert(): Inserts an element at a specific index in the list.

Example:-

fruits = ["apple", "banana"]

fruits.insert(1, "grape")

# fruits: ["apple", "grape", "banana"]

3. remove(): Removes the first occurrence of a specific element from the list.

Example:-

fruits = ["apple", "grape", "banana", "orange"]

fruits.remove("grape")

# fruits: ["apple", "banana", "orange"]

4. pop(): Removes the element at the specified index or the last element if no index
is provided, and returns the removed element.

Example:-

fruits = ["apple", "banana", "orange"]

removed_fruit = fruits.pop(1)

# fruits: ["apple", "orange"]

# removed_fruit: "banana"

5. index(): Returns the index of the first occurrence of a specified element in the
list.

Example:-

fruits = ["apple", "banana", "orange"]

index = fruits.index("banana")

# index: 1

6. count(): Returns the number of occurrences of a specific element in the list.

6
Example:-

fruits = ["apple", "banana", "orange", "banana"]

count = fruits.count("banana")

# count: 2

7. sort(): Sorts the list in ascending order (for numbers and strings) or in
alphabetical order (for strings).

Example:-

numbers = [4, 2, 7, 1, 5]

numbers.sort()

# numbers: [1, 2, 4, 5, 7]

8. reverse(): Reverses the order of the elements in the list.

Example:-

fruits = ["apple", "banana", "orange"]

fruits.reverse()

# fruits: ["orange", "banana", "apple"]

9. clear(): Removes all elements from the list, making it empty.

Example:-

fruits = ["apple", "banana", "orange"]

fruits.clear()

# fruits: []

 Tuples:-

1. Tuples are ordered collections of elements, similar to lists, but they are
immutable, meaning once created, their elements cannot be changed.
2. Tuples are generally used when you want to ensure data integrity and prevent
accidental changes.
3. Since tuples are immutable, you cannot modify their elements after creation.
7
Tuple Methods:-

1. count(): Returns the number of occurrences of a specific element in the tuple.

Example:-

my_tuple = (1, 2, 3, 2, 4, 2)

count = my_tuple.count(2)

# count: 3

2. index(): Returns the index of the first occurrence of a specified element in the
tuple.

Example:-

my_tuple = (10, 20, 30, 40, 30)

index = my_tuple.index(30)

# index: 2

 Dictionaries:-

1. Dictionaries are unordered collections of key-value pairs.

2. Each key in a dictionary must be unique, and the values can be of different data
types.

3. Dictionaries are highly efficient for data retrieval based on keys.

Dictionaries Methods:-
Dictionaries are a powerful data structure that stores data in key-value pairs. They
allow fast access to values based on their keys.
1. keys(): Returns a view object of all the keys in the dictionary.
Example:-
my_dict = {"name": "John", "age": 30, "city": "New York"}
keys = my_dict.keys()
# keys: ["name", "age", "city"]

8
2. values(): Returns a view object of all the values in the dictionary.
Example:-
my_dict = {"name": "John", "age": 30, "city": "New York"}
values = my_dict.values()
# values: ["John", 30, "New York"]

3. items(): Returns a view object of all the key-value pairs in the dictionary as
tuples.
Example:-
my_dict = {"name": "John", "age": 30, "city": "New York"}
items = my_dict.items()
# items: [("name", "John"), ("age", 30), ("city", "New York")]
4. get(): Retrieves the value associated with a given key. It returns a default value
if the key is not found (instead of raising an error).
Example:-
my_dict = {"name": "John", "age": 30}
name = my_dict.get("name", "Unknown")
city = my_dict.get("city", "Unknown")
# name: "John"
# city: "Unknown" (since "city" key is not present)

5. pop(): Removes and returns the value associated with the specified key.
Example:-
my_dict = {"name": "John", "age": 30}
age = my_dict.pop("age")
# age: 30, my_dict: {"name": "John"}

6. popitem(): Removes and returns the last key-value pair added to the dictionary
(since Python 3.7, dictionaries maintain insertion order).
Example:-

9
my_dict = {"name": "John", "age": 30, "city": "New York"}
item = my_dict.popitem()
# item: ("city", "New York"), my_dict: {"name": "John", "age": 30}

7. clear(): Removes all elements from the dictionary, making it empty.


Example:-
my_dict = {"name": "John", "age": 30}
my_dict.clear()
# my_dict: {}

8. update(): Updates the dictionary with key-value pairs from another dictionary
or an iterable of key-value pairs.
Example:-
my_dict = {"name": "John", "age": 30}
my_dict.update({"city": "New York"})
# my_dict: {"name": "John", "age": 30, "city": "New York"}

Python Functions
Python functions are blocks of reusable code that perform a specific task. They
allow you to break down your code into smaller, more manageable pieces, making
it easier to understand, maintain, and reuse. Functions in Python are defined using
the (def) keyword, followed by the function name, parameters (if any), and a colon.
Syntax:-
def function_name(parameter1, parameter2):
# function body
# write some action
return value

Example:-
1. Function without parameters and return value:-
def greet():
10
print("Hello, how are you?")
# Calling the function
greet() # Output: Hello, how are you?

2. Function with parameters and return value:-


def add_numbers(a, b):
return a + b
# Calling the function and storing the result in a variable
result = add_numbers(5, 3)
print(result) # Output: 8

3. Function with default parameter value:-


def greet_user(name="User"):
print(f"Hello, {name}!")
# Calling the function without specifying a parameter
greet_user() # Output: Hello, User!
# Calling the function with a parameter
greet_user("John") # Output: Hello, John!

4. Function with a variable number of arguments (using *args and kwargs):


def print_arguments(*args, kwargs):
print("Positional arguments:", args)
print("Keyword arguments:", kwargs)
# Calling the function with different arguments
print_arguments(1, 2, 3, a='apple', b='banana')
# Output:
# Positional arguments: (1, 2, 3)
# Keyword arguments: {'a': 'apple', 'b': 'banana'}

11
5. Recursive function (a function that calls itself):
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
result = factorial(5)
print(result) # Output: 120 (5! = 5 * 4 * 3 * 2 * 1)

Modules and Packages


Modules and packages are mechanisms to organize and reuse code efficiently. They
help break down large programs into smaller, manageable pieces and allow code to
be shared among different parts of an application or even across different projects.
Using modules and packages helps keep code organized, promotes reusability, and
makes it easier to manage larger Python projects.
 Module:-
A module is a single file containing Python definitions and statements. It acts as a
container for related code and can be imported into other Python scripts to use its
functionality.
Example:-
You have a file named `math_operations.py` that contains some math-related
functions:
Program:-
# math_operations.py
def add(a, b):
return a + b

def subtract(a, b):


return a - b

12
Now, you can import this module in another Python script and use the
functions it provides:
Program:-
# main.py
import math_operations

result1 = math_operations.add(5, 3)
result2 = math_operations.subtract(10, 4)

print(result1) # Output: 8
print(result2) # Output: 6

We have created a module named `math_operations.py`, and then in another script


`main.py`, we import the module using the `import` statement. We can then access
the functions `add()` and `subtract()` defined in the `math_operations` module.

 Package:-
A package is a collection of multiple modules that are organized in a directory
structure. It enables you to group related modules together under a common
namespace.
Example:-
Let's create a package called `shapes` that contains two modules, `circle.py` and
`rectangle.py`, each defining functions related to their respective shapes.
Package:-
shapes/
|-- __init__.py
|-- circle.py
|-- rectangle.py
 Program 1
# circle.py

13
import math
def area(radius):
return math.pi * radius 2

 Program 2
# rectangle.py
def area(length, width):
return length * width
The `shapes` directory contains an `__init__.py` file, which is required to treat the
directory as a package.
Now, we can use the `circle` and `rectangle` modules from the `shapes` package in
our main script:
Program:-
# main.py
from shapes import circle, rectangle
circle_area = circle.area(5)
rectangle_area = rectangle.area(4, 6)

print(circle_area) # Output: 78.53981633974483 (approx)


print(rectangle_area) # Output: 24
We created a package named `shapes` that contains two modules, `circle` and
`rectangle`. We then import these modules into our main script and use their
functions to calculate the area of a circle and a rectangle.
 Different ways to define a Module:-

1. Single File Module:-


The most common way to define a module is by creating a single Python file with
the desired functions, classes, or variables. This file will have a `.py` extension, and
its name will be used as the module name when importing it into other scripts.
Example:-
14
If you have a file named `math_operations.py` containing some math-related
functions:
Program:-
# math_operations.py
def add(a, b):
return a + b
def subtract(a, b):
return a – b

2. Built-in Modules:-
Python comes with a set of built-in modules that are available for use without any
additional installation. These modules provide a wide range of functionalities and
cover areas such as math, file handling, networking, and more. To use built-in
modules, you simply import them into your script.
Program:-
import math
result = math.sqrt(25)
print(result) # Output: 5.0

3. Standard Library Modules:-


Python's standard library includes a vast collection of modules that provide
additional functionality beyond the built-in modules. These modules cover various
areas like working with dates, handling data, interacting with the operating system,
etc. Standard library modules are also imported like built-in modules.
Program:-
import datetime
current_date = datetime.date.today()
print(current_date) # Output: 2023-07-30

4. Third-Party Modules:-

15
Python also allows you to use third-party modules developed by the Python
community. These modules are not part of the Python distribution and need to be
installed separately using package managers like `pip`.
To use a third-party module, you first need to install it, and then you can import and
utilize its functionalities in your scripts.
Program:-
# Installation using pip: pip install pandas
import pandas as pd
data = [1, 2, 3, 4, 5]
series = pd.Series(data)
print(series)

 Commonly used Built-in modules:-

1. math: Provides mathematical functions and constants.

2. random: Generates random numbers and elements.

3. datetime: Allows working with dates and times.

4. os: Provides functions for interacting with the operating system.

5. sys: Provides access to some variables used or maintained by the interpreter and
functions that interact with the interpreter.

6. re: Allows working with regular expressions for pattern matching.

7. json: Provides functions for working with JSON (JavaScript Object Notation)
data.

8. csv: Allows reading and writing CSV (Comma Separated Values) files.

Exception Handling
 Exception handling is essential for writing robust and error-resistant code.

16
 It allows you to handle potential problems gracefully and provides an opportunity
to log or report errors, which is crucial for debugging and maintaining the code's
stability.

 It allows you to gracefully handle errors or exceptional situations that may occur
during the execution of a program.

 It helps prevent the program from crashing and allows you to take appropriate
actions when errors occur.

 Python provides a straight forward syntax for handling exceptions using the try,
except, else, and finally blocks.

1. try: The try block contains the code that might raise an exception. It is the part of
the code where you anticipate potential errors.

2. except: The except block is used to specify how to handle a specific type of
exception. If an exception occurs in the try block and matches the specified type,
the code inside the except block is executed.

3. else: The else block is optional and used to specify code that should be executed
only if no exception occurs in the try block.

4. finally: The finally block is optional and will always be executed, regardless of
whether an exception occurred or not. It's commonly used for clean-up tasks, like
closing files or releasing resources.
Program:-
try:
num1 = int(input("Enter a number: "))
num2 = int(input("Enter another number: "))
result = num1 / num2
print("Result:", result)

except ValueError:
17
print("Please enter valid integers.")

except ZeroDivisionError:
print("Cannot divide by zero.")

else:
print("No exceptions occurred.")

finally:
print("This block always gets executed, regardless of exceptions.")

NumPy Library
NumPy is a powerful library for numerical computing in Python. It stands for
"Numerical Python" and provides support for large, multi-dimensional arrays and
matrices, along with a collection of mathematical functions to operate on these
arrays efficiently. NumPy is widely used in data science, machine learning, and
scientific computing due to its speed and versatility.
 Arrays: The core data structure in NumPy is the `ndarray`, which stands for n-
dimensional array. It is similar to Python lists but allows for more efficient and
faster operations on large datasets.

 Creating Arrays: You can create NumPy arrays using the `numpy.array()`
function or other specific functions like `numpy.zeros()`, `numpy.ones()`, or
`numpy.arange()`.
Program:-
import numpy as np
# Creating a 1D array
arr1 = np.array([1, 2, 3, 4, 5])
# Creating a 2D array
arr2 = np.array([[1, 2, 3], [4, 5, 6]])
 Arithmetic Operations: NumPy allows you to perform various operations on
arrays, such as element-wise addition, subtraction, multiplication, and division.
18
1. Addition: The `np.add()` function is used to add two arrays element-wise or add
a constant value to all elements in an array.
Example:-
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
result = np.add(arr1, arr2)
print(result) # Output: [5 7 9]

2. Subtraction: The `np.subtract()` function is used to subtract one array from


another element-wise or subtract a constant value from all elements in an array.
Example:-
import numpy as np
arr1 = np.array([5, 10, 15])
arr2 = np.array([2, 3, 4])
result = np.subtract(arr1, arr2)
print(result) # Output: [3 7 11]

3. Multiplication: The `np.multiply()` function is used to perform element-wise


multiplication of two arrays or multiply all elements in an array by a constant
value.
Example:-
import numpy as np
arr1 = np.array([2, 3, 4])
arr2 = np.array([5, 6, 7])
result = np.multiply(arr1, arr2)
print(result) # Output: [10 18 28]

19
4. Division: The `np.divide()` function is used to perform element-wise division of
two arrays or divide all elements in an array by a constant value.
Example:-
import numpy as np
arr1 = np.array([10, 20, 30])
arr2 = np.array([2, 5, 10])
result = np.divide(arr1, arr2)
print(result) # Output: [ 5. 4. 3.]

5. Exponentiation: The `np.power()` function is used to raise each element in an


array to a given power.
Example:-
import numpy as np
arr = np.array([2, 3, 4])
result = np.power(arr, 2)
print(result) # Output: [ 4 9 16]

Program:-
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
# Element-wise addition
result_add = arr1 + arr2 # Output: [5, 7, 9]
# Element-wise multiplication
result_mul = arr1 * arr2 # Output: [4, 10, 18]

 Array Functions: NumPy provides many mathematical functions that operate


on arrays efficiently. These include functions like `numpy.sum()`,
`numpy.mean()`, `numpy.min()`, and `numpy.max()`.

20
Program:-
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
# Sum of array elements
sum_arr = np.sum(arr) # Output: 15
# Mean of array elements
mean_arr = np.mean(arr) # Output: 3.0
# Minimum and maximum elements
min_val = np.min(arr) # Output: 1
max_val = np.max(arr) # Output: 5

 Indexing and Slicing: You can access elements of NumPy arrays using indexing
and perform slicing operations to get subsets of the array.
Program:-
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
# Accessing a specific element
print(arr[2]) # Output: 3
# Slicing
print(arr[1:5]) # Output: [2, 3, 4, 5]

It provides powerful tools for working with arrays, handling large datasets, and
performing complex mathematical operations efficiently.

Broadcasting and Numpy function


 Broadcasting:-
Broadcasting is a feature that allows arrays of different shapes to be combined and
operated upon without explicitly having the same dimensions. This makes it easier
to perform mathematical operations on arrays of different sizes, and it simplifies the
code by eliminating the need for explicit loops.
21
When performing operations between two arrays with different shapes, NumPy
automatically broadcasts the smaller array to match the shape of the larger array, so
the operation can be applied element-wise.
Broadcasting Rules:-
1. The array's dimensions are compared from the trailing dimensions towards the
leading dimensions.

2. The dimensions must either be equal, one of them must be 1, or one of them must
not exist.
Program:-
import numpy as np
# Broadcasting example
a = np.array([1, 2, 3])
b = np.array([10, 20, 30])
# Element-wise addition between a and b
result = a + b
print(result) # Output: [11 22 33]

 NumPy Functions:-
NumPy is a powerful library in Python for numerical computing. It provides a wide
range of functions for various mathematical and array manipulation operations.
Functions:-
np.array(): Create a numpy array from a Python list or tuple.
np.zeros(): Create an array filled with zeros.
np.ones(): Create an array filled with ones.
np.arange(): Create an array with evenly spaced values within a given interval.
np.linspace(): Create an array with a specified number of evenly spaced values
within a given interval.
np.reshape(): Reshape an array into a specified shape.

22
np.sum(): Compute the sum of array elements.
np.mean(): Compute the mean of array elements.
np.max(): Find the maximum value in an array.
np.min(): Find the minimum value in an array.
np.dot(): Compute the dot product of two arrays.
np.transpose(): Transpose an array.

Program:-
import numpy as np
# Create an array from a list
arr1 = np.array([1, 2, 3, 4, 5])
# Create an array filled with zeros
arr2 = np.zeros(5)
# Create an array filled with ones
arr3 = np.ones(5)
# Create an array with evenly spaced values from 0 to 9
arr4 = np.arange(10)
# Compute the sum of array elements
sum_arr1 = np.sum(arr1)
# Find the maximum value in an array
max_arr4 = np.max(arr4)
print(arr1)
print(arr2)
print(arr3)
print(arr4)
print(sum_arr1)
print(max_arr4)
23
Pandas Library
Pandas is a powerful Python library for data manipulation and analysis. It provides
data structures like Data Frames and Series, which allow you to handle and process
data efficiently.
The Data Frame is the most commonly used data structure in pandas and is similar
to a table in a relational database or an Excel spreadsheet. It allows you to store and
manipulate data in a tabular format with rows and columns.
Example:-
1. Importing pandas:-
import pandas as pd

2. Creating a DataFrame:-
# Create a DataFrame from a dictionary
data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'City': ['New York', 'London', 'San Francisco']
}
df = pd.DataFrame(data)
print(df)

3. Accessing columns:-
# Access a single column
ages = df['Age']
print(ages)

4. Accessing rows:-
# Access a single row by index
row1 = df.loc[1]
print(row1)

24
5. Data Manipulation:-
# Adding a new column
df['Salary'] = [50000, 60000, 70000]

# Dropping a column
df.drop(columns='City', inplace=True)

# Performing calculations on columns


df['Age_in_5_years'] = df['Age'] + 5
print(df)

6. Data Input/Output:-
# Reading data from CSV file
data = pd.read_csv('data.csv')

# Writing data to CSV file


df.to_csv('output.csv', index=False)

Aggregation Function
In pandas, aggregation functions are used to perform summary operations on data
in a DataFrame. These functions help to collapse multiple data points into a single
value, providing valuable insights into the dataset.
Some commonly used aggregation functions in pandas include sum, mean, median,
min, max, count, std, var, and many others.
Example:-
import pandas as pd
 # Sample DataFrame
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eva'],
'Age': [25, 30, 35, 28, 40],
'Salary': [50000, 60000, 70000, 55000, 80000]
25
}
df = pd.DataFrame(data)

 # Sum of the 'Salary' column


sum_salary = df['Salary'].sum()
print("Sum of 'Salary' column:", sum_salary)
Output: Sum of 'Salary' column: 315000

 # Mean of the 'Age' column


mean_age = df['Age'].mean()
print("Mean of 'Age' column:", mean_age)
Output: Mean of 'Age' column: 33.6

 # Median of the 'Salary' column


median_salary = df['Salary'].median()
print("Median of 'Salary' column:", median_salary)
Output: Median of 'Salary' column: 60000.0

 # Minimum value in the 'Age' column


min_age = df['Age'].min()
print("Minimum value in 'Age' column:", min_age)
Output: Minimum value in 'Age' column: 25

 # Maximum value in the 'Salary' column


max_salary = df['Salary'].max()
print("Maximum value in 'Salary' column:", max_salary)
Output: Maximum value in 'Salary' column: 80000

 # Count of non-null values in the 'Name' column


count_name = df['Name'].count()
26
print("Count of non-null values in 'Name' column:", count_name)
Output: Count of non-null values in 'Name' column: 5

 # Standard deviation of the 'Salary' column


std_salary = df['Salary'].std()
print("Standard deviation of 'Salary' column:", std_salary)
Output: SD of 'Salary' column: 11891.120049505065

 # Variance of the 'Age' column


var_age = df['Age'].var()
print("Variance of 'Age' column:", var_age)
Output: Variance of 'Age' column: 29.2

Visualization using matplotlib and seaborn


‘matplotlib’ and ‘seaborn’ are Python libraries that help you create visualizations
(graphs, charts, plots) from your data. They are widely used in data analysis, data
visualization, and scientific computing.
 Matplotlib:-
`matplotlib` is a powerful library for creating static, interactive, and animated
visualizations in Python. It provides a wide range of plotting functions to create
various types of plots, such as line plots, scatter plots, bar plots, histograms, and
more.
Line Plot Example:-
import matplotlib.pyplot as plt

# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

# Create a line plot


plt.plot(x, y)

27
# Add labels and title
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("Simple Line Plot")

# Display the plot


plt.show()

 Seaborn:-
`seaborn` is a higher-level library built on top of `matplotlib`, designed to create
attractive and informative statistical graphics. It simplifies the process of creating
complex visualizations and enhances the default aesthetics.
Scatter Plot Example:-
import seaborn as sns

# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

# Create a scatter plot


sns.scatterplot(x, y)

# Add labels and title


plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("Simple Scatter Plot")

# Display the plot


plt.show()
Both libraries work well together, and you can choose the one that suits your
visualization needs. `matplotlib` provides more customization options, while
`seaborn` is convenient for quickly creating attractive visualizations with less code.

28
CSV (Comma Separated Values)
CSV stands for Comma-Separated Values. It is a file format used to store tabular
data, such as spreadsheets or databases, in a plain text format. In a CSV file, each
line represents a row of data, and within each line, the individual values are
separated by commas. CSV files provide a lightweight and flexible way to store and
exchange structured data, making them a popular choice in various fields.

Characteristics:-
1. Plain Text Format: CSV files are plain text files that can be easily read and
edited with a text editor or spreadsheet software.

2. Tabular Structure: CSV files are structured as tables, with rows representing
records and columns representing fields or attributes of the data.

3. Comma Separation: The values within each row are separated by commas.
However, other delimiters like semicolons or tabs can also be used depending on
the regional settings or specific requirements.

4. No Data Type Enforcement: CSV files do not enforce any specific data types
for the values. All values are treated as strings unless explicitly converted by the
software reading the file.

5. No Cell Formatting: CSV files do not preserve any formatting such as font
styles, colors, or formulas. They focus solely on storing the raw data.
Example:-
Name,Age,Department,Salary
John Doe,30,Marketing,50000
Jane Smith,35,Finance,60000
Mark Johnson,28,IT,55000

CSV Uses:-
1. Data Import/Export: CSV files are widely used to import or export data
between different software systems or databases. They provide a standardized
format that can be easily understood and processed by different applications.
29
2. Data Analysis: CSV files are often used in data analysis tasks. Data scientists
and analysts can import CSV files into tools like Excel, Python, R, or SQL
databases to perform statistical analysis, generate reports, or visualize the data.

3. Data Interchange: CSV files can be shared and exchanged between different
users or systems. They provide a simple and widely supported format for sharing
data, making it easier to collaborate or integrate data from multiple sources.

4. Web Development: CSV files can be used to store and exchange data in web
applications. For example, they can be used to upload and download data from
web forms or to provide data feeds for other applications.

Machine Learning
Machine learning is a subset of artificial intelligence (AI) that focuses on enabling
computers to learn and make predictions or decisions without being explicitly
programmed. It involves developing algorithms and models that allow machines to
learn from data and improve their performance over time.
(Machine learning is like teaching a computer to recognize patterns and make
decisions based on examples.)

Machine learning can be categorized into three main types:-


 Supervised Learning: The algorithm learns from labeled data, where the desired
output or "label" is already known. The algorithm analyzes the input data along
with the corresponding labels and learns the mapping between them. This allows
the algorithm to make predictions or classify new, unseen data based on the
patterns it has learned.
Example  Email spam classification, image recognition, sentiment analysis, and
stock price prediction.

30
 Unsupervised Learning: The algorithm learns from unlabeled data, where there
are no predefined labels or desired outputs. The algorithm analyzes the input data
and identifies patterns, structures, or relationships within the data. It is often used
for tasks such as clustering, where the algorithm groups similar data points
together based on their characteristics.
Example  Customer segmentation, image compression, market basket analysis,
and outlier detection.

 Reinforcement Learning: The algorithm learns through interaction with an


environment. The algorithm takes actions in the environment and receives
feedback in the form of rewards or penalties. It learns to maximize the rewards
by discovering the optimal sequence of actions. It is commonly used in
applications such as game playing, robotics, and autonomous systems.
Example  It can learn to play a game by trial and error, receiving rewards for
successful moves and penalties for unsuccessful moves.

 Semi-Supervised Learning:- It is a type of machine learning where a model is


trained using a combination of labeled and unlabeled data. It combines the both
supervised and unsupervised learning to improve the model's performance. It can
utilize both labeled and unlabeled data to train the model. It is particularly useful
when there is a large amount of unlabeled data available, but it’s too expensive
or difficult to label all of it.

31
Example  Text classification, Image classification, Anomaly detection, Speech
analysis, Internet content classification.

Types of supervised Machine learning

It can be further divided into two types:-

 Regression:-

Regression algorithms are used if there is a relationship between the input variable
and the output variable. It is used for the prediction of continuous variables, such as
Weather forecasting, Market Trends, etc.

Some popular Regression algorithms:-

1. Linear Regression
2. Regression Trees
3. Non-Linear Regression
4. Bayesian Linear Regression
5. Polynomial Regression

 Classification:-

Classification algorithms are used when the output variable is categorical, which
means there are two classes such as Yes-No, Male-Female, True-false, etc.

Some popular Classification algorithms:-

1. Random Forest
2. Decision Trees
32
3. Logistic Regression
4. Support vector Machines
5. KNN (K-Nearest Neighbors)

KNN Classifier Algorithm


 K-Nearest Neighbor is one of the simplest Machine Learning algorithms based
on Supervised Learning algorithm used for classification and regression tasks.

 It operates based on the assumption that similar instances are likely to have
similar class labels or values.

 It is a non-parametric algorithm, which means it does not make any assumption


on underlying data.

 It is also called a lazy learner algorithm because it does not learn from the training
set immediately instead it stores the dataset and at the time of classification, it
performs an action on the dataset.

KNN Classifier Works

Step-1: Select the number K of the neighbors.

33
Step-2: Calculate the Euclidean distance of K number of neighbors.

Step-3: Take the K nearest neighbors as per the calculated Euclidean distance.
Step-4: Among these k neighbors, count the number of the data points in each
category.
Step-5: Assign the new data points to that category for which the number of the
neighbor is maximum.
Step-6: Our model is ready.

KNN Program
# importing libraries
import numpy as nm
import matplotlib.pyplot as mtp
import pandas as pd
#importing datasets
data_set= pd.read_csv('user_data.csv')
#Extracting Independent and dependent Variable
x= data_set.iloc[:, [2,3]].values
y= data_set.iloc[:, 4].values
# Splitting the dataset into training and test set.
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test= train_test_split(x, y, test_size= 0.25, random_state
=0)
#feature Scaling
from sklearn.preprocessing import StandardScaler
st_x= StandardScaler()
34
x_train= st_x.fit_transform(x_train)
x_test= st_x.transform(x_test)
#Fitting K-NN classifier to the training set
from sklearn.neighbors import KNeighborsClassifier
classifier= KNeighborsClassifier(n_neighbors=5, metric='minkowski', p=2 )
classifier.fit(x_train, y_train)
#Predicting the test set result
y_pred= classifier.predict(x_test)
# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
Naïve Bayes Classifier
 Naïve Bayes algorithm is a supervised learning algorithm, which is based
on Bayes theorem and used for solving classification problems.
 It is mainly used in text classification that includes a high-dimensional training
dataset.
 Naïve Bayes Classifier is one of the simple and most effective Classification
algorithms which helps in building the fast machine learning models that can
make quick predictions.
 It is a probabilistic classifier, which means it predicts on the basis of the
probability of an object.
 Some popular examples of Naïve Bayes Algorithm are spam filtration,
Sentimental analysis, and classifying articles.

There are three types of Naïve Bayes model:-

1. Gaussian (generally we used this model )


2. Multinomial
3. Bernoulli

Naïve Bayes Algorithm works:-


Step 1: Calculate the prior probability for given class labels.
Step 2: Find Conditional probability with each attribute for each class.
Step 3: Multiple same conditional probability
Step 4: Multiple prior probability with step 3 probability.
35
Step 5: See which class has a higher probability, higher probability class belongs
to given the input set step.
Naïve Bayes Program
#Importing the libraries
import numpy as nm
import matplotlib.pyplot as mtp
import pandas as pd
# Importing the dataset
dataset = pd.read_csv('user_data.csv')
x = dataset.iloc[:, [2, 3]].values
y = dataset.iloc[:, 4].values
# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.25, random_stat
e = 0)
# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
x_train = sc.fit_transform(x_train)
x_test = sc.transform(x_test)
# Fitting Naive Bayes to the Training set
from sklearn.naive_bayes import GaussianNB
classifier = GaussianNB()
classifier.fit(x_train, y_train)
# Predicting the Test set results
y_pred = classifier.predict(x_test)
# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
SVM (Support Vector Machine)

 Support Vector Machine or SVM is one of the most popular Supervised Learning
algorithms, which is used for Classification as well as Regression problems.

36
 The goal of the SVM algorithm is to create the best line or decision boundary that
can segregate n-dimensional space into classes so that we can easily put the new
data point in the correct category in the future. This best decision boundary is
called a hyperplane.
 SVM chooses the extreme points/vectors that help in creating the hyperplane.
These extreme cases are called as support vectors, and hence algorithm is termed
as Support Vector Machine.

Main goals of SVM:-


 Maximize Margin: SVMs aim to find a decision boundary with the largest
possible margin between the data points of different classes. By maximizing the
margin, SVMs seek to achieve better generalization and robustness to new,
unseen data.

 Find a Non-linear Decision Boundary: SVMs can handle complex datasets by


using a technique called the kernel trick. This technique allows SVMs to
transform the input data into a higher-dimensional space, where a linear decision
boundary can be found.

 Handle Outliers: SVMs are robust to outliers, which are data points that deviate
significantly from other points. Outliers have a minimal impact on the decision
boundary because SVMs focus on maximizing the margin using the support
vectors.

 Handle High-dimensional Spaces: SVMs perform well even when the number
of features is much larger than the number of instances. This is known as the
"curse of dimensionality." SVMs are less prone to overfitting in high-
37
dimensional spaces, making them suitable for problems with a large number of
features.
Types of SVM:-
 Linear SVM: Linear SVM is used for linearly separable data, which means if a
dataset can be classified into two classes by using a single straight line.
 Non-linear SVM: Non-Linear SVM is used for non-linearly separated data,
which means if a dataset cannot be classified by using a straight line.

SVM Program
#Importing the libraries
import numpy as nm
import matplotlib.pyplot as mtp
import pandas as pd
# Importing the dataset
dataset = pd.read_csv('user_data.csv')
x = dataset.iloc[:, [2, 3]].values
y = dataset.iloc[:, 4].values
# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.25, random_stat
e = 0)
# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
x_train = sc.fit_transform(x_train)
x_test = sc.transform(x_test)
# "Support vector classifier"
from sklearn.svm import SVC
classifier = SVC(kernel='linear', random_state=0)
classifier.fit(x_train, y_train)
# Predicting the Test set results
y_pred = classifier.predict(x_test)
# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
38
cm = confusion_matrix(y_test, y_pred)
Decision Tree
 Decision Tree is a Supervised learning technique that can be used for both
classification and Regression problems, but mostly it is preferred for solving
Classification problems.
 It is a tree-structured classifier, where internal nodes represent the features of a
dataset, branches represent the decision rules and each leaf node represents the
outcome.
 There are two nodes, which are the Decision Node and Leaf Node. Decision
nodes are used to make any decision and have multiple branches, whereas Leaf
nodes are the output of those decisions and do not contain any further branches.
 The decisions or the test are performed on the basis of features of the given
dataset.
 It is called a decision tree because, similar to a tree, it starts with the root node,
which expands on further branches and constructs a tree-like structure.
 In order to build a tree, we use the CART algorithm, which stands
for Classification and Regression Tree algorithm.

Decision Tree Works


Step-1: Begin the tree with the root node, says S, which contains the complete
dataset.

Step-2: Find the best attribute in the dataset using Attribute Selection Measure
(ASM).

39
Step-3: Divide the S into subsets that contains possible values for the best attributes.

Step-4: Generate the decision tree node, which contains the best attribute.

Step-5: Recursively make new decision trees using the subsets of the dataset created
in step -3. Continue this process until a stage is reached where you cannot further
classify the nodes and called the final node as a leaf node.

There are two popular techniques for ASM:-

1. Entropy
2. Gini Index

Pruning:-

Pruning is a process of deleting the unnecessary nodes from a tree in order to get the
optimal decision tree.

A too-large tree increases the risk of overfitting, and a small tree may not capture
all the important features of the dataset. Therefore, a technique that decreases the
size of the learning tree without reducing accuracy is known as Pruning.

There are mainly two types of tree pruning technology used:-

 Cost Complexity Pruning


 Reduced Error Pruning

Decision Tree Program


#Importing the libraries
import numpy as nm
import matplotlib.pyplot as mtp
import pandas as pd
# Importing the dataset
dataset = pd.read_csv('user_data.csv')
x = dataset.iloc[:, [2, 3]].values
y = dataset.iloc[:, 4].values
# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.25, random_stat
e = 0)
40
# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
x_train = sc.fit_transform(x_train)
x_test = sc.transform(x_test)
#Fitting Decision Tree classifier to the training set
From sklearn.tree import DecisionTreeClassifier
classifier= DecisionTreeClassifier(criterion='entropy', random_state=0)
classifier.fit(x_train, y_train)
# Predicting the Test set results
y_pred = classifier.predict(x_test)
# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
Ensemble methods
Ensemble methods in classification involve combining multiple individual
classifiers or models to make more accurate predictions than any single model alone.
These methods leverage the diversity and collective knowledge of the individual
models to improve overall performance.
Some popular ensemble methods for classification:
1. Bagging (Bootstrap Aggregating): Bagging involves training multiple
instances of the same base classifier on different subsets of the training data. Each
classifier is trained independently, and the final prediction is made by
aggregating the predictions of all individual classifiers, often using voting or
averaging. Random Forest is a well-known ensemble method based on
bagging, using decision trees as the base classifiers.

2. Boosting: Boosting is a sequential ensemble method where each subsequent


model is built to correct the mistakes made by the previous models. Each model
is trained on a modified version of the training data, where instances that were
misclassified previously are given higher weights. Boosting algorithms, such as
AdaBoost (Adaptive Boosting) and Gradient Boosting, focus on improving the
overall performance by iteratively adding models and adjusting their weights.

41
3. Stacking: Stacking combines multiple heterogeneous models by training a meta-
model on the predictions of the individual models. The individual models serve
as base models that make predictions on the training data. The meta-model takes
these predictions as inputs and learns to make the final prediction. Stacking
leverages the different strengths and weaknesses of various models to create a
more powerful classifier.

4. Voting: Voting combines the predictions of multiple classifiers by aggregating


their outputs using a voting scheme. There are different types of voting, including
majority voting, where the class with the most votes is selected, and weighted
voting, where the classifiers' votes are weighted based on their confidence or
performance.

Ensemble methods are widely used in classification tasks as they often produce
better results than individual models. They can increase accuracy, improve
generalization, and handle complex datasets.

Machine Learning Evaluation Metrics (Confusion Matrix)


A confusion matrix, also known as an error matrix, is a tabular representation of the
performance of a classification model. The confusion matrix helps to evaluate the
performance of the classification model and understand the types of errors it is
making.
Example:-
Predicted Class
Actual Class Positive Negative
Positive TP FN
Negative FP TN
In the confusion matrix:
 True Positive (TP): The number of instances that were correctly predicted as
positive by the model.
 True Negative (TN): The number of instances that were correctly predicted as
negative by the model.
 False Positive (FP): The number of instances that were incorrectly predicted as
positive by the model (Type I error).
 False Negative (FN): The number of instances that were incorrectly predicted as
negative by the model (Type II error).
42
Performance:-
 Accuracy: The overall accuracy of the model, calculated as
(TP + TN) / (TP + TN + FP + FN).P
 Precision: The ability of the model to correctly identify positive instances,
calculated as
TP / (TP + FP)
 Recall: The proportion of actual positive instances correctly identified by the
model, calculated as
TP / (TP + FN)
 F1 Score: The harmonic mean of precision and recall, providing a balanced
measure between the two, calculated as
2 * ((Precision * Recall) / (Precision + Recall))

Cross Validation
Cross-validation is a technique used in machine learning to assess the performance
and generalization ability of a model. It helps in estimating how well the model will
perform on unseen data. The basic idea behind cross-validation is to split the
available data into multiple subsets, train the model on some of these subsets, and
evaluate its performance on the remaining subset.
Basic steps of cross-validations:-
 Reserve a subset of the dataset as a validation set.
 Provide the training to the model using the training dataset.
 Now, evaluate model performance using the validation set. If the model performs
well with the validation set, perform the further step, else check for the issues.

Method used for cross validation:-


1. Validation Set Approach
2. Leave-P-out cross-validation
3. Leave one out cross-validation
4. K-fold cross-validation
5. Stratified k-fold cross-validation

43
 K-Fold Cross-Validation: The dataset is divided into k subsets, and the model
is trained and evaluated k times, each time using a different subset for evaluation
and the remaining subsets for training.

 Stratified Cross-Validation: Similar to k-fold, but ensures that the distribution


of classes is preserved in each fold, useful for imbalanced datasets.

 Leave-One-Out Cross-Validation: Each data point is used as the validation set


once while the rest is used for training; repeated for all data points, resulting in n
iterations for n data points in the dataset.

 Validation Set Approach: We divide our input dataset into a training set and
test or validation set in the validation set approach. Both the subsets are given
50% of the dataset.

Applications:-

1. This technique can be used to compare the performance of different predictive


modeling methods.
2. It has great scope in the medical research field.
3. It can also be used for the meta-analysis, as it is already being used by the data
scientists in the field of medical statistics.

Computer Vision
Computer vision is a field of study that focuses on enabling computers to understand
and interpret visual information from images or videos. It involves developing
algorithms and techniques to extract meaningful information from visual data.
Some types of computer vision tasks with examples:-
1. Image Classification: Identifying and categorizing objects or scenes in images.
Example  classifying whether an image contains a cat or a dog.

2. Object Detection: Locating and identifying specific objects within an image.


Example  Detecting and drawing bounding boxes around cars in a street scene.

3. Semantic Segmentation: Assigning a label to each pixel in an image to segment


it into meaningful regions.
44
Example  Segmenting an image to distinguish between foreground and
background objects.

4. Instance Segmentation: Similar to semantic segmentation, but different


instances of the same object are individually labeled.
Example  Segmenting an image to separate different cars and pedestrians.

5. Object Tracking: Tracking the movement of specific objects across frames in a


video.
Example  tracking the trajectory of a soccer ball during a match.

6. Facial Recognition: Identifying and verifying individuals based on facial


features.
Example  recognizing a person's face to unlock a smartphone.

7. Pose Estimation: Estimating the pose or position of objects or human body part.
Example  determining the 3D pose of a person's joints from an image or video.

8. Image Captioning: Generating a descriptive caption or textual description for


an image.
Example  automatically generating a caption like "A group of people playing
soccer in a field."

9. Optical Character Recognition (OCR): Extracting text information from


images or documents.
Example  converting scanned documents into editable text files.

10. Depth Estimation: Estimating the depth or 3D information of a scene from a


2D image or video.
Example  Generating a depth map from a single image to understand the scene's
structure.

45
Face Recognition and Detection with OpenCV
 OpenCV:-

OpenCV is huge open-source library for the computer vision, machine learning, and
image processing and now it plays a major role in real-time operation which is very
important in today’s systems. When it integrated with various libraries, such as
NumPy, python is capable of processing the OpenCV array structure for analysis.

To identify image pattern and its various features we use vector space and perform
mathematical operations on these features. OpenCV is a computer vision library that
supports programming languages like Python, C++, and Java. The package was
initially created by Intel in 1999 and was later made open-source and released to the
public.

OpenCV provides a solid foundation for implementing these tasks, but additional
steps, such as data preprocessing, feature selection, and model training, may be
required to build a robust face recognition system. OpenCV can be used in
conjunction with deep learning frameworks like TensorFlow or PyTorch for such
applications.

Applications of OpenCV:-

1. face recognition
2. Automated inspection and surveillance
3. Vehicle counting on highways along with their speeds
4. Interactive art installations
5. Street view image stitching
6. Robot and driver-less car navigation and control
7. object recognition
8. Medical image analysis
9. Movies – 3D structure from motion
10. TV Channels advertisement recognition

46
 Face Detection:-

1. OpenCV provides pre-trained Haar cascades or deep learning-based models for


face detection.

2. To perform face detection, you can use the `CascadeClassifier` class in OpenCV,
which can load the pre-trained Haar cascades for face detection.

3. The Haar cascades or deep learning models are applied to the input image or
video frame, and they identify regions that potentially contain faces.

4. Detected faces are typically represented by bounding boxes that enclose the
detected face regions.

5. OpenCV provides functions like `detectMultiScale()` to detect faces using Haar


cascades and `dnn.readNetFromXXX()` to load deep learning models for face
detection.

 Face Recognition:-

1. Face recognition involves identifying or verifying individuals based on their


facial features.

2. OpenCV can be used in conjunction with other libraries like Dlib or Face
Recognition to implement face recognition algorithms.

3. The first step in face recognition is to capture and preprocess face images.
OpenCV provides functions for face alignment, normalization, and feature
extraction.

4. Feature extraction techniques like Local Binary Patterns (LBP) or Histogram of


Oriented Gradients (HOG) can be used to extract facial features.

5. Once the features are extracted, they are typically used to train a machine learning
model, such as a Support Vector Machine (SVM) or a Convolutional Neural
Network (CNN).

47
6. OpenCV provides machine learning functions and classes for training and using
these models, such as `cv2.ml.SVM` or `cv2.dnn.Net`.

7. During recognition, the trained model is applied to new face images to predict or
classify the individuals they belong to.

8. The predicted results can be used for tasks like face identification, verification,
or attendance systems.

 Face Detection using the OpenCV Implementation:-

Installing the OpenCV library is required before we can begin face detection in
Python.
pip install opencv-python
Once the library is installed, we can start writing our code. The relevant modules
must first be imported and read in an image as the early phase.

Program
import cv2
// load the cascade classifier for face detection //
face_cascade = cv2.CascadeClassifier('path/to/haarcascade_frontalface_default.xml')
// load the image //
img = cv2.imread('path/to/image.jpg')
// convert the image to grayscale //
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
// detect faces in the image //
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)
// draw a rectangle around the detected faces //
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
// display the image //
cv2.imshow('img', img)
48
cv2.waitKey(0)
cv2.destroyAllWindows()

CascadeClassifier:- The CascadeClassifier class to detect faces in the image. This


class takes in a pre-trained cascading classifier that can be used to detect faces in an
image. The classifier can be trained using a dataset of images of faces, and it uses a
combination of features such as edges, shapes, and textures to detect faces.

Detect MultiScale:- This method takes in the image and a few parameters such as
the scale factor and the minimum number of neighbors.

Scale factor:- It is used to control the size of the detection window, and the
minimum number of neighbors is used to control the number of false positives.

 Face Recognition using the OpenCV Implementation:-

Installing the OpenCV library is required before we can begin face detection in
Python.
pip install opencv-python
conda install -c conda-forge dlib
pip install face_recognition
Once the library is installed, we can start writing our code. The relevant modules
must first be imported and read in an image as the early phase.
Program
#defing the variable and reading the file
image_1 = cv2.imread("Elon_musk.png")

#it is interpreting the image in BGR format


rgb_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img_encoding = face_recognition.face_encodings(rgb_img)[0]

#accessing the image in the file from we have to match


image_2 = cv2.imread("images/Elon_musk.png")
rgb_img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)
49
img_encoding2 = face_recognition.face_encodings(rgb_img2)[0]

#For Matching the images for cases


final_result = face_recognition.compare_faces([img_encoding], img_encoding2)
print("final_result: ", final_result)

FaceRecognizer()
In OpenCV, the `FaceRecognizer` class provides a set of methods and
functionalities for face recognition. It is an abstract class that serves as a base for
implementing different face recognition algorithms. These FaceRecognizer classes
provide methods for training the model with labeled face images and performing
face recognition on new, unseen face images.
FaceRecognizer classes in OpenCV:-
 `cv2.face.LBPHFaceRecognizer`: Local Binary Patterns Histograms (LBPH) is
a simple yet effective face recognition algorithm. It extracts local binary patterns
from the face images and generates histograms as features for recognition.

 `cv2.face.EigenFaceRecognizer`: The Eigenfaces algorithm is a popular


method for face recognition. It uses principal component analysis (PCA) to
reduce the dimensionality of face images and represents faces as linear
combinations of eigenfaces.

 `cv2.face.FisherFaceRecognizer`: The Fisherfaces algorithm is another well-


known technique for face recognition. It combines Fisher's linear discriminant
analysis (LDA) with PCA to capture both discriminative and holistic features of
faces.
Program
# import openCV
import cv2

# Create an instance of the LBPHFaceRecognizer


recognizer = cv2.face.LBPHFaceRecognizer_create()

# Load the trained recognizer model from a file


50
recognizer.read('trained_model.yml')

# Load the input image for face recognition


image = cv2.imread('input_image.jpg')

# Convert the image to grayscale


gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Perform face recognition


label, confidence = recognizer.predict(gray)

# Display the predicted label and confidence


print(f"Predicted Label: {label}")
print(f"Confidence: {confidence}")

# Display the output image with the predicted label


cv2.imshow('Face Recognition', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

we create an instance of `cv2.face.LBPHFaceRecognizer` and load a trained model


from a file using the `read()` method. Then, we load an input image and convert it
to grayscale. Finally, we use the `predict()` method to perform face recognition on
the grayscale image and obtain the predicted label and confidence level.

Make sure to replace `'trained_model.yml'` and `'input_image.jpg'` with the paths to


your own trained model file and input image, respectively.

Artificial Neural Network


Artificial neural networks (ANNs) are computational models inspired by the
structure and functioning of the human brain. They are designed to process and
analyze complex data, recognize patterns, and make predictions or decisions based
on that data.

51
An ANN consists of interconnected nodes called artificial neurons or units. These
neurons are organized into layers, typically an input layer, one or more hidden
layers, and an output layer. Each neuron receives input from the neurons in the
previous layer, performs a computation, and passes the result to the neurons in the
next layer.

The connections between neurons in the network are represented by weights, which
determine the strength and significance of the information being passed. During the
training phase, the network learns by adjusting these weights based on the input data
and the desired output. This process is often done using a technique called
backpropagation, which compares the network's output with the expected output and
adjusts the weights accordingly.
Artificial Neural Networks Models:-
 Feedforward Neural Networks (FNN): In this type of network, information
flows in one direction, from the input layer to the output layer. It's like a data
pipeline where the input is processed layer by layer, and the output is generated.

 Convolutional Neural Networks (CNN): CNNs are mainly used for image and
video analysis. They have specialized layers called convolutional layers that
extract features from the input data, such as edges, textures, or shapes. CNNs are
effective in tasks like image recognition and object detection.

 Recurrent Neural Networks (RNN): RNNs are designed to process sequential


data, where the order of the input matters. They have loops within their
architecture, allowing information to flow not only from the input to the output
but also in a feedback loop from the output back to the network. RNNs are used
in tasks like language modeling, speech recognition, and sentiment analysis.

52
Artificial neural networks have become powerful tools in machine learning and have
been applied to a wide range of fields, including image and speech recognition,
natural language processing, recommendation systems, and more.

ANN Structure
An artificial neural network consists of interconnected nodes called artificial
neurons or units. These units are organized into layers. There are typically three
types of layers:

 Input Layer: This is the first layer of the network where the data is initially fed.
Each neuron in the input layer represents a feature or an attribute of the input
data.
Example  if you're training a network to recognize handwritten digits, each
neuron in the input layer could represent a pixel of an image.

 Hidden Layers: These layers are placed between the input layer and the output
layer. They are called "hidden" because we don't directly observe their outputs.
Each hidden layer contains multiple neurons, and the number of hidden layers
can vary depending on the complexity of the problem. The hidden layers perform
computations on the input data and progressively extract more complex features
as we move deeper into the network.

 Output Layer: This is the final layer of the network, where the network's
prediction or output is generated. The number of neurons in the output layer
depends on the specific task the network is designed for.
Example  In a digit recognition task, there would be 10 neurons in the output
layer, each representing a digit from 0 to 9.

53
Characteristics of ANN
Neural networks can face some challenges when learning from data. There are a few
common problems along with their characteristics:-
1. Overfitting: Overfitting occurs when a neural network becomes too specialized
and performs exceptionally well on the training data but fails to generalize to
new, unseen data. It's like a student who memorizes answers for a specific set of
questions but struggles when faced with new ones. Overfitting can happen when
the network becomes too complex or when there is insufficient training data.

2. Underfitting: Underfitting happens when a neural network is too simple to


capture the underlying patterns in the data. It's like a student who fails to grasp
the concepts and performs poorly on both known and unknown questions.
Underfitting occurs when the network lacks complexity or when it is not trained
enough.

3. Bias-Variance Tradeoff: It refers to finding the right balance between a model's


simplicity (bias) and its ability to capture diverse patterns in the data (variance).
A network with high bias may oversimplify the data, while a network with high
variance may overcomplicate it. Striking the right balance is crucial for achieving
good generalization and avoiding underfitting or overfitting.

4. Data Insufficiency: Neural networks require a sufficient amount of diverse and


representative data to learn effectively. If the available data is limited, biased, or
unrepresentative, the network may struggle to generalize well. It's like trying to
learn about the whole world from just a few small pieces of information.

5. Curse of Dimensionality: It refers to the challenge of handling high-


dimensional data. As the number of input features increases, the amount of data
required to learn effectively grows exponentially. It becomes harder for the
network to identify meaningful patterns and relationships in the data.

Feed Forward Neural Network


Imagine a Feedforward Neural Network as a pipeline where you pass some
information in at one end, it flows through a series of processing stages, and the
final result comes out at the other end.

54
How it Works:-
1. Input Layer: The input layer is where we provide the initial data to the network.
Each neuron in the input layer represents a feature or an attribute of the input
data.
Example  If we're working with images, each neuron in the input layer could
represent a pixel's intensity.

2. Hidden Layers: Between the input layer and the output layer, there can be one
or more hidden layers. The hidden layers consist of neurons that process the
information from the input layer. Each neuron in the hidden layers takes input
from the previous layer, performs a computation using weighted connections,
and passes the result to the next layer.

3. Weights and Activation: The special thing about neural networks is that there
are weights associated with the connections between neurons. These weights
determine the importance of the information flowing through each connection.
Additionally, each neuron in the hidden layers applies an activation function to
the result of its computation. The activation function introduces non-linearities
and helps the network to learn complex patterns in the data.

4. Output Layer: After passing through one or more Hidden Layers, the data
reaches the Output Layer. This layer provides the final result of the network's
computation. Each neuron in the Output Layer corresponds to an element of the
desired output.
Example  If the network is trained for binary classification (yes/no), there
would be one neuron in the output layer.

Uses  Feedforward Neural Networks are used in a wide range of applications,


such as image and speech recognition, natural language processing, and even in
making predictions in financial markets.

Activation Function
An activation function in a neural network is a mathematical function that adds non-
linearity to the network's computations. It determines whether a neuron should be
activated (fire) or not based on the input it receives.

55
In simple words, think of an activation function as a gate that allows information to
flow through a neuron only if it meets a certain criteria. This gate introduces
flexibility and allows neural networks to model complex relationships in data.
Common types of activation functions:-
 Sigmoid Function:-

The sigmoid function squeezes the input between 0 and 1. It's like a "squashing"
function that maps large positive and negative numbers to values close to 0 or 1. It
was widely used in the past but is less popular now due to some limitations, like the
"vanishing gradient problem."

 ReLU (Rectified Linear Unit) Function:-

ReLU is a popular activation function that simply returns the input if it's positive
and sets it to zero if it's negative. This means ReLU allows the information to flow
if it's positive and blocks it if it's negative. ReLU is computationally efficient and
helps mitigate the vanishing gradient problem.

 Leaky ReLU Function:-

56
Leaky ReLU is similar to ReLU, but instead of setting negative values to zero, it
allows a small, non-zero slope for negative inputs. This prevents neurons from
becoming inactive and addresses some of the limitations of the regular ReLU.

 Tanh (Hyperbolic Tangent) Function:-

Tanh function maps the input between -1 and 1, making it similar to the sigmoid
function but centered around zero. It allows negative values, which helps with
centering the data and avoiding the vanishing gradient problem to some extent.

 Softmax Function:-

Softmax is commonly used in the output layer for multi-class classification


problems. It takes a set of values and converts them into a probability distribution,
ensuring that the sum of the probabilities is equal to 1. This makes it suitable for
tasks where an input can belong to one of several classes.

Backpropagation
Backpropagation is a technique used in neural networks to adjust the weights of the
connections between neurons during the training phase. It helps the network learn
from its mistakes and improve its predictions.

In simple words, think of backpropagation as a feedback mechanism that tells the


network how it should adjust its weights to get closer to the desired output.
57
 Forward Pass: During the forward pass, the input data is fed into the neural
network, and it propagates through the layers, generating an output. Each neuron
takes the weighted sum of its inputs, applies an activation function, and passes
the result to the next layer. This process continues until the output is generated.

 Backward Pass: In the backward pass, the network compares the generated
output with the desired output and calculates the error. The error is a measure of
how much the network's prediction deviates from the expected outcome. The
goal of backpropagation is to minimize this error.

Feed Forward Backpropagation


Feedforward backpropagation is a combination of feedforward neural networks and
backpropagation.
 Feedforward: In a feedforward neural network, information flows in one
direction, from the input layer to the output layer. It's like a data pipeline where
the input is processed layer by layer, and the output is generated.

 Backpropagation: Backpropagation is a technique used to train neural networks


by adjusting the weights of the connections between neurons. It involves
propagating the error backward from the output layer to the hidden layers,
adjusting the weights along the way to minimize the error.
Combine this Two Concepts:-
1. Forward Pass: During the forward pass, the input data is fed into the network,
and it propagates through the layers, generating an output. Each neuron takes the
weighted sum of its inputs, applies an activation function, and passes the result
to the next layer. This process continues until the output is generated.

2. Error Calculation: After the output is generated, we compare it with the desired
output to calculate the error. The error is a measure of how much the network's
prediction deviates from the expected outcome.

3. Backward Pass: In the backward pass, the network propagates the error
backward from the output layer to the hidden layers. It computes the gradients of
the error with respect to each weight in the network using the chain rule from
calculus.
58
4. Weight Update: The gradients obtained from the backward pass represent the
direction and magnitude of the weight adjustments needed to reduce the error.
The network updates the weights by moving in the opposite direction of the
gradients. This process of updating the weights is called gradient descent.

Feedforward backpropagation is a powerful technique that has been successful in


training deep neural networks for various tasks such as image classification, natural
language processing, and speech recognition.

Convolutional Neural Network


A Convolutional Neural Network (CNN) is a specialized type of artificial neural
network designed to process and analyze visual data, such as images and videos. It's
inspired by how the human brain's visual system works, making it particularly
effective for tasks like image recognition and object detection.
The key idea behind CNNs is to use a specific layer called a "convolutional layer,"
which helps extract meaningful patterns and features from the input images.
How a CNN works:-
1. Convolutional Layer: The first layer in a CNN is the convolutional layer. It
consists of small filters or "kernels," which are small windows that slide across
the input image. Each filter looks for specific features, like edges, textures, or
shapes.

2. Feature Extraction: As the filters slide over the input image, they perform a
mathematical operation called "convolution," which combines the values of the
image pixels within the window to create a "feature map." These feature maps
highlight the presence of different features in the input image.

3. Activation Function: After convolution, each feature map goes through an


activation function (often ReLU) to introduce non-linearity and to help the
network learn complex relationships between features.

4. Pooling Layer: After the convolutional layer, there is usually a pooling layer,
which reduces the spatial dimensions of the feature maps while retaining the most
important information. Pooling helps in reducing computation and making the
network more robust to variations in the input.

59
5. Fully Connected Layers: Following the convolutional and pooling layers, there
are one or more fully connected layers. These layers are similar to those in
traditional neural networks and help combine the features learned from the
previous layers to make predictions.

6. Output Layer: The final layer in the CNN is the output layer, which produces
the network's prediction or classification. The number of neurons in the output
layer depends on the specific task the CNN is designed for. For example, if it's a
digit recognition task, there would be 10 neurons, each representing a digit from
0 to 9.
CNNs have revolutionized computer vision tasks by achieving impressive results in
various applications, such as image classification, object detection, and image
segmentation.

ARCHITECTURE OF CNN

It consists of the three important layers:-


1. Convolutional Layer
2. Pooling Layer
3. Fully Connected Layer

 Convolutional Layer:-
It is the first and core layer of the CNN. It is one of the building blocks of a CNN
and is used for extracting important features from the image.We use a convolution
operation that will extract all the important features from the image.
Convolutional Operations
1. Kernel or Filter:-

60
Every input image is represented by a matrix of pixel values.
Apart from the input matrix, we also have another matrix called the filter matrix.
The filter matrix is also known as a kernel, or simply a filter.

We take the filter matrix, slide it over the input matrix by one pixel, perform
element-wise multiplication, sum up the results, and produce a single number.

2. Stride:-
It refers to the step size at which the convolutional kernel or filter is moved across
the input data. It determines how much the kernel is shifted at each step during the
convolution operation.
There are two types of stride:-
A. Small Stride: When the stride is set to a small number, we can encode a more
detailed representation of the image than when the stride is set to a large number.

B. Large Stride: A stride with a high value takes less time to compute than one
with a low value. It means we capture less data, so it also reduced the
dimensionality of the feature maps as compared to lower stride values.

3. Feature Map:-

A. Various filters are used for extracting different features from the image. If we use
a sharpen filter, then it will sharpen our image.

B. There are several kinds of filters in Image processing domain. Some of them are
used to detect edges, while others may blur the image.
61
C. Our goal is to learn the filter values from the input data and output labels.

D. We can use multiple filters for extracting different features from the image, and
produce multiple feature maps. So, the depth of the feature map will be the
number of filters.

E. If we use seven filters to extract different features from the image, then the depth
of our feature map will be seven.

4. Padding:-
We can simply pad the input matrix with zeros so that the filter can fit the input
matrix.
There are two types of padding:-
A. Zero Padding: Padding with zeros on the input matrix is called same padding or
zero padding.

B. Valid Padding: We can also simply discard the region of the input matrix where
the filter doesn't fit in. This is called valid padding.

 Pooling Layer:-
A pooling layer is a common component used in CNNs for image analysis and other
tasks. Its purpose is to reduce the spatial dimensions (width and height) of the input
data while retaining important features.
62
The pooling layer divides the input into small, non-overlapping regions called
pooling windows. Each pooling window collects information from a local
neighborhood of the input. It helps retain important features while discarding
irrelevant details, contributing to the overall effectiveness and efficiency of the
network.
Types of Pooling Operations:-
There are three major types of pooling operations:-
1. Max Pooling
2. Average Pooling
3. Sum Pooling

A. Max Pooling:-
In max pooling, we slide over the filter on the input matrix and simply take the
maximum value from the filter window.

B. Average Pooling & Sum Pooling:-


In average pooling, we take the average value of the input matrix within the filter
window.
In sum pooling, we sum all the values of the input matrix within the filter window.

63
 Fully Connected Layer:-
A fully connected layer, also known as a dense layer, is a fundamental component
in neural networks that connects every neuron from one layer to every neuron in the
subsequent layer. We flatten the feature map and convert it into a vector, and feed
it as an input to the feedforward network.
The feedforward network takes this flattened feature map as an input, applies an
activation function, such as sigmoid, and returns the output, this is called a fully
connected layer or dense layer.
The main goal of fully connected layers is to learn and model complex relationships
in the data, enabling the network to make accurate predictions or classifications
based on the learned features.

Neural Networks using Tensor Flow


A neural network is a powerful machine learning model inspired by the human
brain's structure. It is used for various tasks like image recognition, natural language
processing, and much more.
TensorFlow is a popular library that helps us build and train neural networks easily.
Think of TensorFlow as a toolkit that makes it simpler to create and manipulate
these networks.
How neural networks work using TensorFlow:-
1. Neurons: At the core of a neural network are neurons, which are tiny information
processors. They take input, perform some calculations, and produce an output.
These neurons are organized into layers.

2. Layers: A neural network has multiple layers, typically divided into three types:

a) Input Layer: This layer receives the initial data, like an image or text.

b) Hidden Layers: These layers process the data and learn patterns from it.

c) Output Layer: The final layer produces the network's prediction or result,
depending on the task.

64
3. Weights and Biases: To learn from data, each connection between neurons has
a weight. These weights determine the importance of one neuron's output on
another. Additionally, each neuron has a bias, which helps in adjusting the
output.

4. Activation Function: After the weighted sum of inputs and biases is calculated,
an activation function is applied to introduce non-linearity into the model. It helps
the network learn complex relationships in the data.

5. Training: During training, the network adjusts its weights and biases to
minimize the difference between predicted output and actual output. It does this
by comparing the predicted values to the correct answers from the training data.

6. Loss Function: The loss function measures how far off the predictions are from
the actual values. The goal is to minimize this difference during training.

7. Optimization Algorithm: To minimize the loss function, an optimization


algorithm is used (e.g., Gradient Descent). It adjusts the weights and biases
gradually to find the optimal values.

8. Epochs: The training process is divided into epochs. In each epoch, the network
goes through the entire dataset once, making adjustments to improve its
predictions.

9. Testing: Once the network is trained, it is tested on a separate set of data to


evaluate its performance on unseen examples.

10. Prediction: After successful training, the neural network can be used to make
predictions on new, unseen data.

Optimizers in Keara’s (Only Read for 1 marks Question)


Optimizers are the method to train your machine/deep learning model. Right
optimizers are necessary for your model as they improve training speed and
performance.
TensorFlow Keras mainly supports the following optimizer classes:-
 SGD: Gradient descent (with momentum) optimizer.
65
 Adam: Optimizer that implements the Adam algorithm.
 RMSprop: Optimizer that implements the RMSprop algorithm.
 Adadelta: Optimizer that implements the Adadelta algorithm.
 Adagrad: Optimizer that implements the Adagrad algorithm.
 Adamax: Optimizer that implements the Adamax algorithm.
 Nadam: Optimizer that implements the NAdam algorithm.
 Ftrl: Optimizer that implements the FTRL algorithm.

Gradient Descent Rules (Only Read for 1 marks Question)


1. Step Towards the Minimum: Gradient descent is like trying to find the lowest
point in a hilly landscape. Imagine you are blindfolded on a hilly terrain, and
your goal is to get to the lowest point. You can only sense the slope of the ground
you are standing on (the gradient).

2. Direction of Steepest Descent: You take small steps downhill in the direction
of the steepest slope. This way, you'll eventually reach the lowest point, which
corresponds to the minimum of the error.

3. Adjusting Parameters: In machine learning, the "parameters" are the weights


and biases of the model. During each step, the gradient descent algorithm updates
these parameters slightly based on the calculated gradient.

4. Learning Rate: The learning rate controls the size of the steps you take. If the
learning rate is too large, you might overshoot the minimum and miss it. If it's
too small, you might take too many tiny steps and take forever to reach the
minimum.

5. Convergence: With each step, the error reduces, and the model's parameters get
closer to the optimal values. The process continues until the parameters converge
to a point where further adjustments do not significantly improve the model's
performance.

6. Local Minima: It's essential to note that gradient descent might get stuck in a
local minimum, which is not the absolute lowest point but a low point nearby.

66
Various techniques, such as using different starting points or advanced
optimization algorithms, are employed to mitigate this issue.

7. Non-Convex Functions: In complex models, the error surface might have many
valleys and hills (non-convex). In such cases, gradient descent can still find good
solutions, but it might take more time and care.
Perceptron Learning Algorithm
The Perceptron Learning Algorithm is one of the simplest learning algorithms used
for training a type of neural network called a "perceptron.". It is the foundation of
neural networks and deep learning. The perceptron is a single-layer neural network
used for binary classification tasks, which means it can decide between two possible
outcomes.
How Works Perceptron Learning Algorithm:-
1. Perceptron: A perceptron is a basic building block of an artificial neural
network. It takes multiple inputs, multiplies each input by a corresponding
weight, and sums them up. The perceptron then applies an activation function to
the sum to produce an output.

2. Initialization: In the beginning, the weights of the perceptron are set to random
values or initialized to small numbers.

3. Learning from Data: The perceptron is trained using a labeled dataset. Each
data point in the dataset consists of input features and a known output (a label).

4. Activation Function: In the Perceptron Learning Algorithm, a simple activation


function is used, often the "step function." If the sum of weighted inputs is greater
than a threshold, the perceptron outputs a "1," otherwise "0."

5. Prediction: With the initial weights, the perceptron predicts the output for each
data point in the training set.

6. Comparison and Weight Update: For each data point, the predicted output is
compared to the actual label. If the prediction is correct, no changes are made to
the weights. However, if the prediction is wrong, the weights are adjusted to
correct the error.

67
7. Updating Weights: To update the weights, the Perceptron Learning Algorithm
takes into account the difference between the predicted output and the actual
label. The weights are then adjusted to reduce this difference.

8. Iterative Process: The training process continues for multiple iterations


(epochs) over the entire dataset. The weights are updated repeatedly after each
data point is presented to the perceptron.

9. Convergence: The algorithm keeps iterating until it reaches a point where the
perceptron makes no mistakes on the entire training dataset or a certain number
of epochs are reached.

10. Termination: The training process stops when the perceptron achieves
satisfactory accuracy on the training data or when it reaches a predefined number
of epochs.
AND or OR Boolean Function using Perceptron
 AND Function using Perceptron:-

1. We have two binary inputs, let's call them “input1” and “input2”.

2. Assign equal weights of `0.5` to both inputs (this means both inputs are equally
important in determining the output).

3. Set the threshold to `1.0`.

4. The activation function will calculate `sum = input1*0.5 + input2*0.5`.

5. If `sum >= 1.0`, the perceptron outputs `1`, representing "True" for the AND
operation; otherwise, it outputs `0`, representing "False."
Truth Table for AND Function:-
INPUT 1 INPUT 2 OUTPUT
0 0 0
0 1 0
1 0 0
1 1 1

68
 OR Function using Perceptron:-

1. Again, we have two binary inputs: `input1` and `input2`.

2. Assign equal weights of `0.5` to both inputs (similarly, both inputs are equally
important).

3. Set the threshold to `0.5`.

4. The activation function will calculate `sum = input1*0.5 + input2*0.5`.

5. If `sum >= 0.5`, the perceptron outputs `1`, representing "True" for the OR
operation; otherwise, it outputs `0`, representing "False."
Truth Table for OR Function:-
INPUT INPUT 2 OUTPUT
1
0 0 0
0 1 1
1 0 1
1 1 1

Both AND and OR functions can be effectively represented using a single-layer


perceptron with appropriate weights and threshold values.

Difference between Single Layer and Multi-Layer Perceptron


The main difference between a single-layer perceptron and a multi-layer perceptron
lies in their architecture and capabilities.
 Single-Layer Perceptron (SLP):-

1. A single-layer perceptron consists of only one layer of neurons (also called the
input layer) that directly connects to the output layer.

2. It can only learn and solve linearly separable problems. Linearly separable
problems are those that can be separated by a straight line or a hyperplane in
higher dimensions.

69
3. SLPs use a simple activation function, typically the step function, which can only
make binary decisions (output 0 or 1).

4. Since it has no hidden layers, it lacks the ability to capture complex patterns or
relationships in the data.

5. SLPs were introduced earlier in the history of neural networks and were limited
in their capabilities.

 Multi-Layer Perceptron (MLP):-

1. A multi-layer perceptron, as the name suggests, consists of multiple layers of


neurons, including one or more hidden layers between the input and output
layers.

2. MLPs can learn and solve both linearly separable and non-linearly separable
problems. Non-linearly separable problems are those that require more complex
decision boundaries to separate the data.

3. MLPs can use a variety of activation functions, such as the sigmoid, ReLU, or
tanh functions, allowing them to model non-linear relationships and capture more
intricate patterns in the data.

4. The presence of hidden layers in MLPs enables them to learn hierarchical


representations of the data, making them more powerful in handling complex
tasks like image recognition, natural language processing, and more.

5. MLPs are the basis for modern deep learning models and are widely used in a
wide range of applications due to their ability to handle complex and high-
dimensional data.
Natural Language Processing
Natural Language Processing (NLP) is a branch of artificial intelligence (AI) that
deals with the interaction between computers and human languages. It focuses on
enabling computers to understand, interpret, and generate human language in a way
that is meaningful and useful.

70
Differentiating Natural Language and Computer Language:-
 Natural Language: Natural language refers to the languages that humans use
for communication, such as English, Spanish, Chinese, etc. It is characterized by
its complexity, ambiguity, and nuances, making it challenging for computers to
process and understand without specialized techniques like NLP.

 Computer Language: Computer language, on the other hand, refers to the


programming languages used to communicate with computers and give them
instructions to perform specific tasks. Examples include Python, Java, C++, etc.
These languages are designed to be unambiguous and precise, allowing
computers to execute instructions accurately.
Advantages of NLP:-
1. Language Understanding: NLP enables computers to understand and process
human language, making it possible to build applications that can interact with
users through natural conversation.
1

2. Information Extraction: NLP allows computers to extract valuable information


from large volumes of text, such as sentiment analysis, named entity recognition,
and topic modeling.

3. Language Translation: NLP is used in machine translation systems like Google


Translate, helping break language barriers and facilitate communication across
different languages.

4. Voice Assistants: NLP powers voice assistants like Siri and Alexa, enabling
users to interact with their devices through spoken language.

71
5. Sentiment Analysis: NLP can determine the sentiment or emotions expressed in
text, making it useful for understanding customer feedback, social media
monitoring, etc.

Disadvantages of NLP:-
1. Ambiguity: Natural languages often have ambiguous meanings, which can be
challenging for computers to interpret correctly, leading to potential
misunderstandings.

2. Language Complexity: Human languages are complex, with variations in


grammar, expressions, and regional dialects, making it difficult to create
universal NLP systems.

3. Data Limitations: NLP models require extensive and diverse training data to
perform well, which might not always be available for certain languages or
domains.

4. Processing Time: NLP tasks can be computationally expensive, especially for


large datasets, leading to increased processing time and resource requirements.

5. Privacy and Ethical Concerns: NLP systems may encounter issues related to
data privacy, bias, and ethical considerations, especially when dealing with
sensitive information or user-generated content.

Text Processing
 Text processing is a broader term that encompasses various techniques used to
handle and manipulate text data in NLP.

 It involves tasks like tokenization, lowercasing, stopword removal,


normalization, and handling special characters.

 The goal of text processing is to clean and prepare the raw text data for further
analysis or modelling.

 Text processing aims to convert unstructured text into a structured format that
can be easily understood and analyzed by computers.

72
1. Tokenization: The first step in text processing is tokenization. It involves
breaking down the text into smaller units called "tokens." Tokens can be words,
sentences, or even characters, depending on the level of granularity needed for
analysis.
1

2. Lowercasing: To ensure consistency, all text is often converted to lowercase.


This avoids treating the same word with different cases (e.g., "apple" and
"Apple") as different tokens.

3. Stopword Removal: Stopwords are common words (e.g., "the," "is," "and") that
don't carry much meaning and are often removed to reduce noise in the text and
focus on more important words.

4. Normalization: Normalization involves removing special characters,


punctuation, and other non-textual elements to clean the text and standardize it
for analysis.

Lexical Processing
 It focuses on understanding and analyzing individual words (lexical units) in a
text.

 It involves tasks like part-of-speech tagging (POS), stemming, lemmatization,


named entity recognition (NER), word embeddings, and word sense
disambiguation.

 The goal of lexical processing is to gain insights into the vocabulary, word
meanings, and grammatical relationships between words in a text.

 Lexical processing helps in understanding the syntactic and semantic aspects of


words and their role in the context of the overall text.

1. Stemming and Lemmatization: Stemming and lemmatization are techniques to


reduce words to their base or root forms. For example, "running," "runs," and
"ran" may all be stemmed to "run," or lemmatized to their base form "run."

73
2. Bag of Words (BoW): BoW is a popular representation for text processing. It
creates a "bag" of all unique words in the text, ignoring grammar and word order,
and counts the frequency of each word.

3. Named Entity Recognition (NER): NER is a task in NLP that identifies and
classifies named entities such as names of people, organizations, locations, and
dates in the text.

4. Sentiment Analysis: Sentiment analysis is a task that determines the emotional


tone of the text, whether it is positive, negative, or neutral.

5. Part-of-Speech Tagging (POS): POS tagging assigns grammatical labels (e.g.,


noun, verb, adjective) to each word in a sentence, helping to understand the
syntactic structure of the text.

NLP tasks in syntax, semantics, and pragmatics (only read)


 Syntax:-
The study of the structure and arrangement of words in a sentence to understand the
grammatical relationships between them. It plays a significant role in various NLP
tasks as it provides valuable insights into how words are combined to form
meaningful sentences.
1. Part-of-Speech Tagging (POS): Identifying the grammatical category (noun,
verb, adjective, etc.) of each word in a sentence.

2. Parsing: Analyzing the sentence's grammatical structure to create a parse tree


that shows the relationship between words.

3. Constituency Parsing: Identifying and labeling the different constituents


(phrases) in a sentence, like noun phrases and verb phrases.

4. Dependency Parsing: Analyzing the relationships between words in a sentence


through directed edges to represent grammatical dependencies.

5. Sentence Structure Analysis: Understanding how words and phrases in a


sentence are structured to identify the subject, predicate, and objects.

 Semantics:-
74
The study of meaning in language and how words and sentences convey
information. It is essential for building NLP systems that can comprehend the
context and intent behind human language. Several NLP tasks involve semantic
analysis to extract and interpret meaning from text.
1. Named Entity Recognition (NER): Identifying and classifying named entities,
like names of people, organizations, locations, and dates, in a sentence.

2. Word Sense Disambiguation: Determining the correct meaning of words with


multiple possible interpretations based on the context in which they appear.

3. Semantic Role Labeling: Assigning specific roles (e.g., agent, patient,


instrument) to words in a sentence to understand their relationships.

4. Sentiment Analysis: Analyzing the emotional tone or sentiment expressed in a


text (e.g., positive, negative, neutral).

 Pragmatics:
It deals with the study of language use in context. It focuses on understanding the
meaning of sentences beyond their literal interpretations and takes into account the
context, speaker's intentions, and the implied meaning of the communication. It
plays a vital role in several NLP tasks that require a deeper understanding of
language use and context.
1. Anaphora Resolution: Identifying and resolving references (anaphora) in a
sentence that refer back to previously mentioned words or phrases.

2. Speech Act Recognition: Understanding the intended meaning or action behind


a sentence, such as making a request, asking a question, or giving a command.

3. Discourse Analysis: Analyzing the flow and coherence of a conversation or text


to understand how sentences relate to each other in a broader context.

4. Intent Recognition: Determining the intended purpose or goal behind a user's


input, often used in chatbots or virtual assistants.

75
Segmentation
Sentence segmentation involves splitting a text into individual sentences. One
common way to achieve this is by using the sent_tokenize function from the NLTK
library (Natural Language Toolkit).

Program:-
# Make sure to download the punkt tokenizer data
import nltk
nltk.download('punkt')

# Import sent_tokenize from nltk library


from nltk.tokenize import sent_tokenize

# Sample Sentence
text = "This is a sample text. It contains multiple sentences. Let's split them."

# Convert into words


sentences = sent_tokenize(text)
print(sentences)

Tokenization
It is the process of tokenizing or splitting a string, text into a list of tokens. One can
think of token as parts like a word is a token in a sentence, and a sentence is a token
in a paragraph.
 Text into sentences tokenization
 Sentences into words tokenization
 Sentences using regular expressions tokenization

76
Program:-
# Import the NLTK library
import nltk
from nltk.tokenize import word_tokenize

# Sample text to be tokenized


text = "Tokenization is an important step in NLP."

# Tokenize the text into words


tokens = word_tokenize(text)

# Print the tokens


print(tokens)
Removal Stop Words
The process of converting data to something a computer can understand is referred
to as pre-processing. One of the major forms of pre-processing is to filter out
useless data. In NLP, useless words (data), are referred to as stop words. A stop
word is a commonly used word (such as “the”, “a”, “an”, “in”)

Program:-
import nltk
nltk.download('punkt')
nltk.download('stopwords')

# import stopwords and word_tokenize from nltk library


from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
77
# User defined Function
def remove_stopwords(text):
stop_words = set(stopwords.words('english'))
words = word_tokenize(text)
filtered_words = [word for word in words if word.lower() not in stop_words]
return " ".join(filtered_words)

# Sample text
text = "This is an example sentence that contains some stop words."

# Remove stopwords
processed_text = remove_stopwords(text)
print(processed_text)

Stemming
Stemming is a text pre-processing technique used in NLP to reduce words to their
base or root form, called the "stem." It involves removing suffixes from words to
obtain the core meaning of the word.
Example  stemming would convert words like "running," "runs," and "ran" to the
common stem "run."

It is commonly used in information retrieval, search engines, and various text


analysis tasks.
Program:-
import nltk
from nltk.stem import PorterStemmer
78
from nltk.tokenize import word_tokenize

nltk.download('punkt')

def perform_stemming(text):
stemmer = PorterStemmer()
words = word_tokenize(text)
stemmed_words = [stemmer.stem(word) for word in words]
return " ".join(stemmed_words)

# Sample text
text = "I am running and eating, while others are playing and laughing."

# Perform stemming
stemmed_text = perform_stemming(text)
print(stemmed_text)

Lemmatization
Lemmatization is a text normalization technique in NLP that reduces words to their
base or root form (known as the lemma) while preserving the context and meaning
of the word. It helps in grouping different forms of a word together, such as plurals
and verb conjugations, making it easier for algorithms to process the text and extract
meaningful information.
Example  lemmatization would convert "running" and "ran" to their base form
"run," "cats" to "cat," and "better" to "good."

Program:-
import nltk
from nltk.stem import WordNetLemmatizer
79
from nltk.tokenize import word_tokenize

nltk.download('punkt')
nltk.download('wordnet')

def lemmatize_text(text):
lemmatizer = WordNetLemmatizer()
words = word_tokenize(text)
lemmatized_words = [lemmatizer.lemmatize(word) for word in words]
return " ".join(lemmatized_words)

# Sample text
text = "The cats were running and playing in the garden."

# Lemmatize the text


lemmatized_text = lemmatize_text(text)
print(lemmatized_text)

Part-of-speech Tagging
It involves assigning a specific grammatical category (such as noun, verb, adjective,
etc.) to each word in a given sentence. This information helps in understanding the
syntactic structure and grammatical relationships within the text.

Program:-
import nltk
from nltk.tokenize import word_tokenize
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')
80
def pos_tagging(text):
words = word_tokenize(text)
pos_tags = nltk.pos_tag(words)
return pos_tags

# Sample text
text = "The quick brown fox jumps over the lazy dog."

# Perform POS tagging


pos_tags_result = pos_tagging(text)
print(pos_tags_result)

The “pos_tagging” function tokenizes the input text into individual words using the
“word_tokenize” function from NLTK. Then, the “pos_tag” function is used to
assign POS tags to each word.

OUTPUT:-
[('The', 'DT'), ('quick', 'JJ'), ('brown', 'NN'), ('fox', 'NN'), ('jumps', 'VBZ'), ('over',
'IN'), ('the', 'DT'), ('lazy', 'JJ'), ('dog', 'NN'), ('.', '.')]
'DT' (Determiner) 'JJ' (Adjective)
'VBZ' (Verb) 'IN' (Preposition)
'NN' (Noun) '.' (Punctuation)

Sentiment Analysis
Sentiment Analysis, also known as Opinion Mining, is a text analysis technique that
aims to determine the sentiment or emotional tone expressed in a piece of text. The
goal is to identify whether the sentiment is positive, negative, neutral, or sometimes
more fine-grained emotions like happy, sad, angry, etc.
Applications:-
1. Social Media Monitoring: Analyzing social media posts, tweets, or comments
to understand public opinion about products, services, or events.

81
2. Customer Feedback Analysis: Identifying customer sentiment in reviews,
surveys, or feedback to gauge customer satisfaction.

3. Brand Monitoring: Monitoring brand mentions and sentiment to understand


brand perception in the market.
Example:-
Text: "The new smartphone is fantastic! I love all its features."
Sentiment: Positive

Text: "I am disappointed with the service. It was very slow and unresponsive."
Sentiment: Negative

Text Classification
Text Classification, also known as Document Classification or Text Categorization,
is a broader task where text documents are categorized into predefined classes or
categories based on their content or topic.
Applications:-
1. Spam Detection: Classifying emails as spam or not spam.

2. Topic Categorization: Categorizing news articles into topics like sports,


politics, entertainment, etc.

3. Intent Recognition: Identifying the intent of user queries for chatbots or virtual
assistants.
Example:-
For an e-commerce website, categorizing product descriptions into different classes
like "Electronics," "Clothing," "Books," etc.

Text: "This smartphone has a powerful processor and high-quality camera."


Category: Electronics

Text: "The latest fashion trends for summer are now available."
Category: Clothing
82
Difference between Training Set and Test Set
 Training Set:-

1. The training set is a subset of the data used to train a machine learning model.

2. It contains labeled data, where the input features (independent variables) and the
corresponding target labels (dependent variable) are known

3. The model learns from this data during the training phase, adjusting its internal
parameters to fit the patterns in the data.

 Test Set:-

1. The test set is another subset of the data that the trained machine learning model
has not seen during training.

2. It also contains labeled data with input features and corresponding target labels.

3. The test set is used to evaluate the model's performance and its ability to
generalize to new, unseen data.

4. By using a test set, we can estimate how well the model will perform on real-
world data and assess its accuracy, precision, recall, and other performance
metrics.
Regarding splitting on the dependent variable:-
When we split the data into a training set and a test set, we want to ensure that both
sets represent the same distribution of data. By randomly sampling data for the
training and test sets, we ensure that both sets have similar characteristics, and the
model learns and evaluates on a representative sample.

However, we do not split the data based on the dependent variable (target label)
because that would introduce a bias. The dependent variable is the variable we are
trying to predict or classify, and it is used to measure the model's performance
during testing.

83
If we split the data based on the dependent variable, we might end up with a test set
that has significantly different characteristics from the training set. This would not
accurately reflect the model's performance on new, unseen data, as the test set would
be biased towards the specific values of the dependent variable in the training set.

84

You might also like