help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • Blog
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
energy-consumption
RSK World
energy-consumption
Energy Consumption Dataset - Time Series Analysis + Energy Forecasting + Smart Grid Analytics
energy-consumption
  • __pycache__
  • .gitignore429 B
  • ADVANCED_FEATURES.md5.3 KB
  • ERRORS_FIXED.md2.9 KB
  • LICENSE1.3 KB
  • PROJECT_INFO.md2 KB
  • README.md5.3 KB
  • RELEASE_NOTES.md4.2 KB
  • advanced_analysis.py10.7 KB
  • analysis.py4.3 KB
  • anomaly_detection.py9 KB
  • energy_consumption.csv1.7 MB
  • energy_consumption.json7.4 MB
  • forecasting.py11.2 KB
  • generate_data.py5.5 KB
  • index.html21.4 KB
  • model_evaluation.py9.6 KB
  • preprocessing.py10.2 KB
  • requirements.txt303 B
  • visualization.py6.5 KB
forecasting.pyvisualization.py
forecasting.py
Raw Download
Find: Go to:
"""
Energy Consumption Dataset - Forecasting Models

Project: Energy Consumption Dataset
Author: RSK World
Website: https://rskworld.in
Email: help@rskworld.in
Phone: +91 93305 39277

Advanced forecasting models for energy consumption prediction.
"""

import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestRegressor
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
import warnings
warnings.filterwarnings('ignore')

