forked from udacity/AIND-Recognizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_model_selectors_org_working.py
142 lines (119 loc) · 5.32 KB
/
my_model_selectors_org_working.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import math
import statistics
import warnings
import numpy as np
from hmmlearn.hmm import GaussianHMM
from sklearn.model_selection import KFold
from asl_utils import combine_sequences
class ModelSelector(object):
'''
base class for model selection (strategy design pattern)
'''
def __init__(self, all_word_sequences: dict, all_word_Xlengths: dict, this_word: str,
n_constant=3,
min_n_components=2, max_n_components=10,
random_state=14, verbose=False):
self.words = all_word_sequences
self.hwords = all_word_Xlengths
self.sequences = all_word_sequences[this_word]
self.X, self.lengths = all_word_Xlengths[this_word]
self.this_word = this_word
self.n_constant = n_constant
self.min_n_components = min_n_components
self.max_n_components = max_n_components
self.random_state = random_state
self.verbose = verbose
def select(self):
raise NotImplementedError
def base_model(self, num_states):
# with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)
# warnings.filterwarnings("ignore", category=RuntimeWarning)
try:
hmm_model = GaussianHMM(n_components=num_states, covariance_type="diag", n_iter=1000,
random_state=self.random_state, verbose=False).fit(self.X, self.lengths)
if self.verbose:
print("model created for {} with {} states".format(self.this_word, num_states))
return hmm_model
except:
if self.verbose:
print("failure on {} with {} states".format(self.this_word, num_states))
return None
class SelectorConstant(ModelSelector):
""" select the model with value self.n_constant
"""
def select(self):
""" select based on n_constant value
:return: GaussianHMM object
"""
best_num_components = self.n_constant
return self.base_model(best_num_components)
class SelectorBIC(ModelSelector):
""" select the model with the lowest Bayesian Information Criterion(BIC) score
http://www2.imm.dtu.dk/courses/02433/doc/ch6_slides.pdf
Bayesian information criteria: BIC = -2 * logL + p * logN
"""
def select(self):
warnings.filterwarnings("ignore", category=DeprecationWarning)
lbicModel = (None, float("inf"))
"""
logL: log of the maximized value of the likelihood function for the estimated model
logN: log of the number of data points in x, the number of observations, or equivalently, the sample size
p: the number of free parameters to be estimated. If the estimated model is a linear regression, p is the number of
regressors, including the intercept
"""
try:
for n in range(self.min_n_components, self.max_n_components + 1):
model = self.base_model(n)
logL = model.score(self.X, self.lengths)
p = n * (n-1) + (n-1) + 2 * self.X.shape[1] * n
bic = (-2 * logL) + (p * np.log(self.X.shape[0])
lbicModel = (model, bic) if bic < lbicModel[1] else lbicModel
except:
pass
return lbicModel[0]
class SelectorDIC(ModelSelector):
def select(self):
warnings.filterwarnings("ignore", category=DeprecationWarning)
# TODO implement model selection based on DIC scores
min_val = float("-inf")
best_model = None
for n in range(self.min_n_components, self.max_n_components+1):
try:
model = self.base_model(n)
logL = model.score(self.X, self.lengths)
total_other_logL = 0
for word in self.words:
other_x, other_lengths = self.hwords[word]
total_other_logL += model.score(other_x, other_lengths)
avg_logL = total_other_logL/(len(self.words)-1)
dic_score = logL - avg_logL
if dic_score > min_val:
min_val = dic_score
best_model = model
except:
continue
return best_model
class SelectorCV(ModelSelector):
''' select best model based on average log Likelihood of cross-validation folds'''
def select(self):
warnings.filterwarnings("ignore", category=DeprecationWarning)
mean_scores = []
# Save reference to 'KFold' in variable as shown in notebook
split_method = KFold()
try:
for n_component in self.n_components:
model = self.base_model(n_component)
# Fold and calculate model mean scores
fold_scores = []
for _, test_idx in split_method.split(self.sequences):
# Get test sequences
test_X, test_length = combine_sequences(test_idx, self.sequences)
# Record each model score
fold_scores.append(model.score(test_X, test_length))
# Compute mean of all fold scores
mean_scores.append(np.mean(fold_scores))
except Exception as e:
pass
states = self.n_components[np.argmax(mean_scores)] if mean_scores else self.n_constant
return self.base_model(states)