NameError:全局名称'numpy'未定义 [英] NameError: global name 'numpy' is not defined

查看:346
本文介绍了NameError:全局名称'numpy'未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过收集essentia(MIR库)函数来编写特征提取器. 流程图类似于:单个特征提取,池,PoolAggregator,使用np.concatenate

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

即使在不导入numpy的情况下,该脚本也可以在ipython Notebook上很好地运行. 我只是在汇总从上一阶段获得的数组或浮点数,但显示错误消息:"NameError: global name 'numpy' is not defined".

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.

我尝试将"import numpy as np"放在模块顶部:

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

import numpy as np
def featureExtractor(path):

或在函数中:

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.

以下是脚本:

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)

我得到了错误:

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

推荐答案

简单尝试

import numpy

位于文件/~/ipython_notebook/featureExtractor.py

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

It seems that your code expect numpy and not np as the module name.

这篇关于NameError:全局名称'numpy'未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