To get the most frequent pattern from a dataset, especially when dealing with spiked values, you're essentially looking to perform some form of pattern mining or frequent itemset mining (if discrete) or sequence mining (if time-based), depending on your data type.
Let’s break this down step by step and clarify what each part means:
🔍 Understanding the Problem
1. What are "spiked values"?
These are typically sudden increases in value that deviate significantly from the normal behavior.
They could be outliers, anomalies, or just high-value events in your data.
Example: In a time series of server traffic, spikes might represent bursts of activity.
2. What do you mean by "most frequent pattern"?
This could mean:
A recurring sequence of values or events
A set of attributes/values that frequently co-occur
A common shape or trend in time series during spikes
🧠 How to Approach This
Depending on your data type and format, here are several approaches:
✅ Case 1: Discrete Events / Categorical Data
If you're working with categorical features or event types, and want to find which combinations of events occur together during spikes, use:
🔹 Frequent Pattern Mining Techniques:
Apriori Algorithm
FP-Growth (Frequent Pattern Growth)
Steps:
Identify when a spike occurs (e.g., using thresholding, Z-score, IQR).
For all those spiked instances, extract the relevant feature values/events.
Treat each spike instance as a transaction (like in market basket analysis).
Apply FP-Growth or Apriori to find frequent itemsets.
from scipy.signal import find_peaks
from sklearn.cluster import KMeans
# Assume ts is your time series
peaks, _ = find_peaks(ts, height=np.mean(ts)+2*np.std(ts))
window_size = 20
patterns = [ts[i-window_size:i+window_size] for i in peaks if i > window_size]
# Cluster patterns
kmeans = KMeans(n_clusters=3)
kmeans.fit(patterns)
# Get most frequent cluster
values, counts = np.unique(kmeans.labels_, return_counts=True)
most_common_label = values[np.argmax(counts)]
# Plot the centroid of the most common pattern
plt.plot(kmeans.cluster_centers_[most_common_label])
plt.title("Most Frequent Spike Pattern")
plt.show()
✅ Case 3: Mixed Data (Time + Features)
If you have multivariate time series (e.g., CPU, RAM, Disk usage over time):
🔹 Use:
Subsequence Clustering
Autoencoders for dimensionality reduction + clustering
SAX (Symbolic Aggregate Approximation) to convert real-valued data into symbolic sequences
You can also extract statistical features (mean, variance, slope) around each spike and then cluster based on those features.
📌 Summary Table
Data Type
Goal
Method
Tool
Categorical/Events
Frequent itemsets during spikes
Apriori, FP-Growth
mlxtend, apyori
Univariate Time Series
Common shape around spikes
Clustering, Matrix Profile
stumpy, sklearn
Multivariate Time Series
Recurring multivariate patterns
Subsequence clustering, SAX, Autoencoders
tsfresh, keras, sktime
🛠️ Tips
Normalize your data if comparing across different scales.
Define clearly what constitutes a "spike" — thresholds matter.
Consider domain knowledge to guide interpretation of patterns.
Would you like help tailoring this approach to your specific dataset? If so, feel free to share:
A sample of your data structure
What defines a "spike"
Whether it's categorical, time series, or mixed data
I’ll help you write code or choose the best method!