-
Notifications
You must be signed in to change notification settings - Fork 1
/
run_shdp_sampler.py
58 lines (52 loc) · 1.69 KB
/
run_shdp_sampler.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
import numpy as np
from numpy.random import choice, dirichlet, binomial, multinomial, beta, gamma, normal,exponential#, gaussian
from scipy.cluster.vq import kmeans2
import matplotlib.pyplot as plt
import csv
from shdp import StickyHDPHMM
import seaborn as sns
def read_data(file_path):
# The read-in data should be a N*W matrix,
# where N is the length of the time sequences,
# W is the number of sensors/data features
i = 0
with open(file_path, 'r') as file:
reader = csv.reader(file, delimiter = ',')
for line in reader:
line = np.array(line, dtype = 'float') # str2float
if i == 0:
data = line
else:
data = np.vstack((data, line))
i += 1
return data
if __name__ == "__main__":
data_path = "obs_data_16d.csv"
data_path = "four_scores.csv"
data = read_data(data_path)
# data = data[15500:16500,0:2]
data = data[7500:8500,0:3]#data[15500:16500,0:1]
n,d = data.shape
# normalize
for i in range(d):
if np.std(data[:,i]) != 0:
data[:,i] = (data[:,i]-np.mean(data[:,i]))/np.std(data[:,i])
else:
data[:,i]
DP = StickyHDPHMM(data, kappa=10, L=10,
kmeans_init=False)
i = 0
while i < 1000:
print("iter: ", i)
DP.sampler()
i+=1
plt.figure(1)
sns.heatmap(DP.state[:, 0:1].T, cbar=False)
plt.figure(2)
for i in range(DP.n):
path = DP.getPath(i)
plt.plot(range(DP.T), path)
plt.show()
all_states, state_counts = np.unique(DP.state, return_counts=True)
#print("all states: ", all_states)
#print("state counts: ", state_counts)