Python NameError:未定义全局名称“numpy”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25239016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:55:01  来源:igfitidea点击:

NameError: global name 'numpy' is not defined

pythonnumpy

提问by Francis

I am trying to write a feature extractor by gathering essentia's (a MIR library) functions. The flow chart is like: individual feature extraction, pool, PoolAggregator, concatenate to form the whole feature list from poolAggregator using np.concatenate

我正在尝试通过收集 essentia 的(一个 MIR 库)函数来编写一个特征提取器。流程图是这样的:单个特征提取,池化,PoolAggregator,使用poolAggregator从poolAggregator连接形成整个特征列表np.concatenate

The script runs well under ipython notebook even without importing numpy.I'm just congregating the array or float number I got from the previous stage but the error message: "NameError: global name 'numpy' is not defined"shows.

即使不导入 numpy,该脚本也能在 ipython notebook 下运行良好。我只是聚集了我从上一阶段得到的数组或浮点数,但错误消息:"NameError: global name 'numpy' is not defined"显示。

I've tried to put "import numpy as np" at the top of the module:

我试图将“import numpy as np”放在模块的顶部:

import numpy as np
def featureExtractor(path):

or in the function:

或在函数中:

def featureExtractor(path):
    import numpy as np

or outside the module in the main file:

或在主文件中的模块之外:

import numpy as np
from featureExtractor import featureExtractor

None of these can solve it, please help me.

这些都不能解决,请帮帮我。

The following is the script:

以下是脚本:

from essentia.standard import *
import essentia

def featureExtractor(path):
    loader = MonoLoader(filename = path)
    x = loader()

    pool = essentia.Pool()
    spectrum = Spectrum()
    w = Windowing(type = 'hann')
    # Create needed objects
    mfcc = MFCC()
    centroid = Centroid()

    for frame in FrameGenerator(x, frameSize = 1024, hopSize = 512):    

        mfcc_bands, mfcc_coeffs = mfcc(spectrum(w(frame))) # output: vector_real
        spec_centroid = centroid(spectrum(w(frame))) # output: real

        pool.add('lowlevel.mfcc', mfcc_coeffs)
        pool.add('lowlevel.centroid', spec_centroid)

    aggrPool = PoolAggregator(defaultStats = [ 'mean', 'var' ])(pool) 
    # calculate mean and var for each feature

    # build a feature vector of type array
    list = ['lowlevel.centroid.mean', 'lowlevel.centroid.var',
            'lowlevel.mfcc.mean', 'lowlevel.mfcc.var']

    feature_vec = []

    for name in list:
        feature = aggrPool[name]
        if type(feature) != float:  # for those type == array
           feature_vec = np.concatenate([feature_vec,feature], axis = 0)
        else: # for those type == float
           feature_vec.append(feature)
    return feature_vec

Then I command in the main file:

然后我在主文件中命令:

path = "/~/Downloads/~.wav"
from featureExtractor import featureExtractor
featureExtractor(path)

I got the error:

我得到了错误:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-109-40b5bbac9b17> in <module>()
      1 from featureExtractor import featureExtractor
      2 
----> 3 featureExtractor(path)

/~/ipython_notebook/featureExtractor.py in featureExtractor(path)
     66         for name in list:
     67                 feature = aggrPool[name]
---> 68         if type(feature) != float:  # for those type == array
     69                 feature_vec = np.concatenate([feature_vec,feature], axis = 0)
     70         else: # for those type == float

NameError: global name 'numpy' is not defined

And I got the same error no matter where I put the command (as described in above)

无论我把命令放在哪里,我都会遇到同样的错误(如上所述)

import numpy as np

采纳答案by Pierre de Buyl

Try simply

简单尝试

import numpy

at the top of the file /~/ipython_notebook/featureExtractor.py

在文件顶部 /~/ipython_notebook/featureExtractor.py

It seems that your code expect numpyand not npas the module name.

似乎您的代码期望numpy而不是np作为模块名称。