class EnergyForecaster:
    """
    Advanced forecasting class for energy consumption prediction.
    """
    
    def __init__(self, df):
        """
        Initialize forecaster with data.
        
        Args:
            df: pandas.DataFrame containing energy consumption data
        """
        self.df = df.copy()
        self.models = {}
        self.scaler = StandardScaler()
        self.prepare_features()
    
    def prepare_features(self):
        """
        Create time-based features for forecasting.
        """
        self.df['timestamp'] = pd.to_datetime(self.df['timestamp'])
        self.df = self.df.sort_values('timestamp').reset_index(drop=True)
        
        # Time features
        self.df['year'] = self.df['timestamp'].dt.year
        self.df['month'] = self.df['timestamp'].dt.month
        self.df['day'] = self.df['timestamp'].dt.day
        self.df['day_of_year'] = self.df['timestamp'].dt.dayofyear
        iso_cal = self.df['timestamp'].dt.isocalendar()
        self.df['week_of_year'] = iso_cal['week'] if isinstance(iso_cal, pd.DataFrame) else iso_cal.week
        
        # Cyclical encoding for time features
        self.df['hour_sin'] = np.sin(2 * np.pi * self.df['hour'] / 24)
        self.df['hour_cos'] = np.cos(2 * np.pi * self.df['hour'] / 24)
        self.df['day_sin'] = np.sin(2 * np.pi * self.df['day_of_week'] / 7)
        self.df['day_cos'] = np.cos(2 * np.pi * self.df['day_of_week'] / 7)
        self.df['month_sin'] = np.sin(2 * np.pi * self.df['month'] / 12)
        self.df['month_cos'] = np.cos(2 * np.pi * self.df['month'] / 12)
        
        # Lag features
        for lag in [1, 24, 168]:  # 1 hour, 1 day, 1 week
            self.df[f'consumption_lag_{lag}'] = self.df.groupby('household_id')['consumption_kwh'].shift(lag)
        
        # Rolling statistics
        self.df['consumption_rolling_mean_24'] = self.df.groupby('household_id')['consumption_kwh'].transform(
            lambda x: x.rolling(window=24, min_periods=1).mean()
        )
        self.df['consumption_rolling_std_24'] = self.df.groupby('household_id')['consumption_kwh'].transform(
            lambda x: x.rolling(window=24, min_periods=1).std()
        )
        
        # Drop rows with NaN from lag features
        self.df = self.df.dropna().reset_index(drop=True)
    
    def train_test_split(self, test_size=0.2):
        """
        Split data into train and test sets.
        
        Args:
            test_size: Proportion of data for testing
        
        Returns:
            tuple: (X_train, X_test, y_train, y_test)
        """
        split_idx = int(len(self.df) * (1 - test_size))
        
        feature_cols = [
            'hour', 'day_of_week', 'month', 'day_of_year',
            'hour_sin', 'hour_cos', 'day_sin', 'day_cos', 'month_sin', 'month_cos',
            'temperature', 'consumption_lag_1', 'consumption_lag_24', 'consumption_lag_168',
            'consumption_rolling_mean_24', 'consumption_rolling_std_24'
        ]
        
        # Filter available columns
        feature_cols = [col for col in feature_cols if col in self.df.columns]
        
        X = self.df[feature_cols]
        y = self.df['consumption_kwh']
        
        X_train = X[:split_idx]
        X_test = X[split_idx:]
        y_train = y[:split_idx]
        y_test = y[split_idx:]
        
        return X_train, X_test, y_train, y_test
    
    def train_linear_regression(self):
        """
        Train Linear Regression model.
        
        Returns:
            dict: Model performance metrics
        """
        X_train, X_test, y_train, y_test = self.train_test_split()
        
        # Scale features
        X_train_scaled = self.scaler.fit_transform(X_train)
        X_test_scaled = self.scaler.transform(X_test)
        
        model = LinearRegression()
        model.fit(X_train_scaled, y_train)
        
        y_pred = model.predict(X_test_scaled)
        
        metrics = {
            'model_name': 'Linear Regression',
            'mae': mean_absolute_error(y_test, y_pred),
            'mse': mean_squared_error(y_test, y_pred),
            'rmse': np.sqrt(mean_squared_error(y_test, y_pred)),
            'r2': r2_score(y_test, y_pred),
            'mape': np.mean(np.abs((y_test - y_pred) / y_test)) * 100
        }
        
        self.models['linear_regression'] = {
            'model': model,
            'metrics': metrics,
            'predictions': y_pred,
            'actual': y_test.values
        }
        
        return metrics
    
    def train_random_forest(self, n_estimators=100):
        """
        Train Random Forest model.
        
        Args:
            n_estimators: Number of trees in the forest
        
        Returns:
            dict: Model performance metrics
        """
        X_train, X_test, y_train, y_test = self.train_test_split()
        
        model = RandomForestRegressor(n_estimators=n_estimators, random_state=42, n_jobs=-1)
        model.fit(X_train, y_train)
        
        y_pred = model.predict(X_test)
        
        metrics = {
            'model_name': 'Random Forest',
            'mae': mean_absolute_error(y_test, y_pred),
            'mse': mean_squared_error(y_test, y_pred),
            'rmse': np.sqrt(mean_squared_error(y_test, y_pred)),
            'r2': r2_score(y_test, y_pred),
            'mape': np.mean(np.abs((y_test - y_pred) / y_test)) * 100
        }
        
        self.models['random_forest'] = {
            'model': model,
            'metrics': metrics,
            'predictions': y_pred,
            'actual': y_test.values
        }
        
        return metrics
    
    def forecast_future(self, model_name='random_forest', periods=24):
        """
        Forecast future consumption.
        
        Args:
            model_name: Name of the model to use
            periods: Number of hours to forecast
        
        Returns:
            pandas.DataFrame: Forecasted values
        """
        if model_name not in self.models:
            raise ValueError(f"Model {model_name} not found. Train it first.")
        
        model = self.models[model_name]['model']
        last_row = self.df.iloc[-1].copy()
        
        forecasts = []
        current_data = last_row.copy()
        
        for i in range(periods):
            # Prepare features for prediction
            feature_cols = [
                'hour', 'day_of_week', 'month', 'day_of_year',
                'hour_sin', 'hour_cos', 'day_sin', 'day_cos', 'month_sin', 'month_cos',
                'temperature', 'consumption_lag_1', 'consumption_lag_24', 'consumption_lag_168',
                'consumption_rolling_mean_24', 'consumption_rolling_std_24'
            ]
            feature_cols = [col for col in feature_cols if col in self.df.columns]
            
            X_pred = current_data[feature_cols].values.reshape(1, -1)
            
            # Scale if using linear regression
            if model_name == 'linear_regression':
                X_pred = self.scaler.transform(X_pred)
            
            pred = model.predict(X_pred)[0]
            forecasts.append(pred)
            
            # Update for next iteration
            current_data['consumption_lag_1'] = pred
            current_data['hour'] = (current_data['hour'] + 1) % 24
            if current_data['hour'] == 0:
                current_data['day_of_week'] = (current_data['day_of_week'] + 1) % 7
        
        # Create forecast dataframe
        last_timestamp = pd.to_datetime(self.df['timestamp'].iloc[-1])
        forecast_dates = pd.date_range(start=last_timestamp + pd.Timedelta(hours=1), periods=periods, freq='H')
        
        forecast_df = pd.DataFrame({
            'timestamp': forecast_dates,
            'forecasted_consumption': forecasts
        })
        
        return forecast_df
    
    def compare_models(self):
        """
        Compare all trained models.
        
        Returns:
            pandas.DataFrame: Comparison of model metrics
        """
        if not self.models:
            print("No models trained yet. Train models first.")
            return None
        
        comparison = []
        for model_name, model_data in self.models.items():
            comparison.append(model_data['metrics'])
        
        return pd.DataFrame(comparison)

