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