在一个看似普通的数组上使用numpy.exp()时出现“AttributeError:exp”

时间:2014-06-28 14:49:02

标签: python arrays numpy attributes exponent

我正在运行一些代码,在中间我有这一行:

e2 = np.exp(dot1)

如果我打印出值dot1,则为:

[[-30.248272500719885]]

但该行会产生此错误:

    e2 = np.exp(dot1)
AttributeError: exp

如果我进入一个新窗口,只需编写代码:

print numpy.exp([[-30.248272500719885]])

没有问题......那么上面的代码有什么问题?

修改

我已将一些周围的代码添加到原始行:

import numpy as np
# LOTS OF CODE INCLUDING THE INITIALIZATION OF ARRAYS y and self.g
# AND getting into into a loop with index i 

for j in range(m):
    print "Calculating beta for class %d ..." % j
    f.write("Calculating beta for class "+str(j)+" ...\n")
    for i in range(self.no_samples):
        i_class = y[i]
        X1 = self.X[i].reshape(1, self.no_features)
        g1 = self.g[:,j].reshape(self.no_features, 1)
        gc =  self.g[:,i_class].reshape(self.no_features, 1)
        dot = 1. + np.dot(X1, g1) - np.dot(X1,gc)
        e = math.exp(dot)
        sum_e = 0.
        for j2 in range(m): # calculating sum of e's except for all j except where j=i_class
            if j2 != i_class: # g based on j2
                g2 = self.g[:,j2].reshape(self.no_features, 1)
                dot1 = 1. + np.dot(X1, g2) - np.dot(X1,gc)
                print dot1
                e2 = np.exp(dot1) ## THIS IS THE LINE THE ERROR REFERS TO
                f.write( str(e2)+"\n")
                sum_e = sum_e + e2[0][0]

显然导致错误的行是该块底部的第3行。 早些时候,我使用了math.exp(),因为我已经在上面几行使用了它......但后来我遇到了溢出错误,在这里:math overflow for a not very overflowing calculation in python

0 个答案:

没有答案