Introduction to Deep Learning and Neural Networks
Business Scenario
Welcome!
You are an AI/ML Engineer on the SmartCart AI team at NextCart Technologies. The catalog team currently uses a simple Logistic Regression model to classify products into three categories: Electronics, Grocery and Apparel
However, the model has reached a limit in its accuracy because the product attributes often overlap, making the categories difficult to separate using simple decision boundaries.
Your main task is to Build a Perceptron and a basic Artificial Neural Network (ANN) from scratch to classify SmartCart products and understand“Why was a neural network needed instead of a traditional ML model?”
Pre-Lab Preparation
Topic: Deep Learning and Neural Networks
1) What is Deep Learning, and how does it differ from traditional Machine Learning
2) The Perceptron
3) Artificial Neural Network (ANN) architecture
4) Activation functions
Git Pull
git pull origin branchNameDeep Learning vs. Traditional Machine Learning
Deep Learning is a subfield of Machine Learning that uses Artificial Neural Networks with multiple layers to automatically learn patterns and representations directly from data, rather than relying on manually engineered features.
Traditional ML vs. Deep Learning
Setup
1
Task 1: Data Loading & Pre-processing
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as snsLoad and Inspect the SmartCart Dataset
2
Dataset :
df = pd.read_csv('smartcart_dataset.csv')
print("Shape:", df.shape)
print(df["category"].value_counts())
df.head()Output
Exploratory Visualization
3
sns.scatterplot(data=df, x='price', y='weight_kg', hue='category')Preprocessing
4
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, LabelEncoder
FEATURES = ['price', 'weight_kg', 'rating', 'discount_pct', 'description_length']
TARGET = 'category'
X = df[FEATURES]
y_raw = df[TARGET]
le = LabelEncoder()
y = le.fit_transform(y_raw)
class_names = le.classes_
print(dict(zip(range(len(class_names)), class_names)))X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y
)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
X_train.shape, X_test.shapeOutput :
Output :
Perceptron
Perceptron — the simplest unit of a neural network.
It takes one or more numerical inputs, multiplies each by a weight, adds a bias and passes the result through an activation function to produce a binary output. It is the neural equivalent of a simple linear classifier.
Import Keras and Build the Model
1
Task 2 : Build a Perceptron
from tensorflow import keras
from keras import Sequential
from keras.layers import Dense
perceptron = Sequential()
perceptron.add(Dense(3, activation='softmax', input_dim=X_train.shape[1]))
perceptron.summary()Output
Compile and Train
2
perceptron.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
history_p = perceptron.fit(X_train, y_train, epochs=100,
validation_split=0.2, verbose=0) weights, bias = perceptron.layers[0].get_weights()
print('weights:\n', weights)
print('bias:\n', bias)Inspect the Learned Weights and Bias
3
Output
Evaluate and Visualize
4
perceptron_loss, perceptron_acc = perceptron.evaluate(X_test, y_test, verbose=0)
print(f'Perceptron test accuracy: {perceptron_acc:.3f}')Output :
plt.plot(history_p.history['loss'], label='Training Loss')
plt.plot(history_p.history['val_loss'], label='Validation Loss')
plt.title('Perceptron training loss')
plt.legend()
plt.show()Aritificial Neural Network
ANN Architecture — an Artificial Neural Network is built by arranging many Perceptron-like units (“neurons”) into layers, stacked so the output of one layer feeds into the next.
Build the Model
1
Task 3 : Build a basic ANN and Test it on the SmartCart dataset
ann = Sequential()
ann.add(Dense(32, activation='relu', input_dim=X_train.shape[1]))
ann.add(Dense(16, activation='relu'))
ann.add(Dense(3, activation='softmax'))
ann.summary()Output
Compile, Train and Evaluate
2
ann.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
history_ann = ann.fit(X_train, y_train, epochs=100,
validation_split=0.2, verbose=0)
ann_loss, ann_acc = ann.evaluate(X_test, y_test, verbose=0)
print(f'ANN test accuracy: {ann_acc:.3f}')plt.plot(history_ann.history['loss'],
label='Training Loss')
plt.plot(history_ann.history['val_loss'],
label='Validation Loss')
plt.title('ANN training loss')
plt.legend()
plt.show()Output :
Plot Training Loss
3
Task 4 : Why This Problem Needed a Neural Network
You've built a Perceptron and an ANN. Now use Logistic Regression, the traditional ML model used in the original scenario, to confirm that it also has a linear decision-boundary limitation.
Run Logistic Regression on the SmartCart Dataset
1
log_reg = LogisticRegression(max_iter=2000)
log_reg.fit(X_train_scaled, y_train)
y_pred_logreg = log_reg.predict(X_test_scaled)
logreg_acc = accuracy_score(y_test, y_pred_logreg)
print(f"Logistic Regression test accuracy: {logreg_acc:.3f}\n")
print(classification_report(y_test, y_pred_logreg, target_names=class_names))Confusion Matrix for All Three Models
2
fig, axes = plt.subplots(1, 3, figsize=(16, 5))
for ax, y_pred, title, cmap in zip(
axes,
[y_pred_perceptron, y_pred_ann, y_pred_logreg],
['Perceptron -- Confusion Matrix', 'ANN -- Confusion Matrix',
'Logistic Regression -- Confusion Matrix'],
['Blues', 'Greens', 'Oranges']):
cm = confusion_matrix(y_test, y_pred)
im = ax.imshow(cm, cmap=cmap)
ax.set_xticks(range(len(class_names))); ax.set_xticklabels(class_names)
ax.set_yticks(range(len(class_names))); ax.set_yticklabels(class_names)
ax.set_xlabel('Predicted'); ax.set_ylabel('Actual')
ax.set_title(title)
for i in range(len(class_names)):
for j in range(len(class_names)):
ax.text(j, i, cm[i, j], ha='center', va='center')
plt.tight_layout()
plt.show()Output :
Compare all 3 Models
3
comparison = pd.DataFrame({
'Model': ['Perceptron (Keras)', 'Logistic Regression (sklearn)',
'ANN (Keras)'],
'Test Accuracy': [perceptron_acc,logreg_acc ,ann_acc ]})
comparison
Great job!
You have successfully compared Deep Learning with traditional Machine Learning, built and evaluated a Perceptron and a basic ANN in Keras using SmartCart data, and explored the key building blocks of neural networks. You compared both models with a Logistic Regression baseline and investigated why this problem needed a neural network, using accuracy scores, confusion matrices, and decision-boundary plots as evidence.
Checkpoint
Git Push
git push origin branchNameNext-Lab Preparation
Topic : Backpropagation and Gradient Descent
1) Loss Function
2) Gradients, Gradient Descent
3) Backpropagation Flow
4) Epochs & Convergence