2

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

Community
  • 1
  • 1
user961627
  • 12,379
  • 42
  • 136
  • 210
  • Can you please provide some surrounding code to ensure the problem is not caused by something else. Also, how are you importing numpy? – sshashank124 Jun 28 '14 at 14:51
  • I've edited the question and added the details. Now the line causing the error is towards the end of the chunk of code. I'm importing numpy as np. – user961627 Jun 28 '14 at 14:57
  • This may seem really silly but I had no problem getting the right value so I think you just delete and rewrite that line and make sure your indentations are correct. – sshashank124 Jun 28 '14 at 15:02
  • 1
    How about trying to create a minimal, running coding example recreating that error? Try to remove lines and the levels above it to give the most minimal running version that's still producing the code (chances are you'll find the problem yourself). – Korem Jun 28 '14 at 15:03
  • 1
    This shouldn't make any difference as such, but just to be on the safe side try using `dot1 = np.asarray(dot1)` before using `np.exp` – Adarsh Chavakula Jun 28 '14 at 15:16
  • @AdarshChavakula just tried that, no difference – user961627 Jun 28 '14 at 15:21
  • 1
    Couldn't reproduce this. An [SSCCE](http://sscce.org/) would help – Dan Oberlam Jun 28 '14 at 15:26
  • 3
    @user961627 Are you 100% sure you didn't redefine `np` somewhere not included in the code sample you provided? – dano Jun 28 '14 at 15:37
  • 3
    Add `print np` before the line where the error occurs. What does it show? – Warren Weckesser Jun 28 '14 at 15:42
  • 1
    Probably too late, but inserting dot1 = dot1.astype(float) before calling np.exp() might help. – ojy Jan 08 '15 at 20:26
  • did you ever solve this? I have the same issue, in python 2.7. I'm trying to do it on an array. `print np` shows I've not redefined it, I tried `np.asarray`, also doesn't help. Works on command line like for you, but not in script. Gaaah python. – crobar Sep 20 '18 at 08:31

0 Answers0