-
Notifications
You must be signed in to change notification settings - Fork 0
/
Polynomial Regression.txt
41 lines (34 loc) · 1.47 KB
/
Polynomial Regression.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from datetime import datetime, timedelta
# Generate synthetic weather data for demonstration
np.random.seed(42)
days = 30
dates = [datetime(2024, 1, 1) + timedelta(days=i) for i in range(days)]
temperatures = np.random.randint(0, 30, days)
# Assume that the target variable (y) is temperature
X = np.arange(days).reshape(-1, 1)
y = temperatures
# Add polynomial features
poly_features = PolynomialFeatures(degree=2)
X_poly = poly_features.fit_transform(X)
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X_poly, y, test_size=0.2, random_state=42)
# Create and train the polynomial regression model
model = LinearRegression()
model.fit(X_train, y_train)
# Predict temperatures for the next 7 days
future_dates = [dates[-1] + timedelta(days=i) for i in range(1, 8)]
future_X = np.arange(days, days + 7).reshape(-1, 1)
future_X_poly = poly_features.transform(future_X)
future_predictions = model.predict(future_X_poly)
# Plot the results
plt.plot(dates, y, label="Actual Temperatures")
plt.plot(future_dates, future_predictions, label="Predicted Temperatures (Next 7 Days - Polynomial Regression)")
plt.xlabel("Date")
plt.ylabel("Temperature (°C)")
plt.title("Weather Prediction for the Next 7 Days (Polynomial Regression)")
plt.legend()
plt.show()