0% found this document useful (0 votes)
10 views5 pages

Prac2 174 Final

The document outlines a practical assignment to predict total payments for claims using linear regression on two datasets: Swedish auto insurance and salary data. It includes Python code for data loading, model training, prediction, and visualization, along with metrics like Mean Squared Error and R-squared. The program demonstrates how to fit a linear regression model and make predictions based on new input data.

Uploaded by

n0buhosasyk3
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)
10 views5 pages

Prac2 174 Final

The document outlines a practical assignment to predict total payments for claims using linear regression on two datasets: Swedish auto insurance and salary data. It includes Python code for data loading, model training, prediction, and visualization, along with metrics like Mean Squared Error and R-squared. The program demonstrates how to fit a linear regression model and make predictions based on new input data.

Uploaded by

n0buhosasyk3
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/ 5

Enrollment no: 202203103510174

Practical-2
Aim-
Write a program to predict total payment for given number of claims on Swedish auto insurance
dataset using linear regression.

❖ With Swedish auto insurance dataset :

Program Code-
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score

df = pd.read_csv("Swedish_Data.csv")

X = df[["X"]].values
Y = df["Y"]

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.1, random_state=0)

model = LinearRegression()
model.fit(X_train, Y_train)

Y_pred = model.predict(X_test)

mse = mean_squared_error(Y_test, Y_pred)


r2 = r2_score(Y_test, Y_pred)

slope = model.coef_[0]
intercept = model.intercept_

sns.scatterplot(data=df, x="X", y="Y", label='Data Points')


plt.plot(x_range, y_range, color='red', label='Regression Line')

UTU/CGPIT/B.TECH-IT-6 A/Machine Intelligence 18


Enrollment no: 202203103510174

plt.legend()
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Linear Regression Fit')
plt.show()

new_data = [[124]]
predicted_y = model.predict(new_data)
print("Predicted for", new_data[0][0], ":", predicted_y[0])

print(f"Slope: {slope}, Intercept: {intercept}")

print(f"R-squared: {r2}")
print(f"Mean Squared Error: {mse}")

UTU/CGPIT/B.TECH-IT-6 A/Machine Intelligence 19


Enrollment no: 202203103510174

Output-

UTU/CGPIT/B.TECH-IT-6 A/Machine Intelligence 20


Enrollment no: 202203103510174

❖ With other dataset :


Program Code-
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score

df = pd.read_csv("Salary_Dataset.csv")
X = df[["YearsExperience"]].values
Y = df["Salary"]

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.1, random_state=0)

model = LinearRegression()
model.fit(X_train, Y_train)

Y_pred = model.predict(X_test)

mse = mean_squared_error(Y_test, Y_pred)


r2 = r2_score(Y_test, Y_pred)

m = model.coef_.flatten()
b = model.intercept_.flatten()

plt.plot(X, Y, 'o', label='Data Points') # scatterplot


plt.plot(X, m*X+b, label='Regression Line') # line
plt.legend()
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Linear Regression Fit')
plt.show()

UTU/CGPIT/B.TECH-IT-6 A/Machine Intelligence 21


Enrollment no: 202203103510174

new_data = [[108.55]]
predicted_y = model.predict(new_data)

print("Predicted for",new_data[0][0], ":", predicted_y[0])

print(f"Slope: {m[0]}, Intercept: {b[0]}")


print(f"Mean Squared Error: {mse}")
print(f"R-squared: {r2}")

Output-

UTU/CGPIT/B.TECH-IT-6 A/Machine Intelligence 22

You might also like