-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTime Series Clustering Visualizer.py
More file actions
105 lines (89 loc) · 2.79 KB
/
Copy pathTime Series Clustering Visualizer.py
File metadata and controls
105 lines (89 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import tkinter as tk
from tkinter import messagebox
import numpy as np
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
# -------------------------
# Generate Sample Time Series
# -------------------------
def generate_time_series(n_series=30, length=50):
data = []
for i in range(n_series):
if i < 10:
series = np.sin(np.linspace(0, 3, length)) + np.random.normal(0, 0.2, length)
elif i < 20:
series = np.cos(np.linspace(0, 3, length)) + np.random.normal(0, 0.2, length)
else:
series = np.linspace(0, 1, length) + np.random.normal(0, 0.2, length)
data.append(series)
return np.array(data)
data = generate_time_series()
# -------------------------
# Clustering & Visualization
# -------------------------
def cluster_and_plot():
try:
k = int(cluster_entry.get())
if k <= 0:
raise ValueError
kmeans = KMeans(n_clusters=k, random_state=42)
labels = kmeans.fit_predict(data)
fig.clear()
for cluster_id in range(k):
ax = fig.add_subplot(k, 1, cluster_id + 1)
for i in range(len(data)):
if labels[i] == cluster_id:
ax.plot(data[i], alpha=0.6)
ax.set_title(f"Cluster {cluster_id + 1}")
ax.set_ylabel("Value")
ax.set_xlabel("Time")
canvas.draw()
except ValueError:
messagebox.showerror("Error", "Enter a valid number of clusters")
# -------------------------
# GUI Setup
# -------------------------
root = tk.Tk()
root.title("Time Series Clustering Visualizer")
root.geometry("850x650")
root.resizable(False, False)
title = tk.Label(
root,
text="Time Series Clustering Visualizer",
font=("Arial", 16, "bold")
)
title.pack(pady=10)
control_frame = tk.Frame(root)
control_frame.pack()
tk.Label(
control_frame,
text="Number of Clusters:",
font=("Arial", 11)
).grid(row=0, column=0, padx=10)
cluster_entry = tk.Entry(control_frame, width=10)
cluster_entry.insert(0, "3")
cluster_entry.grid(row=0, column=1)
cluster_btn = tk.Button(
control_frame,
text="Run Clustering",
font=("Arial", 11),
bg="#3498db",
fg="white",
command=cluster_and_plot
)
cluster_btn.grid(row=0, column=2, padx=10)
# -------------------------
# Plot Area
# -------------------------
fig = plt.Figure(figsize=(8, 5), dpi=100)
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.get_tk_widget().pack(pady=15)
footer = tk.Label(
root,
text="Unsupervised Learning | K-Means | Python Desktop App",
font=("Arial", 9),
fg="gray"
)
footer.pack(side="bottom", pady=5)
root.mainloop()