Machine Learning
Daily Patterns
Data Analysis
Predictive Modeling
Pattern Recognition

Finding daily patterns with machine learning

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Finding daily patterns means learning behavior that repeats within a 24-hour cycle, such as traffic peaks, support load, energy demand, or user activity. The right machine-learning approach depends on the goal: forecasting the next day, grouping similar daily profiles, or detecting days that break the usual rhythm.

Start by Framing the Problem Correctly

"Find daily patterns" can mean several different tasks:

  • forecasting hourly values for tomorrow
  • discovering recurring shapes across many days
  • identifying unusual days compared with the normal daily cycle

Those are different machine-learning problems. If you skip this framing step, you can easily choose the wrong model.

For example:

  • forecasting often uses supervised time-series models
  • grouping daily shapes often uses clustering
  • spotting unusual days often uses anomaly detection

Build Daily Features First

Most daily-pattern work begins with time-aware feature engineering. The raw timestamp itself is rarely enough.

Useful features include:

  • hour of day
  • day of week
  • weekend or holiday indicator
  • lagged values from previous hours or previous days
  • rolling averages

Cyclical features are especially important because hour 23 and hour 0 are close in time even though their raw numeric values are far apart.

python
1import numpy as np
2import pandas as pd
3
4df = pd.DataFrame({
5    "timestamp": pd.date_range("2024-01-01", periods=48, freq="H")
6})
7
8df["hour"] = df["timestamp"].dt.hour
9df["hour_sin"] = np.sin(2 * np.pi * df["hour"] / 24)
10df["hour_cos"] = np.cos(2 * np.pi * df["hour"] / 24)
11
12print(df.head())

These sine and cosine terms help models learn the circular nature of the day.

Forecasting Daily Behavior

If your goal is prediction, supervised learning is often the right tool. For each timestamp, use time-based features and historical measurements to predict the target.

python
1from sklearn.ensemble import RandomForestRegressor
2
3df["value"] = np.random.randint(0, 100, len(df))
4df["lag_1"] = df["value"].shift(1)
5df["lag_24"] = df["value"].shift(24)
6df = df.dropna()
7
8X = df[["hour_sin", "hour_cos", "lag_1", "lag_24"]]
9y = df["value"]
10
11model = RandomForestRegressor(random_state=42)
12model.fit(X, y)
13
14predictions = model.predict(X)
15print(predictions[:5])

This is not the only forecasting approach, but it shows the key idea: represent daily structure explicitly and let the model learn how today depends on recent history and time-of-day position.

Clustering Daily Profiles

If you want to discover recurring daily shapes, aggregate the data into one row per day and one column per hour, then cluster those daily profiles.

python
1from sklearn.cluster import KMeans
2
3hourly = pd.DataFrame({
4    "day": np.repeat(["Mon", "Tue", "Wed", "Thu"], 24),
5    "hour": list(range(24)) * 4,
6    "value": np.random.rand(96)
7})
8
9daily_matrix = hourly.pivot(index="day", columns="hour", values="value")
10
11kmeans = KMeans(n_clusters=2, random_state=42, n_init=10)
12labels = kmeans.fit_predict(daily_matrix)
13
14print(labels)

Now similar days get grouped together. This is useful when you want to discover patterns such as:

  • quiet weekends versus busy weekdays
  • seasonal store opening behavior
  • different usage modes for different customer segments

Anomaly Detection Around Daily Rhythm

Sometimes the real task is not prediction or clustering, but finding days or hours that deviate from the expected daily pattern.

A simple strategy is:

  1. build a model of normal daily behavior
  2. compare new observations to expected values
  3. flag large deviations

That can be done with forecasting residuals, Isolation Forest, autoencoders, or simpler thresholding based on historically normal hour-by-hour ranges.

The important point is that anomaly detection only works well when the daily cycle itself has already been modeled or normalized.

Data Quality Matters More Than Model Sophistication

Daily-pattern projects often fail before the modeling stage because of:

  • missing timestamps
  • inconsistent time zones
  • daylight-saving transitions
  • irregular sampling frequency

If the timestamps are wrong or the sampling interval is inconsistent, even a strong model will learn distorted patterns. Cleaning and resampling the time series is therefore not optional.

Common Pitfalls

  • Treating "find daily patterns" as one problem when forecasting, clustering, and anomaly detection are actually different tasks.
  • Feeding raw hour numbers into a model without cyclical encoding, which hides the fact that midnight and 11 PM are adjacent in the daily cycle.
  • Ignoring time-zone and daylight-saving issues, which can make daily periodicity appear noisy or broken.
  • Using random train-test splits for time-series prediction instead of chronological splits, which leaks future information.
  • Reaching for a complex model before verifying that the data is consistently sampled and aligned by day and hour.

Summary

  • Daily patterns are usually learned through forecasting, clustering, or anomaly detection, depending on the goal.
  • Strong time-based feature engineering is often more important than exotic model choice.
  • Cyclical encoding of hour-of-day is a key technique for 24-hour behavior.
  • Grouping data into daily profiles is effective when you want to cluster whole-day shapes.
  • Clean timestamps, correct time-zone handling, and time-aware evaluation are essential for reliable results.

Course illustration
Course illustration

All Rights Reserved.