模块化代码python时未知的librarie

时间:2019-03-13 17:00:40

标签: python pandas

我尝试模块化我的python代码。

在笔记本的主要代码中,插入以下内容:

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))

然后我创建一个名为“ my_math”的模块作为外部.py文件:此处为代码:

def feature_std_normalize(f):
    return (f - mu) / std

def feature_log_normalize(f):
   # return (f - mu) / std
    return np.log(f+1)

当我在笔记本中运行以下语句时:

df["std_line_amount_log_normal"] = df["std_line_amount"].apply(lambda f: my_math.feature_log_normalize(f))

我收到此错误:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-90-32256b03bfa2> in <module>()
----> 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()

<ipython-input-90-32256b03bfa2> in <lambda>(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

可以帮我解决这个问题吗?

谢谢

2 个答案:

答案 0 :(得分:0)

尝试也在外部.py中导入numpy,这样:

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)

答案 1 :(得分:0)

确保已为Python 3安装了NumPy,您可能在先前的代码中使用了Python 2的安装。希望对您有帮助