Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,48 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
# Load the saved model
|
| 4 |
from keras.models import load_model
|
| 5 |
-
from keras.losses import mean_squared_error
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
# Load
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
# Function to predict the irradiance
|
| 13 |
def predict_irradiance(month, hour, latitude, longitude, panel_capacity, panel_efficiency, wind_speed, cloud_cover, temperature):
|
| 14 |
# Encode the month and hour
|
| 15 |
encoded_month_hour = encoder.transform([[month, hour]])
|
|
@@ -23,19 +56,22 @@ def predict_irradiance(month, hour, latitude, longitude, panel_capacity, panel_e
|
|
| 23 |
predicted_irradiance = loaded_model.predict(reshaped_features)
|
| 24 |
return max(predicted_irradiance[0][0], 0.0)
|
| 25 |
|
| 26 |
-
# Function to get the actual irradiance for a given month
|
| 27 |
def get_actual_irradiance(month):
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
month = 'January'
|
| 33 |
hour = 12
|
| 34 |
predicted_irradiance = predict_irradiance(month, hour, 28.570633, 77.327215, 500, 0.15, 6.43988, 17.7, 55)
|
| 35 |
print(f'Predicted irradiance for {month}, hour {hour}: {predicted_irradiance}')
|
| 36 |
|
| 37 |
# Plot Actual vs. Predicted Irradiance for a specific month
|
| 38 |
-
month = 'January'
|
| 39 |
actual_irradiance = get_actual_irradiance(month)
|
| 40 |
predicted_irradiances = []
|
| 41 |
|
|
@@ -44,10 +80,10 @@ for hour in range(24):
|
|
| 44 |
predicted_irradiances.append(irradiance)
|
| 45 |
|
| 46 |
plt.figure(figsize=(12, 6))
|
| 47 |
-
plt.plot(range(24), actual_irradiance, label='Actual Irradiance')
|
| 48 |
plt.plot(range(24), predicted_irradiances, label='Predicted Irradiance')
|
| 49 |
plt.xlabel('Hour')
|
| 50 |
plt.ylabel('Irradiance (W/m^2)')
|
| 51 |
plt.title(f'Actual vs. Predicted Irradiance for {month}')
|
| 52 |
plt.legend()
|
| 53 |
-
plt.show()
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pickle
|
|
|
|
| 3 |
from keras.models import load_model
|
| 4 |
+
from keras.losses import mean_squared_error
|
| 5 |
+
from sklearn.preprocessing import OneHotEncoder, StandardScaler
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
|
| 8 |
+
# Load the saved model
|
| 9 |
+
loaded_model = load_model('solar_irradiance_model.h5', custom_objects={'mse': mean_squared_error})
|
| 10 |
|
| 11 |
+
# Load or initialize the encoder
|
| 12 |
+
try:
|
| 13 |
+
with open('encoder.pkl', 'rb') as file:
|
| 14 |
+
encoder = pickle.load(file)
|
| 15 |
+
except FileNotFoundError:
|
| 16 |
+
# Define and fit the encoder
|
| 17 |
+
encoder = OneHotEncoder(sparse_output=False)
|
| 18 |
+
month_hour_data = np.array([
|
| 19 |
+
['January', 0], ['February', 0], ['March', 0], ['April', 0],
|
| 20 |
+
['May', 0], ['June', 0], ['July', 0], ['August', 0],
|
| 21 |
+
['September', 0], ['October', 0], ['November', 0], ['December', 0]
|
| 22 |
+
])
|
| 23 |
+
# Adding all 24 hours for each month (simplified for example)
|
| 24 |
+
month_hour_data = np.concatenate(
|
| 25 |
+
[np.column_stack((month_hour_data[:, 0], np.arange(24))) for _ in range(12)]
|
| 26 |
+
)
|
| 27 |
+
encoder.fit(month_hour_data)
|
| 28 |
+
with open('encoder.pkl', 'wb') as file:
|
| 29 |
+
pickle.dump(encoder, file)
|
| 30 |
|
| 31 |
+
# Load or initialize the scaler
|
| 32 |
+
try:
|
| 33 |
+
with open('scaler.pkl', 'rb') as file:
|
| 34 |
+
scaler = pickle.load(file)
|
| 35 |
+
except FileNotFoundError:
|
| 36 |
+
# Placeholder: Fit the scaler on the actual dataset during training
|
| 37 |
+
scaler = StandardScaler()
|
| 38 |
+
# Assuming some training data is available for fitting
|
| 39 |
+
# Replace `training_features` with your actual training numerical features
|
| 40 |
+
training_features = np.random.rand(100, 7) # Dummy data
|
| 41 |
+
scaler.fit(training_features)
|
| 42 |
+
with open('scaler.pkl', 'wb') as file:
|
| 43 |
+
pickle.dump(scaler, file)
|
| 44 |
|
| 45 |
+
# Function to predict the irradiance
|
| 46 |
def predict_irradiance(month, hour, latitude, longitude, panel_capacity, panel_efficiency, wind_speed, cloud_cover, temperature):
|
| 47 |
# Encode the month and hour
|
| 48 |
encoded_month_hour = encoder.transform([[month, hour]])
|
|
|
|
| 56 |
predicted_irradiance = loaded_model.predict(reshaped_features)
|
| 57 |
return max(predicted_irradiance[0][0], 0.0)
|
| 58 |
|
| 59 |
+
# Function to get the actual irradiance for a given month (assuming `data` exists)
|
| 60 |
def get_actual_irradiance(month):
|
| 61 |
+
# Replace `data` with your actual dataset
|
| 62 |
+
data = {
|
| 63 |
+
'Month': ['January', 'January', 'February', 'February'], # Example
|
| 64 |
+
'Irradiance(W/m^2)': [500, 520, 480, 510]
|
| 65 |
+
}
|
| 66 |
+
return [data['Irradiance(W/m^2)'][i] for i in range(len(data['Month'])) if data['Month'][i] == month]
|
| 67 |
|
| 68 |
+
# Example usage
|
| 69 |
month = 'January'
|
| 70 |
hour = 12
|
| 71 |
predicted_irradiance = predict_irradiance(month, hour, 28.570633, 77.327215, 500, 0.15, 6.43988, 17.7, 55)
|
| 72 |
print(f'Predicted irradiance for {month}, hour {hour}: {predicted_irradiance}')
|
| 73 |
|
| 74 |
# Plot Actual vs. Predicted Irradiance for a specific month
|
|
|
|
| 75 |
actual_irradiance = get_actual_irradiance(month)
|
| 76 |
predicted_irradiances = []
|
| 77 |
|
|
|
|
| 80 |
predicted_irradiances.append(irradiance)
|
| 81 |
|
| 82 |
plt.figure(figsize=(12, 6))
|
| 83 |
+
plt.plot(range(24), actual_irradiance[:24], label='Actual Irradiance') # Ensure actual data matches 24 hours
|
| 84 |
plt.plot(range(24), predicted_irradiances, label='Predicted Irradiance')
|
| 85 |
plt.xlabel('Hour')
|
| 86 |
plt.ylabel('Irradiance (W/m^2)')
|
| 87 |
plt.title(f'Actual vs. Predicted Irradiance for {month}')
|
| 88 |
plt.legend()
|
| 89 |
+
plt.show()
|