-
Notifications
You must be signed in to change notification settings - Fork 0
/
GMM_training.py
52 lines (39 loc) · 1.76 KB
/
GMM_training.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
import pandas as pd
import numpy as np
from sklearn.mixture import GaussianMixture
class GMMTraining():
def __init__(self, values):
self.values = np.array([[val] for val in values])
self.x_values = np.linspace(0, 1, 1000)
'''
p_values = np.array([ [val] for val in df_pD_values.p.values])
D_values = np.array([ [val] for val in df_pD_values.D.values])
N = np.arange(1, 11)
models = [None for i in range(len(N))]
for i in range(len(N)):
models[i] = GaussianMixture(N[i]).fit(p_values)
AIC = [m.aic(p_values) for m in models]
BIC = [m.bic(p_values) for m in models]
best_GMM = models[np.argmin(AIC)]
logprob = best_GMM.score_samples(x_values.reshape(-1, 1))
responsibilities = best_GMM.predict_proba(x_values.reshape(-1, 1))
pdf = np.exp(logprob)
pdf_individual = responsibilities * pdf[:, np.newaxis]
'''
N = np.arange(1, 11)
self.models = [None for i in range(len(N))]
for i in range(len(N)):
self.models[i] = GaussianMixture(N[i]).fit(self.values)
self.AIC = [m.aic(self.values) for m in self.models]
self.BIC = [m.bic(self.values) for m in self.models]
self.best_GMM = self.models[np.argmin(self.AIC)]
self.logprob = self.best_GMM.score_samples(self.x_values.reshape(-1, 1))
self.responsibilities = self.best_GMM.predict_proba(self.x_values.reshape(-1, 1))
self.pdf = np.exp(self.logprob)
self.pdf_individual = self.responsibilities * self.pdf[:, np.newaxis]
def get_pdf(self):
return self.pdf
def get_individual_pdf(self):
return self.pdf_individual
def get_x_values(self):
return self.x_values