0% found this document useful (0 votes)
4 views31 pages

ML Lab File

The document outlines a series of machine learning experiments, including data pre-processing, simple and multiple linear regression, decision trees, random forest classification, Naïve Bayes, K-Nearest Neighbours, and K-Means clustering. Each experiment includes an aim, theoretical background, algorithm steps, Python program examples, inputs, outputs, results, and applications. The overall focus is on implementing various machine learning algorithms and their practical applications in data analysis.

Uploaded by

princesingh02718
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)
4 views31 pages

ML Lab File

The document outlines a series of machine learning experiments, including data pre-processing, simple and multiple linear regression, decision trees, random forest classification, Naïve Bayes, K-Nearest Neighbours, and K-Means clustering. Each experiment includes an aim, theoretical background, algorithm steps, Python program examples, inputs, outputs, results, and applications. The overall focus is on implementing various machine learning algorithms and their practical applications in data analysis.

Uploaded by

princesingh02718
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

INDEX

[Link] Experiment [Link]


Implement data pre-processing
1

Deploy Simple Linear Regression


2

Simulate Multiple Linear Regression


3

Implement Decision Tree


4

Deploy Random forest classification


5

Simulate Naïve Bayes algorithm


6

Implement K-Nearest Neighbours (K-NN), k-Means


7

Deploy Support Vector Machine, Apriori algorithm


8

Simulate Artificial Neural Network


9

Implement the Genetic Algorithm code


10
EXPERIMENT NO: 1

Aim:
Implement Data Pre-processing in Machine Learning

Theory:
Data Pre-processing is the process of cleaning and transforming raw data into a suitable format for
machine learning models.

Steps in Data Pre-processing:


1. Handling Missing Values
2. Encoding Categorical Data
3. Feature Scaling
4. Splitting Dataset

Why Pre-processing is important:


• Improves model accuracy
• Removes noise and inconsistencies
• Makes data suitable for algorithms

Algorithm:
1. Import required libraries
2. Load dataset
3. Handle missing values
4. Encode categorical variables
5. Perform feature scaling
6. Split data into training and testing sets

Program (Python)
import numpy as np
import pandas as pd
from [Link] import SimpleImputer
from [Link] import LabelEncoder, StandardScaler
from sklearn.model_selection import train_test_split
# Sample dataset
data = {
'Age': [25, 30, [Link], 35, 40],
'Salary': [50000, 60000, 65000, [Link], 70000],
'Country': ['India', 'USA', 'India', 'USA', 'India']
}

df = [Link](data)

print("Original Data:\n", df)

# Handling missing values


imputer = SimpleImputer(strategy='mean')
df[['Age', 'Salary']] = imputer.fit_transform(df[['Age', 'Salary']])

# Encoding categorical data


encoder = LabelEncoder()
df['Country'] = encoder.fit_transform(df['Country'])

# Splitting data
X = df[['Age', 'Salary']]
y = df['Country']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

# Feature scaling
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = [Link](X_test)

print("\nProcessed Data:\n", df)

Input:
Raw dataset with missing and categorical values
Output:
Cleaned and processed dataset ready for ML model

Result:
Data pre-processing techniques were successfully applied, and the dataset was prepared for
machine learning.

Applications:
• Data cleaning in real-world datasets
• Improving ML model performance
• Data analysis pipelines
EXPERIMENT NO: 2

Aim:
Deploy Simple Linear Regression

Theory:
Simple Linear Regression is a supervised learning algorithm used to model the relationship between
one independent variable (X) and one dependent variable (Y).
It follows the equation:
𝑦 = 𝑚𝑥 + 𝑐

Where:
• m = slope (coefficient)
• c = intercept
The model finds the best-fit line that minimizes prediction error.

Algorithm:
1. Import required libraries
2. Load dataset
3. Split dataset into training and testing sets
4. Train the Linear Regression model
5. Predict results
6. Visualize output

Program (Python)
import numpy as np
import pandas as pd
import [Link] as plt
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split

