从另一个.py文件导入函数时未定义numpy包

时间:2021-07-22 16:00:42

In my master file I have:

在我的主文件中,我有:

import matplotlib.pyplot as plt
import seaborn
import numpy as np
import time

import sys
sys.path.append("C:/.../python check/createsplit")
import createsplit

data='MJexample'
X,Y,N,Ntr=create_training_data(data)

where I am calling create_training_data function from createsplit.py file which is:

我在createsplit.py文件中调用create_training_data函数,它是:

import numpy as np
import scipy.io

def create_training_data(data_type):
     """
     creates training data
     """
     if data_type=='MJexample':
         N=300
         Ntr = 150
         X=np.linspace(0,1,N)
         X = np.array([X,X*X,np.linspace(5,10,N),np.sin(X),np.cos(X),np.sin(X)*np.cos(X)]).T

         fac=40
         Y=np.array([np.sin(fac*x)*np.cos(fac*x**2) for x in X[:,0]])[:,None]

         _X=X
         _Y=Y
         return _X,_Y,N,Ntr

However running my original file results in error: NameError: global name 'np' is not defined for some reason I do not understand. I assume I am importing the functions in a wrong way but I don't really understand what would be wrong.

但是,运行我的原始文件会导致错误:NameError:由于某些我无法理解的原因,未定义全局名称'np'。我假设我以错误的方式导入函数,但我真的不明白什么是错的。

1 个解决方案

#1


1  

I think this issue raises just because of a wrong call of the function. Try

我认为这个问题只是因为函数的错误调用而引发的。尝试

X, Y, N, Ntr = createsplit.create_training_data(data)

instead, and it should work.

相反,它应该工作。

#1


1  

I think this issue raises just because of a wrong call of the function. Try

我认为这个问题只是因为函数的错误调用而引发的。尝试

X, Y, N, Ntr = createsplit.create_training_data(data)

instead, and it should work.

相反,它应该工作。