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

Assignment 2

The document outlines a data analysis assignment that involves predicting monthly temperatures using linear regression models based on historical data. It describes the process of loading temperature data, training models for each month, evaluating their performance using metrics like MSE, MAE, and R2, and visualizing the results. Additionally, it includes a function to predict future temperatures for specific months and years.

Uploaded by

shraddha.clg0905
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 views6 pages

Assignment 2

The document outlines a data analysis assignment that involves predicting monthly temperatures using linear regression models based on historical data. It describes the process of loading temperature data, training models for each month, evaluating their performance using metrics like MSE, MAE, and R2, and visualizing the results. Additionally, it includes a function to predict future temperatures for specific months and years.

Uploaded by

shraddha.clg0905
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/ 6

Assignment2 file:///home/oslab-09/Downloads/Assignment2.

html

In [6]: import pandas as pd


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

In [8]: df.columns

Out[8]: Index(['YEAR', 'JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP',
'OCT', 'NOV', 'DEC', 'ANNUAL', 'JAN-FEB', 'MAR-MAY', 'JUN-SEP',
'OCT-DEC'],
dtype='object')

In [14]: import pandas as pd


from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score

# Assuming df is already loaded


# df = pd.read_csv('temperature.csv')

months = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN',


'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC']
# this is for the 12 months calculate the 12 different models

# Reshape YEAR column once here: convert Series to 2D numpy array (n_samples, 1)
X_year = df['YEAR'].values.reshape(-1, 1)

# store the 12 models and 12 performances


models = {}
performance = {}

# Train models
for month in months:
y = df[month]# Select temperature data for this month as target variable y
model = LinearRegression()# Create a new Linear Regression model
model.fit(X_year, y) # Train the model using X_year (year) as input and y (temperature) as output
models[month] = model# Store the trained model in a dictionary with month as key

# Evaluate models
for month in months:
y_true = df[month]
y_pred = models[month].predict(X_year)

1 of 6 04/08/25, 15:53
Assignment2 file:///home/oslab-09/Downloads/Assignment2.html

#MSE-The average of the squares of the errors — the difference between actual and predicted values.
#MAE-The average of the absolute differences between actual and predicted values.
mse = mean_squared_error(y_true, y_pred)
mae = mean_absolute_error(y_true, y_pred)
r2 = r2_score(y_true, y_pred)

performance[month] = {'MSE': mse, 'MAE': mae, 'R2': r2}

# Print performance
for month in months:
perf = performance[month]
print(f"{month} -> MSE: {perf['MSE']:.3f}, MAE: {perf['MAE']:.3f}, R2: {perf['R2']:.3f}")

JAN -> MSE: 0.462, MAE: 0.527, R2: 0.331


FEB -> MSE: 0.763, MAE: 0.679, R2: 0.419
MAR -> MSE: 0.785, MAE: 0.714, R2: 0.307
APR -> MSE: 0.555, MAE: 0.585, R2: 0.292
MAY -> MSE: 0.434, MAE: 0.510, R2: 0.166
JUN -> MSE: 0.342, MAE: 0.482, R2: 0.138
JUL -> MSE: 0.168, MAE: 0.315, R2: 0.229
AUG -> MSE: 0.129, MAE: 0.282, R2: 0.428
SEP -> MSE: 0.164, MAE: 0.312, R2: 0.441
OCT -> MSE: 0.322, MAE: 0.428, R2: 0.347
NOV -> MSE: 0.260, MAE: 0.380, R2: 0.487
DEC -> MSE: 0.282, MAE: 0.381, R2: 0.536

In [15]: import matplotlib.pyplot as plt

months_to_plot = ['JAN', 'FEB', 'OCT', 'DEC']

plt.figure(figsize=(12, 10))

for i, month in enumerate(months_to_plot, 1):


y_true = df[month]
y_pred = models[month].predict(X_year)

plt.subplot(2, 2, i) # 2 rows, 2 cols grid


plt.scatter(df['YEAR'], y_true, color='blue', label='Actual')
plt.plot(df['YEAR'], y_pred, color='red', label='Predicted')
plt.title(f'Linear Regression for {month}')
plt.xlabel('Year')

2 of 6 04/08/25, 15:53
Assignment2 file:///home/oslab-09/Downloads/Assignment2.html

plt.ylabel('Temperature (°C)')
plt.legend()

plt.tight_layout()
plt.show()

3 of 6 04/08/25, 15:53
Assignment2 file:///home/oslab-09/Downloads/Assignment2.html

4 of 6 04/08/25, 15:53
Assignment2 file:///home/oslab-09/Downloads/Assignment2.html

In [16]: from statistics import mean

mean_mse = mean(performance[m]['MSE'] for m in months)


mean_mae = mean(performance[m]['MAE'] for m in months)
mean_r2 = mean(performance[m]['R2'] for m in months)

print("Average performance across all months:")


print(f"Mean MSE: {mean_mse:.3f}")#Lower MSE → better fit.
print(f"Mean MAE: {mean_mae:.3f}")#Lower MAE → better fit.
print(f"Mean R2 : {mean_r2:.3f}")#Closer to 1 → better model.Near 0 → model does no better than average prediction.

Average performance across all months:


Mean MSE: 0.389
Mean MAE: 0.466
Mean R2 : 0.343

In [17]: def predict_temperature(year, month):


"""
Predict temperature for a given year and month using the trained model.

Parameters:
- year: int or float, the year to predict temperature for
- month: str, month abbreviation in uppercase, e.g. 'JAN', 'FEB', etc.

Returns:
- predicted temperature (float)
"""
month = month.upper()
if month not in models:
return f"Error: Month '{month}' model not found. Choose from: {list(models.keys())}"

model = models[month]
year_reshaped = [[year]] # Reshape to 2D array as sklearn expects

pred_temp = model.predict(year_reshaped)[0] # Predict and get scalar value


return pred_temp

# Example usage:
print(predict_temperature(2025, 'JAN'))

5 of 6 04/08/25, 15:53
Assignment2 file:///home/oslab-09/Downloads/Assignment2.html

print(predict_temperature(1990, 'OCT'))

24.622018013157692
30.146403088112617

In [ ]:

6 of 6 04/08/25, 15:53

You might also like