Python: key error

I am using scikit-learn to teach GMM and am trying to change the number of components in the mix. I ran into problems that were fixed here .

I ended up with this code:

from sklearn.mixture import GMM

class_names = ['name1','name2','name3']
covs =  ['spherical', 'diagonal', 'tied', 'full']
num_comp = [1,2,3]
models = dict()

for c in class_names:
    for cov in covs:
        models[c,cov] = values = []
        for num in num_comp:
            values.append(GMM(n_components=num,covariance_type=cov, init_params='wmc',n_init=1, n_iter=10))
print models

I use the dict () model later, and I have problems because I don’t know how to work with dict.

training_data={'name1':'path2data1', 'name2': path2data2, 'name3': path2data3}

for cov in covs:
    for c in class_names:
        for num in num_comp:
            models[c][cov].fit(training_data[c])

I get the error "KeyError: 'name1'"

I tried the models [c, cov] .fit (training_data [c]), but then I get the error message "AttributeError:" list "does not have the attribute" fit ""

Thanks in advance for any advice!

+4
source share
1 answer

dict (c, cov (c, cov)):

models[c,cov] = values = []

, dict :

for cov in covs:
    for c in class_names:
        for num in num_comp:
            models[(c, cov)].fit(training_data[c])

AttributeError, : models[(c,cov)] - , (values)

, :

for c in class_names:
    models[c] = {}
    for cov in covs:
        models[c][cov] = values = []
        for num in num_comp:
            values.append(GMM(n_components=num,covariance_type=cov, init_params='wmc',n_init=1, n_iter=10))

list models[c][cov] :

for cov in covs:
    for c in class_names:
        for num in num_comp:
            for f in models[c][cov]
                f.fit(training_data[c])
+3

All Articles