“AttributeError: exp” while using numpy.exp() on an apparently ordinary array

旧时模样 提交于 2019-12-02 05:05:59

问题


I'm running some code where in the middle I have this line:

e2 = np.exp(dot1)

If I print out the value dot1, it is:

[[-30.248272500719885]]

But the line produces this error:

    e2 = np.exp(dot1)
AttributeError: exp

If I go to a new window and simply code this:

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

there are no problems... then what's wrong with the above code?

Edit

I've added some surrounding code to the original line:

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]

The line that's apparently causing the error is the 3rd line from the bottom of this chunk. Earlier, I used math.exp() as I've already used it a few lines above... but then I got an overflow error, here: math overflow for a not very overflowing calculation in python

来源:https://stackoverflow.com/questions/24467970/attributeerror-exp-while-using-numpy-exp-on-an-apparently-ordinary-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!