# Sample dataset
data = {
'Experience': [1, 2, 3, 4, 5],
'Salary': [30000, 35000, 40000, 45000, 50000]
}

df = [Link](data)

# Splitting data
X = df[['Experience']]
y = df['Salary']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

# Model training
model = LinearRegression()
[Link](X_train, y_train)

# Prediction
y_pred = [Link](X_test)

print("Predicted Salary:", y_pred)

# Visualization
[Link](X, y, color='blue')
[Link](X, [Link](X), color='red')
[Link]("Experience")
[Link]("Salary")
[Link]("Simple Linear Regression")
[Link]()

Input:
Dataset with independent variable (Experience) and dependent variable (Salary)

Output:
Predicted salary values and regression line

Result:
The Simple Linear Regression model was successfully trained and deployed to predict output values.

Applications:
• Salary prediction
• Sales forecasting
• Price estimation
EXPERIMENT NO: 3

Aim:
Simulate Multiple Linear Regression

Theory:
Multiple Linear Regression is a supervised learning algorithm used when the output depends on
more than one input feature.
It follows the equation:
𝑦 = 𝑏0 + 𝑏1 𝑥1 + 𝑏2 𝑥2 + ⋯ + 𝑏𝑛 𝑥𝑛

Where:
• 𝑥1 , 𝑥2 , . ..= independent variables
• 𝑏0 = intercept
• 𝑏1 , 𝑏2 = coefficients
The model finds the best-fit hyperplane in multi-dimensional space.

Algorithm:
1. Import required libraries
2. Load dataset with multiple features
3. Split data into training and testing sets
4. Train Multiple Linear Regression model
5. Predict output
6. Evaluate results

Program (Python)
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split

# Sample dataset
data = {
'Experience': [1, 2, 3, 4, 5],
'Education': [10, 12, 12, 16, 18],
'Salary': [30000, 35000, 40000, 45000, 50000]
}

df = [Link](data)

# Independent and dependent variables


X = df[['Experience', 'Education']]
y = df['Salary']

# Split dataset
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

# Train model
model = LinearRegression()
[Link](X_train, y_train)

# Prediction
y_pred = [Link](X_test)

print("Predicted Salary:", y_pred)

# Coefficients
print("Intercept:", model.intercept_)
print("Coefficients:", model.coef_)

Input:
Dataset with multiple input features (Experience, Education)

Output:
Predicted salary values and model coefficients

Result:
The Multiple Linear Regression model was successfully implemented and used to predict output
based on multiple inputs.

Applications:
• House price prediction
• Business forecasting
• Risk analysis
EXPERIMENT NO: 4

Aim:
Implement Decision Tree Algorithm

Theory:
A Decision Tree is a supervised learning algorithm used for classification and regression tasks.
It works like a tree structure:
• Root Node → Starting point
• Decision Nodes → Conditions/tests
• Leaf Nodes → Final output (class label)

How it works:
The algorithm splits data based on features using measures like:
• Information Gain (Entropy)
• Gini Index

Entropy Formula:
𝐸𝑛𝑡𝑟𝑜𝑝𝑦 = −∑𝑝𝑖 log⁡2 𝑝𝑖

Lower entropy → better split

Algorithm:
1. Select dataset
2. Choose best feature using entropy or gini index
3. Split dataset based on feature
4. Repeat recursively
5. Stop when all data is classified

Program (Python)
import pandas as pd
from [Link] import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn import tree

# Sample dataset
data = {
'Age': [25, 30, 45, 35, 22],
'Income': [40000, 50000, 80000, 60000, 30000],
'Student': [0, 1, 0, 1, 0],
'Buy': [0, 1, 1, 1, 0]
}

df = [Link](data)

# Features and target


X = df[['Age', 'Income', 'Student']]
y = df['Buy']

# Split data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

# Model
model = DecisionTreeClassifier()
[Link](X_train, y_train)

# Prediction
y_pred = [Link](X_test)

print("Predicted Output:", y_pred)

# Visualize tree
tree.plot_tree(model, feature_names=[Link], class_names=['No','Yes'], filled=True)

