Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have a permission to ask a question, You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here
Sign InSign Up

ErrorCorner

ErrorCorner Logo ErrorCorner Logo

ErrorCorner Navigation

  • Home
  • Contact Us
  • About Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Contact Us
  • About Us
Home/ Questions/Q 615
Next
Answered
Kenil Vasani
Kenil Vasani

Kenil Vasani

  • 646 Questions
  • 567 Answers
  • 77 Best Answers
  • 26 Points
View Profile
  • 1
Kenil Vasani
Asked: December 14, 20202020-12-14T21:07:18+00:00 2020-12-14T21:07:18+00:00In: Python

Why do I get the ‘loop of ufunc does not support argument 0 of type int’ error for numpy.exp?

  • 1

I have a dataframe and I’d like to perform exponential calculation on a subset of rows in a column. I’ve tried three versions of code and two of them worked. But I don’t understand why one version gives me the error.

import numpy as np

Version 1 (working)

np.exp(test * 1.0)

Version 2 (working)

np.exp(test.to_list())

Version 3 (Error)

np.exp(test)

It shows the error below:

AttributeError                            Traceback (most recent call last)
AttributeError: 'int' object has no attribute 'exp'

The above exception was the direct cause of the following exception:

TypeError                                 Traceback (most recent call last)
<ipython-input-161-9d5afc93942c> in <module>()
----> 1 np.exp(pd_feature.loc[(pd_feature[col] > 0) & (pd_feature[col] < 700), col])

TypeError: loop of ufunc does not support argument 0 of type int which has no callable exp method

The test data is generated by:

test = pd.loc[(pd['a'] > 0) & (pd['a'] < 650), 'a']

The data in test is just:

0      600
2      600
42     600
43     600
47     600
60     600
67     600
Name: a, dtype: Int64

and its data type is:

<class 'pandas.core.series.Series'>

However, if I try to generate a dummy dataset, it works:

data = {'a':[600, 600, 600, 600, 600, 600, 600], 'b': ['a', 'a', 'a', 'a', 'a', 'a', 'a']} 

df = pd.DataFrame(data) 

np.exp(df.loc[:,'a'])

Any idea of why I see this error? Thank you very much.

exponentialnumpypython
  • 1 1 Answer
  • 22 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    1 Answer

    • Voted
    1. Kenil Vasani

      Kenil Vasani

      • 646 Questions
      • 567 Answers
      • 77 Best Answers
      • 26 Points
      View Profile
      Best Answer
      Kenil Vasani
      2020-12-14T21:02:23+00:00Added an answer on December 14, 2020 at 9:02 pm

      I just saw your post and would like to answer.

      I guess your problem occurs because some numpy functions require float type argument explicity, whereas your such use of the code as np.exp(test) puts int data into the argument.

      Solution could be:

      import numpy as np
      
      your_array = your_array.float()
      output = np.exp(your_array)
      
      # OR
      
      def exp_test(x)
        x.float()
        return np.exp(x)
      
      output = exp_test(your_array)
      
      

      Would you check if it works to you?
      I’d be glad to help.

      • 7
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    You must login to add an answer.

    Forgot Password?

    Sidebar

    Ask A Question
    • Popular
    • Kenil Vasani

      SyntaxError: invalid syntax to repo init in the AOSP code

      • 5 Answers
    • Kenil Vasani

      xlrd.biffh.XLRDError: Excel xlsx file; not supported

      • 3 Answers
    • Kenil Vasani

      Homebrew fails on MacOS Big Sur

      • 3 Answers
    • Kenil Vasani

      runtimeError: package fails to pass a sanity check for numpy ...

      • 3 Answers
    • Kenil Vasani

      ERROR: torch has an invalid wheel, .dist-info directory not found

      • 2 Answers

    Explore

    • Most Answered
    • Most Visited
    • Most Voted
    • Random

    © 2020-2021 ErrorCorner. All Rights Reserved
    by ErrorCorner.com