Machine Learning courses with 110+ Real-time projects Start Now!!
Program 1
We want to predict a person’s electricity bill based on how many units they consumed per month and few additional features like: Units Consumed , No. of People in House , Use of AC (Yes/No), Use of Electric Geyser (Yes/No), Use of Solar Panel (Yes/No)
import numpy as np # For data handling.
import pandas as pd # # For data handling.
from sklearn.model_selection import train_test_split # Split data into train and test.
from sklearn.preprocessing import MinMaxScaler # Normalize the data between 0 and 1.
from tensorflow.keras.models import Sequential # Used to build the Neural Network using Keras.
from tensorflow.keras.layers import Dense # Used to build the Neural Network using Keras.
import matplotlib.pyplot as plt # To visualize the training process (loss over epochs).
# Sample Dataset
data = {
'Units_Consumed': [150, 300, 450, 600, 750, 900, 1050, 1200, 1350, 1500],
'People': [2, 3, 4, 5, 2, 3, 4, 5, 2, 3],
'AC_Used': [1, 1, 1, 0, 1, 1, 0, 0, 1, 1],
'Geyser_Used': [1, 1, 0, 0, 1, 0, 0, 1, 1, 0],
'Solar_Panel': [0, 1, 0, 1, 0, 0, 1, 0, 1, 0],
'Bill_Amount': [500, 1200, 1800, 2200, 3000, 3500, 4000, 4500, 5000, 5500]
}
df = pd.DataFrame(data)
df.head()
df.shape
# Features and Target
X = df.drop('Bill_Amount', axis=1) # Independed variables Input values
y = df['Bill_Amount'] # Depended variabls (Output)
X.shape
y.shape
# Normalize data
# Normalize Features
# Neural networks work better if data is between 0 and 1.
# Prevents big numbers from dominating small numbers during training.
scaler = MinMaxScaler()
X_scaled = scaler.fit_transform(X)
X_scaled
# Split data
# 80% data for training, 20% data for testing.
# random_state=42 ensures reproducibility.
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=42)
# Build ANN Model
model = Sequential() # Define ANN Model
model.add(Dense(10, activation='relu', input_shape=(X.shape[1],))) # Input + First Hidden Layer
#To allow the model to learn more complex patterns.
#Deep neural networks often perform better with multiple hidden layers.
#First layer may capture basic relationships, second layer may capture higher-order interactions.
model.add(Dense(5, activation='relu')) # Second Hidden Layer
model.add(Dense(1)) # Output layer
# Compile model
model.compile(optimizer='adam', loss='mean_squared_error')
# Train Model
history = model.fit(X_train, y_train, validation_split=0.2, epochs=200, verbose=0)
# Evaluate on test data
loss = model.evaluate(X_test, y_test)
print("Test Loss (MSE):", loss)
# Predict example: 1000 units, 3 people, AC used, geyser used, solar panel not used
new_input = np.array([[1000, 3, 1, 1, 0]])
new_input_scaled = scaler.transform(new_input)
predicted_bill = model.predict(new_input_scaled)
print(f"Predicted Bill Amount: {predicted_bill[0][0]:.2f}")