0% found this document useful (0 votes)
60 views10 pages

Lab Experiments Vi Sem-1

The document describes 10 experiments implementing machine learning algorithms and models including linear regression, perceptron algorithm, and case study on an auto insurance dataset. The experiments load data, estimate statistics, coefficients, and apply algorithms like linear regression, stochastic gradient descent, and perceptron learning.

Uploaded by

kunnu6263
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)
60 views10 pages

Lab Experiments Vi Sem-1

The document describes 10 experiments implementing machine learning algorithms and models including linear regression, perceptron algorithm, and case study on an auto insurance dataset. The experiments load data, estimate statistics, coefficients, and apply algorithms like linear regression, stochastic gradient descent, and perceptron learning.

Uploaded by

kunnu6263
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/ 10

Enrollment no:

Name:

Semester: 6th

Year : 3rd

Subject: Machine Learning Subject Code: CS601


IES COLLEGE OF TECHNOLOGY, BHOPAL /
IES INSTITUTE OF TECHNOLOGY & MANGEMENT
DEPARTMENT OF COMPUTER SCIENCE& ENGINEERING
CS601MACHINE LEARNING (LAB)

Course
S. No Name of Experiment
Outcome

Implement and Load data from CSV for finding the most specific CL601.1
01
hypothesis based on a given set of training data samples.

02 Explain and Evaluation of simple Linear Regression models. CL601.2

03 Implement and Estimate Mean and Variance for both X and Y. CL601.1

04 Implement and Estimate Covariance for both X CL601.2


and Y.

05 Implement and Estimate linear regression coefficients from data. CL601.4

06 Implement and Estimate coefficients for multivariate linear regression. CL601.3


Implement and Estimate linear regression coefficients using stochastic
07 gradient descent. CL601.4
08 Implement the Perceptron algorithm and Estimate Perceptron weights. CL601.2
09 Case Study on Auto Insurance Dataset. CL601.5
10 Write a python program to find linear regression. CL601.5
1. Implement and Load Data from CSV for Finding the Most Specific
Hypothesis

Theory: The most specific hypothesis is the hypothesis that is as specific as possible while still being
consistent with the training examples. It is used in the Candidate Elimination algorithm, which
updates the hypothesis as more positive examples are encountered.

Implementation:

import csv

def load_csv(filename):
lines = csv.reader(open(filename, "r"))
dataset = list(lines)
headers = dataset.pop(0)
return dataset

def find_most_specific_hypothesis(data):
specific_h = data[0][:-1]
for example in data:
if example[-1] == 'Yes':
for i in range(len(specific_h)):
if specific_h[i] != example[i]:
specific_h[i] = '?'
return specific_h

filename = 'data.csv'
data = load_csv(filename)
hypothesis = find_most_specific_hypothesis(data)
print("Most Specific Hypothesis:", hypothesis)

2. Explain and Evaluate Simple Linear Regression Models

Theory: Linear regression models the relationship between two variables by fitting a linear equation
to observed data. The model assumes that the relationship between the dependent variable and the
independent variable is linear.

Implementation:

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(0)
X = 2 * np.random.rand(100, 1)
y = 4 + 3 * X + np.random.randn(100, 1)

X_b = np.c_[np.ones((100, 1)), X]


theta_best = np.linalg.inv(X_b.T.dot(X_b)).dot(X_b.T).dot(y)

X_new = np.array([[0], [2]])


X_new_b = np.c_[np.ones((2, 1)), X_new]
y_predict = X_new_b.dot(theta_best)

plt.plot(X_new, y_predict, "r-", linewidth=2, label="Predictions")


plt.plot(X, y, "b.", label="Actual Data")
plt.xlabel("X")
plt.ylabel("y")
plt.legend()
plt.show()

print("Intercept:", theta_best[0][0])
print("Slope:", theta_best[1][0])

3.Implement and Estimate Mean and Variance for Both X and Y

Theory: The mean is the average of a set of values. The variance measures the spread of the data
points around the mean.

Implementation:

import numpy as np

X = np.array([1, 2, 3, 4, 5])
Y = np.array([2, 3, 4, 5, 6])

mean_X = np.mean(X)
mean_Y = np.mean(Y)

var_X = np.var(X)
var_Y = np.var(Y)

print("Mean of X:", mean_X)


print("Mean of Y:", mean_Y)
print("Variance of X:", var_X)
print("Variance of Y:", var_Y)
4. Implement and Estimate Covariance for Both X and Y

Theory: Covariance measures the directional relationship between two random variables. A positive
covariance indicates that the variables increase together, while a negative covariance indicates that
one variable decreases as the other increases.

Implementation:

import numpy as np

X = np.array([1, 2, 3, 4, 5])
Y = np.array([2, 3, 4, 5, 6])

covariance = np.cov(X, Y, bias=True)[0][1]

print("Covariance between X and Y:", covariance)

5. Implement and Estimate Linear Regression Coefficients from Data

Theory: Linear regression coefficients can be estimated using the least squares method, which
minimizes the sum of the squared differences between the observed and predicted values.

Implementation:

import numpy as np

