Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python sklearn GMM分类器的分类输出错误_Python_Machine Learning_Scikit Learn - Fatal编程技术网

Python sklearn GMM分类器的分类输出错误

Python sklearn GMM分类器的分类输出错误,python,machine-learning,scikit-learn,Python,Machine Learning,Scikit Learn,我正在使用sklearn中的GMM工具包构建一个基本的说话人识别器。我有3个类,每个类都有一个分类器。在测试阶段,应选择概率最高的说话人的GMM,程序应返回每个测试样本的预测等级。我想改变混合物成分的数量,并在此示例代码中设置n_components=4。 如果我使用4个混合成分,分类器的输出将是0、1、2或3。如果我使用3种混合物成分,它将是0、1或2。我感觉分类器返回预测的混合成分,而不是整个GMM。但我希望它能预测类:1,2或3 这是我的密码: import numpy as np fro

我正在使用
sklearn
中的
GMM
工具包构建一个基本的说话人识别器。我有3个类,每个类都有一个分类器。在测试阶段,应选择概率最高的说话人的
GMM
,程序应返回每个测试样本的预测等级。我想改变混合物成分的数量,并在此示例代码中设置
n_components=4
。 如果我使用4个混合成分,分类器的输出将是0、1、2或3。如果我使用3种混合物成分,它将是0、1或2。我感觉分类器返回预测的混合成分,而不是整个GMM。但我希望它能预测类:1,2或3

这是我的密码:

import numpy as np
from sklearn.mixture import GMM

#set path
path="path"

class_names = [1,2,3]

covs =  ['spherical', 'diag', 'tied', 'full']

training_data = {1: np.loadtxt(path+"/01_train_debug.data"), 2:  np.loadtxt(path+"/02_train_debug.data"), 3: np.loadtxt(path+"/03_train_debug.data")}

print "Training models"
models = {}
for c in class_names:
    # make a GMM for each of the classes in class_names
    models[c] = dict((covar_type,GMM(n_components=4,
                    covariance_type=covar_type, init_params='wmc',n_init=1, n_iter=20))
                   for covar_type in covs)


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

#define test set
test01 = np.loadtxt(path+"/01_test_debug.data")
test02 = np.loadtxt(path+"/02_test_debug.data")
test03 = np.loadtxt(path+"/03_test_debug.data")

testing_data = {1: test01, 2: test02, 3: test03}

probs = {}

print "Calculating Probabilities"

for c in class_names:
    probs[c] = {}
    for cov in covs:
        probs[c][cov] = {}
        for p in class_names:
            probs[c][cov] = models[p][cov].predict(testing_data[c])


for c in class_names:
    print c
    for cov in covs:
        print "   ",cov,
        for p in class_names:
            print p, probs,
        print 
我上面的假设是正确的还是代码中有逻辑错误? 在sklearn中有解决这个问题的方法吗? 提前感谢您的帮助

在您的代码中,第一次输入的
模型的键是协方差类型,第二次输入的键是类名。对不起,我误读了你的密码

编辑:如果您需要拟合的GMM模型下数据的每个样本的可能性,则应使用
score\u samples
方法。
predict
方法不返回概率,而是返回组件分配


此外,默认情况下,GMM是非监督模型。如果您想从一堆GMM模型中构建一个有监督的模型,您可能应该将其包装为一个estimator类来包装它们,并实现fit/predict API,以便能够通过交叉验证来估计其准确性,并通过网格搜索来调整超参数值。正在实施类似的东西。随着时间的推移,它可能会被纳入下一个scikit学习版(0.15版,应该在2014年初发布)。

谢谢!我最初使用的是score_samples方法,但不确定如何根据结果计算混淆矩阵。将切换回并尝试一下!您必须参加与每个样本得分最高的模型相匹配的课程。@ogrisel您好,我想知道您是否可以提前在我的感谢中查看类似的问题。