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

DWM Exp 8

Uploaded by

divyachavannn
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)
9 views2 pages

DWM Exp 8

Uploaded by

divyachavannn
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

Experiment No 8

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression

# 1. Load the data (already done, so we are using 'data')


data = pd.read_csv("Salary.csv")
X = data[['YearsExperience']] # Independent variable (Years of Experience)
Y = data[['Salary']] # Dependent variable (Salary)

# 2. Split the data into training and test sets


X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=42)

# 3. Fit a Linear Regression model


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

# Predict on the test data


Y_pred = model.predict(X_test)

# 4. Print learned parameters


print(f"Learned parameters: W = {model.coef_[0][0]}, b = {model.intercept_[0]}")

# 5. Visualize the regression line against the actual data


plt.figure(figsize=(8, 6))

# Plot the training data points


plt.scatter(X_train, Y_train, color='blue', label='Training data')

# Plot the regression line


plt.plot(X_train, model.predict(X_train), color='red', label='Regression line')

plt.title("Linear Regression: Salary vs Years of Experience")


plt.xlabel("Years of Experience")
plt.ylabel("Salary")
plt.legend()
plt.show()

# 6. Evaluate the model (optional)


r2_score = model.score(X_test, Y_test)
print(f"R^2 score: {r2_score}")
Output :-

Learned parameters: W = 8578.767476685925, b = 29078.626034406887


R^2 score: 0.8914234140042779

You might also like