I try to modularize my python code .
In my main code the notebook, I insert this :
import numpy as np
import pandas as pd
from pandas import DataFrame
import my_math
df["std_line_amount_log_normal"] = df["std_line_amount"].apply(lambda f: my_math.feature_log_normalize(f))
And I create a module called "my_math" as an external .py file : here the code :
def feature_std_normalize(f):
return (f - mu) / std
def feature_log_normalize(f):
# return (f - mu) / std
return np.log(f+1)
When I run this statement in my notebook :
df["std_line_amount_log_normal"] = df["std_line_amount"].apply(lambda f: my_math.feature_log_normalize(f))
I get this error :
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
in ()
----> 1 df["std_line_amount_log_normal"] = df["std_line_amount"].apply(lambda f: my_math.feature_log_normalize(f))
~/anaconda3/envs/python3/lib/python3.6/site-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds)
2549 else:
2550 values = self.asobject
-> 2551 mapped = lib.map_infer(values, f, convert=convert_dtype)
2552
2553 if len(mapped) and isinstance(mapped[0], Series):
pandas/_libs/src/inference.pyx in pandas._libs.lib.map_infer()
in (f)
----> 1 df["std_line_amount_log_normal"] = df["std_line_amount"].apply(lambda f: my_math.feature_log_normalize(f))
~/SageMaker/my_math.py in feature_log_normalize(f)
10
11 def feature_log_normalize(f):
---> 12 # return (f - mu) / std
13 return np.log(f+1)
14
NameError: name 'np' is not defined
Ca you help me to resolve this problem please?
thank you
解决方案
Try to import numpy also in external .py so:
import numpy as np
def feature_std_normalize(f):
return (f - mu) / std
def feature_log_normalize(f):
# return (f - mu) / std
return np.log(f+1)