def main():
    """
    Main function to demonstrate forecasting capabilities.
    """
    print("\n" + "=" * 60)
    print("ENERGY CONSUMPTION DATASET - FORECASTING MODELS")
    print("=" * 60)
    print("Project: Energy Consumption Dataset")
    print("Author: RSK World")
    print("Website: https://rskworld.in")
    print("=" * 60 + "\n")
    
    # Load data
    try:
        df = pd.read_csv('energy_consumption.csv')
        df['timestamp'] = pd.to_datetime(df['timestamp'])
        print(f"Loaded {len(df):,} records")
    except FileNotFoundError:
        print("Error: energy_consumption.csv not found. Please generate data first.")
        return
    
    # Initialize forecaster
    print("\nInitializing forecaster and preparing features...")
    forecaster = EnergyForecaster(df)
    
    # Train models
    print("\nTraining Linear Regression model...")
    lr_metrics = forecaster.train_linear_regression()
    print(f"  MAE: {lr_metrics['mae']:.3f} kWh")
    print(f"  RMSE: {lr_metrics['rmse']:.3f} kWh")
    print(f"  R² Score: {lr_metrics['r2']:.3f}")
    print(f"  MAPE: {lr_metrics['mape']:.2f}%")
    
    print("\nTraining Random Forest model...")
    rf_metrics = forecaster.train_random_forest(n_estimators=100)
    print(f"  MAE: {rf_metrics['mae']:.3f} kWh")
    print(f"  RMSE: {rf_metrics['rmse']:.3f} kWh")
    print(f"  R² Score: {rf_metrics['r2']:.3f}")
    print(f"  MAPE: {rf_metrics['mape']:.2f}%")
    
    # Compare models
    print("\n" + "=" * 60)
    print("MODEL COMPARISON")
    print("=" * 60)
    comparison = forecaster.compare_models()
    print(comparison.to_string(index=False))
    
    # Generate forecast
    print("\n" + "=" * 60)
    print("GENERATING 24-HOUR FORECAST")
    print("=" * 60)
    forecast = forecaster.forecast_future(model_name='random_forest', periods=24)
    print("\nNext 24 Hours Forecast:")
    print(forecast.head(10).to_string(index=False))
    print(f"\n... and {len(forecast) - 10} more hours")
    
    # Save forecast
    forecast.to_csv('forecast_24h.csv', index=False)
    print("\nForecast saved to forecast_24h.csv")
    
    print("\n" + "=" * 60)
    print("Forecasting complete!")
    print("For more information, visit: https://rskworld.in")

if __name__ == "__main__":
    main()

320 lines•11.2 KB
python
visualization.py
Raw Download
Find: Go to:
"""
Energy Consumption Dataset - Visualization Script

Project: Energy Consumption Dataset
Author: RSK World
Website: https://rskworld.in
Email: help@rskworld.in
Phone: +91 93305 39277
"""

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from datetime import datetime
import numpy as np

# Set style
sns.set_style("whitegrid")
plt.rcParams['figure.figsize'] = (12, 6)

def load_data(csv_file='energy_consumption.csv'):
    """
    Load energy consumption data from CSV file.
    
    Returns:
        pandas.DataFrame: Loaded dataset
    """
    try:
        df = pd.read_csv(csv_file)
        df['timestamp'] = pd.to_datetime(df['timestamp'])
        return df
    except FileNotFoundError:
        print(f"Error: {csv_file} not found. Please ensure the file exists.")
        return None

def plot_time_series(df, save_path='time_series_plot.png'):
    """
    Plot time series of energy consumption.
    
    Args:
        df: pandas.DataFrame containing energy consumption data
        save_path: Path to save the plot
    """
    plt.figure(figsize=(14, 6))
    
    # Plot consumption over time
    df_sorted = df.sort_values('timestamp')
    plt.plot(df_sorted['timestamp'], df_sorted['consumption_kwh'], 
             linewidth=0.5, alpha=0.7, color='#2ecc71')
    plt.title('Energy Consumption Over Time', fontsize=16, fontweight='bold')
    plt.xlabel('Date', fontsize=12)
    plt.ylabel('Consumption (kWh)', fontsize=12)
    plt.grid(True, alpha=0.3)
    plt.tight_layout()
    plt.savefig(save_path, dpi=300, bbox_inches='tight')
    print(f"Time series plot saved to {save_path}")
    plt.close()

