#pr5 linear regression and MSE,MAE,rsm

import pandas as pd                
import numpy as np                 
import matplotlib.pyplot as plt    
from sklearn.linear_model import LinearRegression   
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score  


df = pd.read_csv("temperatures.csv")   
data = df[['YEAR', 'ANNUAL']]         
data = data.dropna()                   
print(data)


X = data['YEAR'].values.reshape(-1, 1)  
y = data['ANNUAL'].values  


model = LinearRegression()              
model.fit(X, y)                        
y_pred = model.predict(X)  


mse = mean_squared_error(y, y_pred)    
mae = mean_absolute_error(y, y_pred)   
r2 = r2_score(y, y_pred)                
print("MSE =", mse)
print("MAE =", mae)
print("R² =", r2)


plt.figure(figsize=(10, 6))
plt.scatter(X, y, color='blue', label='Actual Data')    
plt.plot(X, y_pred, color='red', label='Regression Line') 
plt.xlabel('YEAR')
plt.ylabel('Annual Temperature (°C)')
plt.title('Year vs Annual Temperature (Linear Regression)')
plt.legend()
plt.show()