python 代码模块化,模块化代码python时未知的librarie

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值