Input:
Dataset with features (Age, Income, Student)

Output:
Predicted class labels

Result:
The Decision Tree model was successfully implemented and used for classification.

Applications:
• Customer decision analysis
• Medical diagnosis
• Credit risk prediction
EXPERIMENT NO: 5

Aim:
Deploy Random Forest Classification

Theory:
Random Forest is an ensemble learning algorithm that combines multiple Decision Trees to
improve accuracy and reduce overfitting.

Key Idea:
• Build multiple decision trees
• Each tree is trained on a random subset of data
• Final output is based on majority voting

Advantages:
• High accuracy
• Reduces overfitting
• Works well with large datasets

Algorithm:
1. Import dataset
2. Split data into training and testing sets
3. Create Random Forest model
4. Train model using training data
5. Predict output using test data
6. Evaluate performance

Program (Python)
import pandas as pd
from [Link] import RandomForestClassifier
from sklearn.model_selection import train_test_split

# Sample dataset
data = {
'Age': [25, 30, 45, 35, 22],
'Income': [40000, 50000, 80000, 60000, 30000],
'Student': [0, 1, 0, 1, 0],
'Buy': [0, 1, 1, 1, 0]
}

df = [Link](data)

# Features and target


X = df[['Age', 'Income', 'Student']]
y = df['Buy']

# Split dataset
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

# Model
model = RandomForestClassifier(n_estimators=10)
[Link](X_train, y_train)

# Prediction
y_pred = [Link](X_test)

print("Predicted Output:", y_pred)

Input:
Dataset with features (Age, Income, Student)

Output:
Predicted class labels

Result:
The Random Forest Classifier was successfully implemented and used for classification.

Applications:
• Fraud detection
• Medical diagnosis
• Customer segmentation
• Recommendation systems
EXPERIMENT NO: 6

Aim:
Simulate Naïve Bayes Algorithm

Theory:
Naïve Bayes is a supervised learning algorithm based on Bayes’ Theorem, used for classification
problems.
It is called “naïve” because it assumes that all features are independent of each other.

Bayes’ Theorem:
𝑃(𝑋 ∣ 𝐶)𝑃(𝐶)
𝑃(𝐶 ∣ 𝑋) =
𝑃(𝑋)
𝑃(𝐴)
𝑃(𝐵 ∣ 𝐴)
𝑃(𝐵 ∣ ¬𝐴)
𝑃(𝐵 ∣ 𝐴)𝑃(𝐴)
𝑃(𝐴 ∣ 𝐵) = ≈ 0.68, 𝑃(𝐵) ≈ 0.25
𝑃(𝐵)
P(B)=0.25P(B|A)P(A)=0.17P(A|B)~0.68Posterior = useful evidence / total evidence
Where:
• 𝐶= class label
• 𝑋= input features

Types of Naïve Bayes:


• Gaussian Naïve Bayes
• Multinomial Naïve Bayes
• Bernoulli Naïve Bayes

Algorithm:
1. Load dataset
2. Calculate prior probabilities
3. Calculate likelihood probabilities
4. Apply Bayes’ theorem
5. Predict class with highest probability

Program (Python)
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB

# Sample dataset
data = {
'Age': [25, 30, 45, 35, 22],
'Income': [40000, 50000, 80000, 60000, 30000],
'Student': [0, 1, 0, 1, 0],
'Buy': [0, 1, 1, 1, 0]
}

df = [Link](data)

# Features and target


X = df[['Age', 'Income', 'Student']]
y = df['Buy']

# Split data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

# Model
model = GaussianNB()
[Link](X_train, y_train)

# Prediction
y_pred = [Link](X_test)

print("Predicted Output:", y_pred)


EXPERIMENT NO: 7

Aim:
Implement K-Nearest Neighbours (KNN) and K-Means Clustering

Theory
K-Nearest Neighbours (KNN)
KNN is a supervised learning algorithm used for classification and regression.
• It classifies data based on nearest neighbors
• Uses distance metric (Euclidean distance)

Distance Formula:

