Spaces:
Sleeping
Sleeping
new file: dataset.py
Browse files- dataset.py +64 -0
- features.py +18 -0
dataset.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
TIMESTEPS = 120
|
| 4 |
+
|
| 5 |
+
def generate_normal():
|
| 6 |
+
acc = np.random.normal(9.8, 2, (TIMESTEPS, 3))
|
| 7 |
+
gyro = np.random.normal(0, 0.5, (TIMESTEPS, 3))
|
| 8 |
+
speed = np.linspace(30, 50, TIMESTEPS)
|
| 9 |
+
return np.hstack([acc, gyro, speed.reshape(-1,1)]), 0
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def generate_brake():
|
| 13 |
+
acc = np.random.normal(9.8, 2, (TIMESTEPS, 3))
|
| 14 |
+
acc[-20:] += np.random.normal(-10, 3, (20, 3))
|
| 15 |
+
gyro = np.random.normal(0, 1, (TIMESTEPS, 3))
|
| 16 |
+
speed = np.linspace(50, 5, TIMESTEPS)
|
| 17 |
+
return np.hstack([acc, gyro, speed.reshape(-1,1)]), 0
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def generate_crash():
|
| 21 |
+
acc = np.random.normal(9.8, 2, (TIMESTEPS, 3))
|
| 22 |
+
acc[80:90] += np.random.normal(40, 12, (10, 3))
|
| 23 |
+
|
| 24 |
+
gyro = np.random.normal(0, 0.5, (TIMESTEPS, 3))
|
| 25 |
+
gyro[80:90] += np.random.normal(25, 10, (10, 3))
|
| 26 |
+
|
| 27 |
+
speed = np.linspace(60, 0, TIMESTEPS)
|
| 28 |
+
speed[90:] = 0
|
| 29 |
+
|
| 30 |
+
return np.hstack([acc, gyro, speed.reshape(-1,1)]), 1
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def generate_phone_drop():
|
| 34 |
+
acc = np.random.normal(9.8, 2, (TIMESTEPS, 3))
|
| 35 |
+
acc[60:70] += np.random.normal(25, 5, (10, 3))
|
| 36 |
+
|
| 37 |
+
gyro = np.random.normal(0, 2, (TIMESTEPS, 3))
|
| 38 |
+
speed = np.zeros(TIMESTEPS)
|
| 39 |
+
|
| 40 |
+
return np.hstack([acc, gyro, speed.reshape(-1,1)]), 0
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def create_dataset(n=5000):
|
| 44 |
+
X, y = [], []
|
| 45 |
+
|
| 46 |
+
for _ in range(n):
|
| 47 |
+
choice = np.random.choice(
|
| 48 |
+
["normal", "brake", "crash", "phone"],
|
| 49 |
+
p=[0.25, 0.25, 0.3, 0.2]
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
if choice == "normal":
|
| 53 |
+
d, l = generate_normal()
|
| 54 |
+
elif choice == "brake":
|
| 55 |
+
d, l = generate_brake()
|
| 56 |
+
elif choice == "crash":
|
| 57 |
+
d, l = generate_crash()
|
| 58 |
+
else:
|
| 59 |
+
d, l = generate_phone_drop()
|
| 60 |
+
|
| 61 |
+
X.append(d)
|
| 62 |
+
y.append(l)
|
| 63 |
+
|
| 64 |
+
return np.array(X), np.array(y)
|
features.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
def extract_features(sample):
|
| 4 |
+
acc = np.sqrt(sample[:,0]**2 + sample[:,1]**2 + sample[:,2]**2)
|
| 5 |
+
|
| 6 |
+
return [
|
| 7 |
+
np.mean(acc),
|
| 8 |
+
np.max(acc),
|
| 9 |
+
np.std(acc),
|
| 10 |
+
|
| 11 |
+
np.max(np.abs(sample[:,3:6])),
|
| 12 |
+
|
| 13 |
+
np.mean(sample[:,6]),
|
| 14 |
+
np.min(sample[:,6]),
|
| 15 |
+
np.max(sample[:,6]),
|
| 16 |
+
|
| 17 |
+
np.mean(acc[-10:])
|
| 18 |
+
]
|