Python-Scipy:multivariable_normal-选择输入的正确子集

Python-Scipy:multivariable_normal-选择输入的正确子集,python,numpy,scipy,Python,Numpy,Scipy,非常感谢任何能推动我找到正确解决方案的帮助 我尝试分两步进行分类: 1.)计算训练集上的mu、sigma和pi。 2.)创建一个测试例程,该例程需要 - mu, sigma, pi - an array of Feature IDs - testx and testy. 第1部分)工作。它回来了 -穆#形4,13 -西格玛#形状4,13,13 -圆周率#形状4 def fit_generative_model(x,y): k = 3 # labels 1,2,...,k d

非常感谢任何能推动我找到正确解决方案的帮助

我尝试分两步进行分类:

1.)计算训练集上的mu、sigma和pi。 2.)创建一个测试例程,该例程需要

- mu, sigma, pi
- an array of Feature IDs
- testx and testy.
第1部分)工作。它回来了 -穆#形4,13 -西格玛#形状4,13,13 -圆周率#形状4

def fit_generative_model(x,y):
    k = 3  # labels 1,2,...,k
    d = (x.shape)[1]  # number of features
    mu = np.zeros((k+1,d))
    sigma = np.zeros((k+1,d,d))
    pi = np.zeros(k+1)
    for label in range(1,k+1):
        indices = (y == label)
        mu[label] = np.mean(x[indices,:], axis=0)
        sigma[label] = np.cov(x[indices,:], rowvar=0, bias=1)
        pi[label] = float(sum(indices))/float(len(y))
    return mu, sigma, pi
第2部分)不起作用,因为我似乎无法选择mu和sigma的正确子集

def test_model(mu, sigma, pi, features, tx, ty):
    mu, sigma, pi = fit_generative_model(trainx,trainy)
    # set the variables
    k = 3 # Labels 1,2,...,k
    nt = len(testy)
    score = np.zeros((nt,k+1))
    covar = sigma
    for i in range(0,nt):
        for label in range(1,k+1):
            score[i,label] = np.log(pi[label]) + \
            multivariate_normal.logpdf(testx[i,features], mean=mu[label,:], cov=covar[label,:,:])
    predictions = np.argmax(score[:,1:4], axis=1) + 1

    errors = np.sum(predictions != testy)

return errors

当限制到指定的特性时,它应该返回生成模型在测试数据上所犯错误的数量。

试试这个。它应该会起作用


mean=mu[label,features],cov=covar[label,features,features]

这适用于选择1、2和所有特征。但当选择三个时,它给出了过高的估计。在另一个论坛上,他们说当
covar[label,fetures,fetures]
(当features=[2,4,6]),这将导致
[2,2],[4,4],[6,6]
而不是3*3矩阵


不幸的是,另一个论坛没有提供答案。

虽然作为答案发布,但它看起来确实不是答案。也许你应该问一个新问题?