【发布时间】:2014-01-02 19:38:48
【问题描述】:
我正在使用来自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中解决这个问题? 提前感谢您的帮助!
【问题讨论】:
标签: python machine-learning scikit-learn