X = np.array([1, 2, 3, 4, 5]).reshape(-1, 1)

y = np.array([1, 2, 3, 4, 5])

X_b = np.c_[np.ones((5, 1)), X]

theta_best = np.linalg.inv(X_b.T.dot(X_b)).dot(X_b.T).dot(y)

print("Intercept:", theta_best[0])

print("Slope:", theta_best[1])
6. Implement and Estimate Coefficients for Multivariate Linear Regression

Theory: Multivariate linear regression involves more than one independent variable. The coefficients
can be estimated using the same least squares method as in simple linear regression but applied to
multiple variables.

Implementation:

import numpy as np

X = np.array([[1, 2], [2, 4], [3, 6], [4, 8], [5, 10]])

y = np.array([2, 3, 4, 5, 6])

X_b = np.c_[np.ones((5, 1)), X]

theta_best = np.linalg.inv(X_b.T.dot(X_b)).dot(X_b.T).dot(y)

print("Coefficients:", theta_best)

7. Implement and Estimate Linear Regression Coefficients using Stochastic


Gradient Descent

Theory: Stochastic Gradient Descent (SGD) is an iterative method for optimizing an objective
function by updating the model parameters in the direction of the gradient of the objective function
with respect to the parameters.

Implementation:

import numpy as np

X = np.array([[1], [2], [3], [4], [5]])

y = np.array([2, 3, 4, 5, 6])

m = len(X)

theta = np.random.randn(2, 1)

def predict(X, theta):

return np.dot(X, theta)


def compute_cost(X, y, theta):

predictions = predict(X, theta)

errors = predictions - y

return (1 / (2 * m)) * np.dot(errors.T, errors)

def gradient_descent(X, y, theta, learning_rate, iterations):

for i in range(iterations):

predictions = predict(X, theta)

errors = predictions - y

gradients = (1 / m) * np.dot(X.T, errors)

theta = theta - learning_rate * gradients

return theta

X_b = np.c_[np.ones((m, 1)), X]

learning_rate = 0.01

iterations = 1000

theta = gradient_descent(X_b, y, theta, learning_rate, iterations)

print("Coefficients using SGD:", theta)

8. Implement the Perceptron Algorithm and Estimate Perceptron Weights

Theory: The Perceptron algorithm is a type of linear classifier that updates its weights based on the
errors made on training examples. It is an early and simple form of a neural network.

Implementation:

import numpy as np

def step_function(t):
return 1 if t >= 0 else 0

def predict(X, weights):

return step_function(np.dot(X, weights))

def perceptron(X, y, learning_rate, epochs):

weights = np.zeros(X.shape[1])

for _ in range(epochs):

for i in range(len(X)):

prediction = predict(X[i], weights)

error = y[i] - prediction

weights += learning_rate * error * X[i]

return weights

X = np.array([[1, 0], [0, 1], [1, 1], [0, 0]])

y = np.array([1, 1, 1, 0])

learning_rate = 0.1

epochs = 10

weights = perceptron(X, y, learning_rate, epochs)

print("Perceptron Weights:", weights)

theta)

9. Case Study on Auto Insurance Dataset

Theory: A case study involves detailed analysis of a dataset to understand its characteristics, identify
patterns, and derive insights. In this case, we will use an auto insurance dataset to perform
exploratory data analysis (EDA) and apply machine learning techniques.

Implementation:

import pandas as pd
# Load the dataset

df = pd.read_csv('auto_insurance.csv')

# Display basic statistics

print(df.describe())

# Check for missing values

print(df.isnull().sum())

# Correlation matrix

print(df.corr())

# Simple EDA plots

import seaborn as sns

import matplotlib.pyplot as plt

sns.pairplot(df)

plt.show()

# Example: Predicting insurance claim amount using linear regression

from sklearn.model_selection import train_test_split

from sklearn.linear_model import LinearRegression

X = df[['Age', 'Number_of_Vehicles', 'Accidents']]

y = df['Claim_Amount']

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

model = LinearRegression()

model.fit(X_train, y_train)

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

10. Write a Python Program to Find Linear Regression

Theory: Linear regression aims to model the relationship between a dependent variable and one or
more independent variables by fitting a linear equation to observed data.

Implementation:

import numpy as np
import matplotlib.pyplot as plt

X = np.array([[1], [2], [3], [4], [5]])


y = np.array([2, 3, 4, 5, 6])

# Fit linear regression model


X_b = np.c_[np.ones((len(X), 1)), X]
theta_best = np.linalg.inv(X_b.T.dot(X_b)).dot(X_b.T).dot(y)

# Make predictions
X_new = np.array([[0], [6]])
X_new_b = np.c_[np.ones((len(X_new), 1)), X_new]
y_predict = X_new_b.dot(theta_best)

# Plot results
plt.plot(X_new, y_predict, "r-", linewidth=2, label="Predictions")
plt.plot(X, y, "b.", label="Actual Data")
plt.xlabel("X")
plt.ylabel("y")
plt.legend()
plt.show()

print("Intercept:", theta_best[0])
print("Slope:", theta_best[1])

You might also like