0% found this document useful (0 votes)
51 views48 pages

MADA

The document discusses different machine learning concepts including supervised and unsupervised learning. It explains key algorithms like linear regression, support vector regression, K-means clustering and dimensionality reduction. Examples of each concept are also provided.

Uploaded by

Vivek Kumar
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)
51 views48 pages

MADA

The document discusses different machine learning concepts including supervised and unsupervised learning. It explains key algorithms like linear regression, support vector regression, K-means clustering and dimensionality reduction. Examples of each concept are also provided.

Uploaded by

Vivek Kumar
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/ 48

Experiment No 2

AIM:
Basic syntax of python in term of variables, conditional statement, loops, operator
and function.
Conditional statement

There are situations in real life when we need to do some specific task and
based on some specific conditions, we decide what we should do next.

Similarly, there comes a situation in programming where a specific task is to


be performed if a specific condition is True.
The following are the conditional statements.
1. if
2. if..else
3. nested if statement
4. neif-elif statement
if Statement

If the simple code of block is to be performed if the condition holds true then
the if statement is used.
if else Statement in Python

In conditional if Statement the additional block of code is merged as else


statement which is performed when if condition is false.
Nested if Statement

if statement can also be checked inside other if statement. This conditional


statement is called a nested if statement. This means that inner if condition
will be checked only if outer if condition is true and by this, we can see
multiple conditions to be satisfied.
Neif-elif Statement

The if-elif statement is shortcut of if..else chain. While using if-elif statement
at the end else block is added which is performed if none of the above if-elif
statement is true.
Loops

In Python, loops are used to repeatedly execute a block of code. There are
two main types of loops in Python: for loop and while loop.
1. for Loop:

The for loop is typically used for iterating over a sequence (such as a list,
tuple, string, or range) or other iterable objects.

Example 1: Iterating over a list


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

for fruit in fruits:


print(fruit)

Example 2: Using range to iterate over numbers


for i in range(5):
print(i)
2. while Loop:

The while loop is used to repeatedly execute a block of code as long as the
specified condition is true.

Example: Using a while loop to print numbers 0 to 4


count = 0

while count < 5:


print(‘count’)
count += 1
Function

A function is a block of reusable code that performs a specific task. Functions


help in organizing code, making it more modular, and promoting code reuse.

Example-
def function_name(parameter1, parameter2, ...):
# Code to perform the task
# ...
# Optionally, return a value
return result
Function

Example of a simple function that adds two numbers:


def add_numbers(a, b):
result = a + b
return result

# Calling the function


sum_result = add_numbers(3, 5)
print(sum_result) # Output: 8
Experiment – No. 3
AIM:
Introduction of basic library for data analytics (numpy, pandas, sklearn, matplotlib)
NumPy:
➢ NumPy is a fundamental package for scientific computing with Python.

➢ It provides support for large, multi-dimensional arrays and matrices,


along with a collection of mathematical functions to operate on these
elements.
Example
import numpy as np
# Creating a NumPy array
arr = np.array([1, 2, 3, 4, 5])
# Performing operations on the array
squared_arr = np.square(arr)
Pandas:
➢ Pandas is a powerful data manipulation and analysis library.

➢ It provides data structures like Series and DataFrame, which are designed
to handle and analyze structured data easily.
Example
import pandas as pd
# Creating a DataFrame
data = {'Name': ['John', 'Jane', 'Bob'],
'Age': [25, 30, 22]}
df = pd.DataFrame(data)
# Performing operations on the DataFrame
mean_age = df['Age'].mean()
Scikit-learn (sklearn):
➢ Scikit-learn is a machine-learning library that provides simple and efficient
tools for data analysis and modelling.

➢ It includes various algorithms for classification, regression, clustering, and


more.
Example
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
# Splitting data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X,
y, test_size=0.2)
# Creating and training a linear regression model
model = LinearRegression()
model.fit(X_train, y_train)
# Making predictions and evaluating the model
predictions = model.predict(X_test)
mse = mean_squared_error(y_test, predictions)
Matplotlib:
➢ Matplotlib is a 2D plotting library for creating static, animated, and interactive
visualizations in Python.

➢ It provides a wide variety of charts and plots to visualize data.


Example
import matplotlib.pyplot as plt
# Creating a simple line plot
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
plt.plot(x, y)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Sine Function')
plt.show()
Arrays are a fundamental data structure, and an important part of most
programming languages. In Python, they are containers which are able to
store more than one item at the same time.

Specifically, they are an ordered collection of elements with every value


being of the same data type.
Experiment – No. 8
AIM:
Perform supervised modelling on the predefined dataset using linear
regression and support vector regression.
ML: Definition
Machine learning is a subset of AI, which enables the machine to
automatically learn from data, improve performance from past
experiences, and make predictions.

These ML algorithms help to solve different business problems like


Regression, Classification, Forecasting, Clustering, and
Associations, etc.
Machine learning is divided into four main types, which are:
➢ Supervised Machine Learning
➢ Unsupervised Machine Learning
➢ Semi-Supervised Machine Learning
➢ Reinforcement Learning
Types of machine learning algorithms
Supervised Learning:
In supervised learning, the algorithm learns from labeled data, where each example is tagged with
the correct answer. The algorithm tries to learn the mapping between the input variables and the
target variable, making predictions on new, unseen data.

Supervised Learning Models:

Regression Models: Used for predicting continuous outcomes. Examples include linear regression,
polynomial regression, and support vector regression.

