-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
82 lines (65 loc) · 2.56 KB
/
main.py
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
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Load the TSV datasets
mousedata = pd.read_csv('mousedata.tsv', sep='\t', parse_dates=['Time'])
mouse_mov_speeds = pd.read_csv('mouse_mov_speeds.tsv', sep='\t', parse_dates=['Time'])
# Check and remove any unnecessary columns
mousedata = mousedata[['Time', 'Event_Type', 'X', 'Y', 'Daylight']]
mouse_mov_speeds = mouse_mov_speeds[['Time', 'Speed(ms)', 'Daylight']]
# Merge datasets on timestamp
merged_data = pd.merge_asof(mousedata.sort_values('Time'), mouse_mov_speeds.sort_values('Time'), on='Time', direction='nearest')
merged_data.dropna(inplace=True)
# Convert categorical data to numerical data
if 'Event_Type' in merged_data.columns:
merged_data['Event_Type'] = merged_data['Event_Type'].astype('category').cat.codes
if 'Daylight_x' in merged_data.columns:
merged_data['Daylight_x'] = merged_data['Daylight_x'].astype('category').cat.codes
# Feature engineering
merged_data['Speed_Change'] = merged_data['Speed(ms)'].diff().fillna(0)
merged_data['Acceleration'] = merged_data['Speed_Change'].diff().fillna(0)
merged_data['Distance'] = np.sqrt(merged_data['X'].diff()**2 + merged_data['Y'].diff()**2).fillna(0)
merged_data['Click_Count'] = merged_data['Event_Type'].apply(lambda x: 1 if x == 0 else 0).cumsum()
# Select features for clustering
features = [ 'X', 'Y', 'Speed(ms)']
X = merged_data[features]
# Standardize the features
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# Apply KMeans clustering
kmeans = KMeans(n_clusters=4, random_state=42)
kmeans.fit(X_scaled)
clusters = kmeans.predict(X_scaled)
# Add cluster labels to the data
merged_data['Cluster'] = clusters
# Print the first few rows of the data with cluster labels
print(merged_data.head())
# Create a 3D scatter plot using matplotlib
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Assign colors to clusters
colors = ['r', 'g', 'b', 'y']
# Plot each cluster
for cluster in range(4):
cluster_data = merged_data[merged_data['Cluster'] == cluster]
ax.scatter(
cluster_data['X'],
cluster_data['Y'],
cluster_data['Speed(ms)'],
c=colors[cluster],
label=f'Cluster {cluster}'
)
# Set labels
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Speed(ms)')
plt.title('3D Scatter Plot of Clusters')
plt.legend()
# Show plot
import joblib
# After training your model
joblib.dump(kmeans, 'kmeans_model.joblib')
joblib.dump(scaler, 'scaler.joblib')