import pandas as pd import numpy as np from sklearn.linearmodel import LinearRegression…
- User
import pandas as pd import numpy as np from sklearn.linear_model import LinearRegression import matplotlib.pyplot as plt
Load the dataset (assuming you have a .csv file for the data)
data = pd.read_csv('/content/TEMP_ANNUAL_MEAN_1901-2021.csv')
Separate features (years) and target (temperature columns)
X = data[['YEAR']] pred_years = np.arange(X['YEAR'].max() + 1, X['YEAR'].max() + 21).reshape(-1, 1)
Columns for predictions (monthly, seasonal, and annual averages)
columns_to_predict = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC', 'ANNUAL', 'JAN-FEB', 'MAR-MAY', 'JUN-SEP', 'OCT-DEC']
Store predictions
predictions = pd.DataFrame(pred_years, columns=['YEAR'])
Initialize plot
plt.figure(figsize=(15, 20)) plot_idx = 1
Iterate over each column to train the model and make predictions
for col in columns_to_predict: y = data[[col]]
# Train a linear regression model model = LinearRegression() model.fit(X, y) # Forecast for the next 20 years forecast = model.predict(pred_years) predictions[col] = forecast.flatten() # Save forecast to predictions DataFrame # Plot the actual data and predictions plt.subplot(5, 4, plot_idx) plt.plot(X, y, label=f'Actual {col}', color='blue') plt.plot(pred_years, forecast, label=f'Forecast {col}', color='red', linestyle='--') plt.xlabel('Year') plt.ylabel(f'{col} Temperature (°C)') plt.legend() plt.title(f'{col} Temperature Prediction') plot_idx += 1Display the plots
plt.tight_layout() plt.show()
Save predictions to a new CSV file
predictions.to_csv('temperature_forecasts.csv', index=False)
This code works, but it generates the following warnings: /usr/local/lib/python3.10/dist-packages/sklearn/base.py:493: UserWarning: X does not have valid feature names, but LinearRegression was fitted with feature names warnings.warn( /usr/local/lib/python3.10/dist-packages/sklearn/base.py:493: UserWarning: X does not have valid feature names, but LinearRegression was fitted with feature names warnings.warn( /usr/local/lib/python3.10/dist-packages/sklearn/base.py:493: UserWarning: X does not have valid feature names, but LinearRegression was fitted with feature names warnings.warn( /usr/local/lib/python3.10/dist-packages/sklearn/base.py:493: UserWarning: X does not have valid feature names, but LinearRegression was fitted with feature names warnings.warn( /usr/local/lib/python3.10/dist-packages/sklearn/base.py:493: UserWarning: X does not have valid feature names, but LinearRegression was fitted with feature names warnings.warn( /usr/local/lib/python3.10/dist-packages/sklearn/base.py:493: UserWarning: X does not have valid feature names, but LinearRegression was fitted with feature names warnings.warn( /usr/local/lib/python3.10/dist-packages/sklearn/base.py:493: UserWarning: X does not have valid feature names, but LinearRegression was fitted with feature names warnings.warn( /usr/local/lib/python3.10/dist-packages/sklearn/base.py:493: UserWarning: X does not have valid feature names, but LinearRegression was fitted with feature names warnings.warn( /usr/local/lib/python3.10/dist-packages/sklearn/base.py:493: UserWarning: X does not have valid feature names, but LinearRegression was fitted with feature names warnings.warn( /usr/local/lib/python3.10/dist-packages/sklearn/base.py:493: UserWarning: X does not have valid feature names, but LinearRegression was fitted with feature names warnings.warn( /usr/local/lib/python3.10/dist-packages/sklearn/base.py:493: UserWarning: X does not have valid feature names, but LinearRegression was fitted with feature names warnings.warn( /usr/local/lib/python3.10/dist-packages/sklearn/base.py:493: UserWarning: X does not have valid feature names, but LinearRegression was fitted with feature names warnings.warn( /usr/local/lib/python3.10/dist-packages/sklearn/base.py:493: UserWarning: X does not have valid feature names, but LinearRegression was fitted with feature names warnings.warn( /usr/local/lib/python3.10/dist-packages/sklearn/base.py:493: UserWarning: X does not have valid feature names, but LinearRegression was fitted with feature names warnings.warn( /usr/local/lib/python3.10/dist-packages/sklearn/base.py:493: UserWarning: X does not have valid feature names, but LinearRegression was fitted with feature names warnings.warn( /usr/local/lib/python3.10/dist-packages/sklearn/base.py:493: UserWarning: X does not have valid feature names, but LinearRegression was fitted with feature names warnings.warn( /usr/local/lib/python3.10/dist-packages/sklearn/base.py:493: UserWarning: X does not have valid feature names, but LinearRegression was fitted with feature names warnings.warn( Can you fix this issue?
- Assistant