Classification Models: Used for predicting categorical outcomes. Examples include logistic
regression, decision trees, random forests, and support vector machines.
Unsupervised Learning:
Unsupervised learning deals with unlabeled data. The algorithm tries to find patterns or structures
in the data without explicit guidance.

Unsupervised Learning Models:

Clustering Models: Group similar data points together based on their features. Examples include
K-means clustering and hierarchical clustering.

Dimensionality Reduction Models: Reduce the number of features in the dataset while preserving
important information. Examples include principal component analysis (PCA).
Semi-supervised Learning:
Semi-supervised learning combines elements of both supervised and unsupervised learning. It uses
a small amount of labeled data along with a large amount of unlabeled data to improve learning
accuracy.

Semi-supervised Learning Models: These models utilize both labeled and unlabeled data for
training. Algorithms like self-training and co-training are examples of semi-supervised learning
approaches.
Reinforcement Learning:

Reinforcement learning involves training agents to make decisions within an environment to


achieve certain goals. The agent learns to take actions that maximize cumulative rewards over time
through trial and error.

Reinforcement Learning Models: Learn to make sequences of decisions in an environment to


maximize cumulative rewards. Reinforcement learning algorithms include Q-learning, Deep Q-
Networks (DQN), and policy gradient methods.
Deep Learning Models:

Convolutional Neural Networks (CNNs): Designed for processing structured grid data like images or
sequences. CNNs are commonly used in image recognition and computer vision tasks.

Recurrent Neural Networks (RNNs): Specialized for sequence data, RNNs have feedback connections
allowing them to process sequences of inputs. Long Short-Term Memory Networks (LSTMs) and Gated
Recurrent Units (GRUs) are variants of RNNs.

Generative Models: Generate new data samples that resemble the training data. Examples include
Variational Autoencoders (VAEs) and Generative Adversarial Networks (GANs).
Ensemble Learning Models:

Random Forests: Consist of multiple decision trees trained on different subsets of the data. The final
prediction is the aggregation of predictions from individual trees.

Gradient Boosting Machines (GBMs): Build a sequence of decision trees where each subsequent tree
corrects the errors made by the previous ones.
Supervised Machine Learning Regression models:

1. Simple Linear Regression

2. Support Vector for Regression (SVR)


Types of Linear Regression
There are two main types of linear regression:
1. Simple Linear Regression
2. Multiple Linear Regression

Simple Linear Regression


This is the simplest form of linear regression, and it involves only one independent variable and one
dependent variable. The equation for simple linear regression is:

where:
• 𝑦 is the dependent variable
•X is the independent variable
•β0 is the intercept
•β1 is the slope
Support Vector for Regression (SVR)

SVR was initially proposed by Drucker, which is a supervised learning technique, based on
the concept of Vapnik's support vectors. SVR aims at reducing the error by determining the
hyperplane and minimising the range between the predicted and the observed values.
Consider these two red lines as the decision boundary and the green line as the hyperplane. Our
objective, when we are moving on with SVR, is to basically consider the points that are
within the decision boundary line. Our best-fit line is the hyperplane that has a maximum
number of points.
Experiment – No. 8
AIM:
Dimensionality reduction (using PCA) and modelling (K-means) of
dataset.
The main feature of unsupervised learning algorithms, when compared to classification and
regression methods, is that input data are unlabeled (i.e. no labels or classes given) and that the
algorithm learns the structure of the data without any assistance.

This creates two main differences. First, it allows us to process large amounts of data because the
data does not need to be manually labeled.

Second, it is difficult to evaluate the quality of an unsupervised algorithm due to the absence of
an explicit goodness metric as used in supervised learning.
One of the most common tasks in unsupervised learning is dimensionality reduction.

On one hand, dimensionality reduction may help with data visualization (e.g. t-SNA method)

On the other hand, it may help deal with the multicollinearity of your data and prepare the data
for a supervised learning method (e.g. decision trees).
There are two types of Dimensionality Reduction techniques:

1.Feature Selection
2.Feature Extraction

Feature Selection Techniques:


1. Backward Elimination
2. Forward Selection
3. Bi-directional Elimination
4. Score Comparison

Feature Extraction Techniques:


1. Principal Component Analysis (PCA)
2. Linear Discriminant Analysis (LDA)
3. Kernel PCA
Principal Component analysis-PCA
Principal Component analysis-PCA

Used for

➢ Noise filtering

➢ Visualization

➢ Feature Extraction

➢ Stock market predictions

➢ Gene data analysis


Principal Component analysis-PCA

➢ Identify patterns in data

➢ Detect the correlation between variables


Principal Component analysis-PCA
Reduce the dimensions of a d-dimensional dataset by projecting it onto a (k)-

dimensional subspace (Where K<d)


Principal Component analysis-PCA
Main functions of PCA

▪ Standardize the data

▪ Obtain the Eigenvectors and Eigenvalues from the covariance matrix or correlation matrix, or

perform Singular Vector decomposition.

▪ Sort eigenvalues in descending order and choose the k eigenvectors that correspond to the k

largest eigenvalues where k is the number of dimensions of the new feature subspace.

▪ Construct the projection matrix W from the selected k eigenvectors.

▪ Transform the original dataset X via W to obtain a k-dimensional feature subspace Y


Principal Component analysis-PCA
➢ Learn about the relationship between X and Y values

➢ Find list of principal axes

You might also like