𝑑 = √(𝑥1 − 𝑥2 )2 + (𝑦1 − 𝑦2 )2

K-Means Clustering
K-Means is an unsupervised learning algorithm used to group data into clusters.

Steps:
• Choose number of clusters (K)
• Assign points to nearest centroid
• Update centroids
• Repeat until convergence

Algorithm
KNN:
1. Choose value of K
2. Calculate distance from test point
3. Select K nearest neighbors
4. Assign majority class

K-Means:
1. Initialize K centroids
2. Assign data points to nearest centroid
3. Update centroid positions
4. Repeat until stable

Program (Python)
KNN Implementation
import pandas as pd
from sklearn.model_selection import train_test_split
from [Link] import KNeighborsClassifier

# Sample dataset
data = {
'Age': [25, 30, 45, 35, 22],
'Income': [40000, 50000, 80000, 60000, 30000],
'Buy': [0, 1, 1, 1, 0]
}

df = [Link](data)

X = df[['Age', 'Income']]
y = df['Buy']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

model = KNeighborsClassifier(n_neighbors=3)
[Link](X_train, y_train)

y_pred = [Link](X_test)

print("KNN Prediction:", y_pred)

K-Means Implementation
import pandas as pd
from [Link] import KMeans
import [Link] as plt
# Sample dataset
data = {
'X': [1, 2, 3, 8, 9, 10],
'Y': [2, 3, 4, 8, 9, 10]
}

df = [Link](data)

# Model
kmeans = KMeans(n_clusters=2)
[Link](df)

# Output
labels = kmeans.labels_
centroids = kmeans.cluster_centers_

print("Cluster Labels:", labels)


print("Centroids:", centroids)

# Plot
[Link](df['X'], df['Y'], c=labels)
[Link](centroids[:,0], centroids[:,1], marker='x')
[Link]()

Input:
Dataset with features

Output:
• KNN → Predicted class
• K-Means → Clusters and centroids

Result:
KNN and K-Means algorithms were successfully implemented for classification and clustering tasks.
Applications:
KNN:
• Recommendation systems
• Pattern recognition
K-Means:
• Customer segmentation
• Image compression
EXPERIMENT NO: 8

Aim:
Deploy Support Vector Machine (SVM) and Apriori Algorithm

Theory
Support Vector Machine (SVM)
SVM is a supervised learning algorithm used for classification and regression.
• It finds the optimal hyperplane that separates data into classes
• The best hyperplane maximizes the margin between classes

Key Concepts:
• Support Vectors → Data points closest to boundary
• Margin → Distance between classes
• Kernel Trick → Handles non-linear data

Apriori Algorithm
Apriori is an unsupervised learning algorithm used for association rule mining.
• Finds frequent itemsets in a dataset
• Uses minimum support and confidence

Example:
If people buy bread, they also buy butter

Algorithm
SVM:
1. Import dataset
2. Split data into training and testing sets
3. Train SVM model
4. Predict output
5. Evaluate results

Apriori:
1. Set minimum support
2. Generate frequent itemsets
3. Generate association rules
4. Filter rules based on confidence

Program (Python)
SVM Implementation
import pandas as pd
from sklearn.model_selection import train_test_split
from [Link] import SVC

# Sample dataset
data = {
'Age': [25, 30, 45, 35, 22],
'Income': [40000, 50000, 80000, 60000, 30000],
'Buy': [0, 1, 1, 1, 0]
}

df = [Link](data)

X = df[['Age', 'Income']]
y = df['Buy']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

# Model
model = SVC(kernel='linear')
[Link](X_train, y_train)

# Prediction
y_pred = [Link](X_test)

print("SVM Prediction:", y_pred)

Apriori Implementation
import pandas as pd
from mlxtend.frequent_patterns import apriori, association_rules

# Sample dataset (transaction format)


data = {
'Milk': [1, 0, 1, 1, 0],
'Bread': [1, 1, 1, 0, 1],
'Butter': [0, 1, 1, 1, 0]
}

df = [Link](data)

