0% found this document useful (0 votes)
22 views15 pages

47 Exp2 Dav

The document outlines an experiment for a Data Analytics and Visualization Lab course, focusing on the implementation of linear regression. It includes objectives, procedures for both R and Python programming, and evaluation criteria for performance and understanding. The conclusion emphasizes the importance of the summary() function in R for assessing model performance.
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)
22 views15 pages

47 Exp2 Dav

The document outlines an experiment for a Data Analytics and Visualization Lab course, focusing on the implementation of linear regression. It includes objectives, procedures for both R and Python programming, and evaluation criteria for performance and understanding. The conclusion emphasizes the importance of the summary() function in R for assessing model performance.
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/ 15

ACADEMIC YEAR: 2024-25

Course: Data Analytics And Visualization Lab


Course code: CSL601
Year/Sem: TE/VI

Experiment No.: 02

Aim: To write the implementation of linear regression.

Name: Bhoomi Vipul Sherkar

Roll Number: 47

Date of Performance: 24/01/25

Date of Submission: 31/01/25

Evaluation
Marks
Performance Indicator Max. Marks
Obtaine
d
Performance 5
Understanding 5
Journal work and timely submission. 10
Total 20

Performance Exceed Expectations Meet Expectations Below Expectations


Indicator (EE) (ME) (BE)
Performance 5 3 2
Understanding 5 3 2
Journal work and
10 8 4
timely submission.
Checked
by Name of Faculty​ : Mrs. Komal Champanerkar
Signature​ :
Date​ :
EXPERIMENT NO 2

AIM: To write the implementation of linear regression.

Objective:- To understand the use of simple linear regression techniques by implementing user define
dataset and importing dataset.

Description:

Procedure:

LM(): Fits a linear model in R. Example: model <- lm(Sales ~ TV, data = df) fits a
regression model predicting Sales from TV ad spend.

Predict(): Makes predictions using the fitted model. Example: predictions <- predict(model,
newdata = test_data).

Summary(): Provides detailed statistics of the fitted model, including coefficients,


R-squared, and p-values. Example: summary(model).

PROGRAM:- 1. Write a Simple Linear Regression program for user define dataset

X=c(1,2,3,4)

Y=c(3,4,5,7)

plot(X,Y)

relation=lm(Y~X)

print(relation)

print(summary(relation)

) a=data.frame(X=170)

result=predict(relation,a

) print(result)

png(file="linearregression.png")
plot(Y,X,col="green",main="Height & Weight Regression",abline(lm(X~Y)),
cex=1.3,pch=16,Xlab="Weight in kg",Ylab="Height in cm")

dev.off()

RESULT:

Thus the implementation of linear regression was executed and verified successfully.

OUTPUT:
PROGRAM:- 2. Write a Simple Linear Regression program for in-built dataset

data()

print(Orange)

data (Orange)

head (Orange)

plot (Orange $circumference, Orange $age)

model <- lm(circumference~age, data = Orange)

print(model)

print(predict(model))
summary(model)

plot(Orange$age, Orange$circumference, col = 'green', main = 'Simple Linear Regression',

cex = 1.5, pch = 16, xlab = 'Age', ylab = 'Circumference')

abline(model, col = 'blue', lwd = 2)

Output
PROGRAM 2:
PROGRAM:- 3. Write a Simple Linear Regression program for external datasets

data<- read.csv("C:\\R Studio\\advertising.csv")

print(head (data))

plot(data $TV,data $Sales)

relation = lm(Sales~TV, data = data)

print(relation)

print(summary(relation))

Output
With Python:

1]​ PROGRAM:- 1. Write a Simple Linear Regression program for user define

dataset Code:

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

from sklearn.linear_model import LinearRegression

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

Y = np.array([3, 4, 5, 7])
plt.scatter(X, Y)

plt.xlabel('X')

plt.ylabel('Y')

plt.title('Scatter Plot of X and Y')

plt.show()

model = LinearRegression()

model.fit(X, Y)

print(f'Coefficients: {model.coef_}')

print(f'Intercept: {model.intercept_}')

print(f'R^2 Score: {model.score(X, Y)}')

a = pd.DataFrame({'X': [170]})

result = model.predict(a)

print(f'Predicted value for X=170: {result[0]}')

Output:
2]​Program 2: Write a Simple Linear Regression program for external datasets
Code:

import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score

data = pd.read_csv("C:\\R Studio\\advertising.csv")


print(data.head())

plt.scatter(data['TV'], data['Sales'])
plt.xlabel('TV Advertising Budget')
plt.ylabel('Sales')
plt.title('TV Advertising Budget vs Sales')
plt.show()

X = data['TV'].values.reshape(-1, 1)
y = data['Sales'].values

model = LinearRegression()
model.fit(X, y)

print("Intercept:", model.intercept_)
print("Slope:", model.coef_[0])

y_pred = model.predict(X)
print("Mean Squared Error:", mean_squared_error(y, y_pred))
print("R^2 Score:", r2_score(y, y_pred))

Output:
Conclusion:

1.​ Function used for linear regression in R is ‘lm(formula, data)’ (Function_name (Parameters)).

2.​ Explain use of summary().


→ The summary() function in R gives a detailed breakdown of a model’s performance. When used
with lm(), it shows key statistics like coefficients, R-squared (how well the model fits), and p-values
(whether predictors are significant). It also provides residual analysis to check errors. Basically, it helps
you understand if your model is good or needs improvement.

You might also like