← Back to projects View source code

Smart City Pattern & Anomaly Detection

Clustering traffic patterns and detecting unusual energy-grid behavior with compact machine learning workflows.

This project applies K-means clustering and Gaussian anomaly detection to smart city management data, showing how transportation and energy activity can be grouped, scored, and reviewed for unusual operational patterns.

K-means • Gaussian Anomaly Detection • Smart Cities • NumPy • Pandas • Matplotlib

K-means Traffic pattern clustering method
3 Traffic clusters identified
Gaussian Probability-based anomaly model
3 Energy anomalies detected

Project Overview

Smart city systems generate operational data from transportation networks, energy usage, sensors, and infrastructure. This project demonstrates how machine learning can organize traffic behavior into meaningful spatial groups and flag low-probability energy-grid observations for further investigation.

Traffic Clustered location-level movement patterns to identify spatial activity groups.
Energy Modeled normal energy behavior and flagged unusual observations using probability scores.
Proof of Concept Built as a compact proof-of-concept for smart city monitoring, pattern discovery, and anomaly review.

Two-Part Machine Learning Workflow

The project combines two machine learning workflows: clustering traffic patterns and detecting abnormal energy usage. The goal is not to create a production monitoring system, but to demonstrate the core modeling logic behind smart city pattern discovery, anomaly scoring, and threshold-based detection.

01

Traffic Pattern Clustering

K-means is implemented from scratch by initializing centroids, assigning points to the nearest centroid, recomputing cluster centers, and visualizing traffic groupings.

02

Energy Grid Anomaly Detection

A Gaussian probability model estimates normal energy behavior, computes probability scores for each observation, selects an F1-optimized threshold, and flags low-probability observations as anomalies.

01 Load data Traffic and energy records are prepared for clustering, probability modeling and anomaly review.
02 Model patterns K-means groups traffic points, while Gaussian parameters describe normal energy behavior.
03 Evaluate signals Cluster validation and probability thresholds support interpretation of unusual observations.

Traffic Pattern Clustering

The clustering workflow uses K-means to group traffic observations into spatial activity patterns. The resulting clusters show how traffic points separate into distinct zones, while the elbow analysis helps justify the selected number of clusters.

K-means clustering of traffic data
Traffic clusters. K-means grouped the traffic observations into three spatial activity zones, with centroids marking the center of each detected pattern.
K-Means elbow curve for traffic clustering
Elbow analysis. The within-cluster sum of squares drops sharply up to K = 3 and then begins to flatten, supporting three clusters as a practical choice.

Interpretation

The clustering results show three interpretable traffic activity zones: a lower-activity region in the lower-left area, a middle spatial region, and a higher-location region in the upper-right area. The elbow curve supports K = 3 because most of the compactness improvement occurs by the third cluster, with smaller gains after that point. Although additional clusters may create finer segmentation, three clusters provide a clearer and more practical summary of the traffic patterns in this sample.

Energy Grid Anomaly Detection

The anomaly detection workflow models normal energy-grid behavior using Gaussian probability scores. Observations with unusually low probability are flagged as anomalies, making them candidates for operational review, sensor validation, or equipment monitoring.

Threshold performance curve for energy anomaly detection
Threshold tuning. Precision, recall, and F1-score were evaluated across possible anomaly thresholds to select an epsilon value that balances detection strength with false-positive control.

Interpretation

The model identified three unusual energy observations while preserving strong separation from normal activity. The threshold tuning curve shows how different epsilon values affect precision, recall, and F1-score, helping justify the final anomaly cutoff rather than selecting it arbitrarily. In a smart city setting, these flagged observations could represent abnormal demand, sensor issues, equipment faults, or events that require further monitoring before operational action is taken.

Role and Implementation

I implemented the clustering and anomaly detection workflow in Python, including the K-means helper functions, Gaussian parameter estimation, threshold selection, visual diagnostics, and project documentation.

Algorithm Implementation

Built the K-means workflow from scratch, including centroid assignment, centroid updates, iteration control, and cluster interpretation.

Anomaly Detection Logic

Estimated Gaussian parameters, computed probability scores, selected a threshold using F1-score, and flagged low-probability observations.

Visualization and Communication

Created diagnostic visuals that explain model behavior, cluster selection, anomaly detection results, and threshold performance.

Tools

Python • NumPy • Pandas • Matplotlib • Jupyter Notebook • GitHub

Artifacts

The project artifacts include the notebook, source code, data workflow, visual outputs, and documentation used to explain the modeling approach.

GitHub Repository

Source repository containing the project notebook, data folder structure, README, and generated visuals.

Open repository →

Notebook

Main Jupyter Notebook implementing clustering, Gaussian anomaly detection, threshold tuning, and visualization.

notebooks/smart_city_anomaly_detection.ipynb

Visual Outputs

Diagnostic plots for K-Means validation, threshold performance, and detected energy-grid anomalies.

images/projects/smart-city/