# Frequent itemsets
frequent_items = apriori(df, min_support=0.4, use_colnames=True)

# Association rules
rules = association_rules(frequent_items, metric="confidence", min_threshold=0.6)

print("Frequent Itemsets:\n", frequent_items)


print("\nAssociation Rules:\n", rules)

Input:
• SVM → Dataset with features
• Apriori → Transaction dataset

Output:
• SVM → Predicted class labels
• Apriori → Frequent itemsets and rules

Result:
SVM and Apriori algorithms were successfully implemented for classification and association rule
mining.

Applications:
SVM:
• Image classification
• Face detection
• Text classification
Apriori:
• Market basket analysis
• Recommendation systems
EXPERIMENT NO: 9

Aim:
Simulate Artificial Neural Network (ANN)

Theory:
An Artificial Neural Network (ANN) is a computational model inspired by the human brain. It
consists of layers of interconnected nodes (neurons).

Structure of ANN:
• Input Layer → Receives data
• Hidden Layer(s) → Processes data
• Output Layer → Produces result

Working:
Each neuron performs:
1. Weighted sum of inputs
2. Apply activation function

Activation Function (example):


1
𝑓(𝑥) =
1 + 𝑒 −𝑥

(Sigmoid function)

Learning Process:
• Forward propagation
• Error calculation
• Backpropagation
• Weight update

Algorithm:
1. Initialize weights randomly
2. Perform forward propagation
3. Compute error
4. Apply backpropagation
5. Update weights
6. Repeat until error is minimized

Program (Python using Neural Network)


import numpy as np
from sklearn.neural_network import MLPClassifier

# Sample dataset
X = [Link]([[0,0],[0,1],[1,0],[1,1]])
y = [Link]([0, 1, 1, 0]) # XOR problem

# Model
model = MLPClassifier(hidden_layer_sizes=(2,), max_iter=1000)

# Train model
[Link](X, y)

# Prediction
pred = [Link](X)

print("Predicted Output:", pred)

Input:
Training data (XOR inputs)

Output:
Predicted outputs after training

Result:
The Artificial Neural Network was successfully implemented and used for classification.

Applications:
• Image recognition
• Speech recognition
• Medical diagnosis
EXPERIMENT NO: 10

Aim:
Implement Genetic Algorithm

Theory:
A Genetic Algorithm is a search and optimization technique inspired by natural selection and
genetics.
It works on a population of candidate solutions and improves them using evolutionary operations.

Key Concepts:
• Population → Set of possible solutions
• Chromosome → Representation of solution
• Fitness Function → Evaluates solution quality
• Selection → Choose best individuals
• Crossover → Combine parents
• Mutation → Random changes

Working:
1. Initialize population
2. Evaluate fitness
3. Select best individuals
4. Apply crossover
5. Apply mutation
6. Repeat until optimal solution found

Algorithm:
1. Generate initial population randomly
2. Calculate fitness for each chromosome
3. Select parents based on fitness
4. Perform crossover to produce offspring
5. Apply mutation
6. Replace old population
7. Repeat until stopping condition

Program (Python)
import random

# Fitness function: maximize f(x) = x^2


def fitness(x):
return x * x

# Generate initial population


population = [[Link](0, 31) for _ in range(6)]

generations = 10

for gen in range(generations):


print(f"Generation {gen}: {population}")

# Calculate fitness
population = sorted(population, key=lambda x: fitness(x), reverse=True)

# Selection (top 2)
parent1, parent2 = population[0], population[1]

# Crossover
crossover_point = 2
child = (parent1 & (15 << crossover_point)) | (parent2 & ~(15 << crossover_point))

# Mutation
mutation_point = [Link](0, 4)
child ^= (1 << mutation_point)

# Replace worst
population[-1] = child
print("\nBest Solution:", max(population, key=fitness))

Input:
Initial population (random values)

Output:
Best solution after several generations

Result:
The Genetic Algorithm was successfully implemented and used to find an optimal solution.

Applications:
• Optimization problems
• Scheduling
• Machine learning parameter tuning
• Game strategy optimization

You might also like