0% found this document useful (0 votes)
7 views2 pages

Linear Regression

The document describes a Python program that uses linear regression to model the relationship between load and effort based on user input. It prompts the user to enter a specified number of data points, fits a linear regression model, and outputs the regression equation. Additionally, it allows the user to predict effort for a given load value.
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)
7 views2 pages

Linear Regression

The document describes a Python program that uses linear regression to model the relationship between load and effort based on user input. It prompts the user to enter a specified number of data points, fits a linear regression model, and outputs the regression equation. Additionally, it allows the user to predict effort for a given load value.
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/ 2

NAME: VALLABH KHANDAGALE

DIV: TE MECH (A)


ROLL NO & BATCH: A1-15

import numpy as np
from sklearn.linear_model import LinearRegression

# 2) accept number of datapoints from user. Input number of data


points n.
n = int(input("Enter the number of data points: "))

# 3) Initialize two empty lists for load and effort.


load = []
effort = []

# 4) apply for loop,, Repeat for each data point i = 1 to n:


for i in range(n):
# Accept Load (x) from user.
x = float(input(f"Enter Load for data point {i+1}: "))
# Accept Effort (y) from user.
y = float(input(f"Enter Effort for data point {i+1}: "))
# Append x to load list.
load.append(x)
# Append y to effort list.
effort.append(y)

# 5) Convert load and effort lists into NumPy arrays.


load = np.array(load)
effort = np.array(effort)

# Reshape load into a column vector.


load = load.reshape(-1, 1)

# 6) Create a LinearRegression model.


model = LinearRegression()

# 7) Fit the model using (load, effort).


model.fit(load, effort)

# 8) Extract slope, a = model.coef_ and intercept, b =


model.intercept_.
a = model.coef_[0]
b = model.intercept_

# 9) Display regression equation:


# Effort=a×Load+ b use format with curly bracket.
print(f"Regression Equation: Effort = {a:.2f} * Load + {b:.2f}")

# 10) Accept input from user for prediction. (here 18 kg)


prediction_load = float(input("Enter the load for prediction: "))
# 11) Predict effort using the trained model for load = 18.
predicted_effort = model.predict([[prediction_load]])

# 12) Print predicted value of effort.


print(f"Predicted effort for load {prediction_load} is:
{predicted_effort[0]:.2f}")

Enter the number of data points: 5


Enter Load for data point 1: 10
Enter Effort for data point 1: 0.75
Enter Load for data point 2: 15
Enter Effort for data point 2: 0.935
Enter Load for data point 3: 20
Enter Effort for data point 3: 1.1
Enter Load for data point 4: 25
Enter Effort for data point 4: 1.2
Enter Load for data point 5: 30
Enter Effort for data point 5: 1.3
Regression Equation: Effort = 0.03 * Load + 0.51
Enter the load for prediction: 18
Predicted effort for load 18.0 is: 1.00

You might also like