Python error support note: "... does not support argument 0 of type float ..."

I'm new to Python / machine learning. Leave a note of error handling for the data type.

The situation that occurred

An error occurred when performing the following calculation in a python program.

import numpy as np

def sigmoid(x)
  return (1 / (1 + np.exp(-x)))

hoge = sigmoid(3) #An error occurs here

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

Found cause

--It seems that an error will occur if the argument assigned to the calculation of the numpy library contains int type data (see GitHub URL in the reference column). --It is good to change the data type to float type before the calculation process to the argument.

solution

--Sigmoid (x) solved by explicitly stating that the data type of x is float.

def sigmoid(x)
  x = x.float()
  return (1 / (1 + np.exp(-x)))

hoge = sigmoid(3) # ->I was able to calculate correctly without any error

reference

Supplement

--When creating deep learning, this error occurred at the timing of using the sigmoid function. An array is entered in the argument x, but it seems that this problem occurred because each element of the array was an int type. ――I realized again that one of the characteristics of Python is that it works to some extent without declaring the data type, and I thought that it is better to keep in mind the calculation that specifies the data type.

(that's all)

Recommended Posts

Python error support note: "... does not support argument 0 of type float ..."
[Python] Type Error: Summary of error causes and remedies for'None Type'
[Python] Reason why index error does not occur in slice
Python --Check type of values
[PyTorch] Runtime Error: Expected object of scalar type Float but got scalar type Double for argument # 4'mat1'
Python version does not switch
[Python] Tensorflow 2.0 did not support Python 3.8, so the story of downgrading Python
Expansion by argument of python dictionary
Processing when the key input of Python pygame does not go well.
Do not specify a mutable object (list type, dictionary type, etc.) as the initial value of the function argument of python.
python note: when easy_install is not available
[Python] Name Error: name'urlparse' is not defined
[VScode] autopep8 format does not work [Python]
[Python] Understand the content of error messages
[python] [meta] Is the type of python a type?
Python> Python does not include the last offset
(Note) Bulk upgrade of python installed packages
Python Note: The secret role of commas
Python http.server did not support Range Request
About the problem that the python version of Google App Engine does not mesh
What to do if you get the error "Error: opencv3: Does not support building both Python 2 and 3 wrappers" when installing openCV 3