# Practical: 1 Simple Linear Regression

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_absolute_error

df = pd.read_csv('Study.csv')

x = df.iloc[:,0].values.reshape(-1, 1)
y = df.iloc[:,1]

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=10)

regressor = LinearRegression()
regressor.fit(x_train, y_train)
regressor_pred = regressor.predict(x_test)

mean_absolute_error(y_test, regressor_pred)

plt.scatter(x_train, y_train, color='blue', label='Training data')
plt.scatter(x_test, y_test, color='green', label='Test data')
plt.plot(x_train, regressor.predict(x_train), color='red', label='Regression Line')
plt.xlabel('Hours Studied')
plt.ylabel('Score')
plt.title('Simple Linear Regression')
plt.legend()
plt.grid(True)
plt.show()