def plot_hourly_patterns(df, save_path='hourly_patterns.png'):
    """
    Plot average consumption by hour of day.
    
    Args:
        df: pandas.DataFrame containing energy consumption data
        save_path: Path to save the plot
    """
    plt.figure(figsize=(12, 6))
    
    hourly_avg = df.groupby('hour')['consumption_kwh'].mean()
    
    plt.bar(hourly_avg.index, hourly_avg.values, color='#3498db', alpha=0.7)
    plt.title('Average Energy Consumption by Hour of Day', fontsize=16, fontweight='bold')
    plt.xlabel('Hour of Day', fontsize=12)
    plt.ylabel('Average Consumption (kWh)', fontsize=12)
    plt.xticks(range(0, 24, 2))
    plt.grid(True, alpha=0.3, axis='y')
    plt.tight_layout()
    plt.savefig(save_path, dpi=300, bbox_inches='tight')
    print(f"Hourly patterns plot saved to {save_path}")
    plt.close()

def plot_seasonal_patterns(df, save_path='seasonal_patterns.png'):
    """
    Plot seasonal consumption patterns.
    
    Args:
        df: pandas.DataFrame containing energy consumption data
        save_path: Path to save the plot
    """
    plt.figure(figsize=(12, 6))
    
    df['month'] = df['timestamp'].dt.month
    monthly_avg = df.groupby('month')['consumption_kwh'].mean()
    
    months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 
              'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    
    plt.plot(monthly_avg.index, monthly_avg.values, 
             marker='o', linewidth=2, markersize=8, color='#e74c3c')
    plt.title('Average Energy Consumption by Month', fontsize=16, fontweight='bold')
    plt.xlabel('Month', fontsize=12)
    plt.ylabel('Average Consumption (kWh)', fontsize=12)
    plt.xticks(monthly_avg.index, [months[i-1] for i in monthly_avg.index])
    plt.grid(True, alpha=0.3)
    plt.tight_layout()
    plt.savefig(save_path, dpi=300, bbox_inches='tight')
    print(f"Seasonal patterns plot saved to {save_path}")
    plt.close()

def plot_household_comparison(df, save_path='household_comparison.png'):
    """
    Plot consumption comparison across households.
    
    Args:
        df: pandas.DataFrame containing energy consumption data
        save_path: Path to save the plot
    """
    plt.figure(figsize=(12, 6))
    
    household_avg = df.groupby('household_id')['consumption_kwh'].mean().sort_values()
    
    colors = plt.cm.viridis(np.linspace(0, 1, len(household_avg)))
    plt.barh(range(len(household_avg)), household_avg.values, color=colors)
    plt.yticks(range(len(household_avg)), household_avg.index)
    plt.title('Average Energy Consumption by Household', fontsize=16, fontweight='bold')
    plt.xlabel('Average Consumption (kWh)', fontsize=12)
    plt.ylabel('Household ID', fontsize=12)
    plt.grid(True, alpha=0.3, axis='x')
    plt.tight_layout()
    plt.savefig(save_path, dpi=300, bbox_inches='tight')
    print(f"Household comparison plot saved to {save_path}")
    plt.close()

def plot_heatmap(df, save_path='consumption_heatmap.png'):
    """
    Create heatmap of consumption by hour and day of week.
    
    Args:
        df: pandas.DataFrame containing energy consumption data
        save_path: Path to save the plot
    """
    plt.figure(figsize=(14, 8))
    
    # Create pivot table
    pivot_data = df.pivot_table(
        values='consumption_kwh', 
        index='day_of_week', 
        columns='hour', 
        aggfunc='mean'
    )
    
    # Map day numbers to names
    day_names = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
    pivot_data.index = [day_names[i] for i in pivot_data.index]
    
    sns.heatmap(pivot_data, cmap='YlOrRd', annot=False, fmt='.1f', 
               cbar_kws={'label': 'Consumption (kWh)'})
    plt.title('Energy Consumption Heatmap: Day of Week vs Hour', 
              fontsize=16, fontweight='bold')
    plt.xlabel('Hour of Day', fontsize=12)
    plt.ylabel('Day of Week', fontsize=12)
    plt.tight_layout()
    plt.savefig(save_path, dpi=300, bbox_inches='tight')
    print(f"Heatmap saved to {save_path}")
    plt.close()

def main():
    """
    Main function to generate all visualizations.
    """
    print("\n" + "=" * 60)
    print("ENERGY CONSUMPTION DATASET - VISUALIZATION")
    print("=" * 60)
    print("Project: Energy Consumption Dataset")
    print("Author: RSK World")
    print("Website: https://rskworld.in")
    print("=" * 60 + "\n")
    
    # Load data
    df = load_data()
    if df is None:
        return
    
    # Generate visualizations
    print("Generating visualizations...\n")
    plot_time_series(df)
    plot_hourly_patterns(df)
    plot_seasonal_patterns(df)
    plot_household_comparison(df)
    plot_heatmap(df)
    
    print("\nAll visualizations generated successfully!")
    print("For more information, visit: https://rskworld.in")

if __name__ == "__main__":
    main()

198 lines•6.5 KB
python

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer