DWM Exp 8

Download as pdf or txt
Download as pdf or txt
You are on page 1of 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy