python - 名称错误 : global name 'numpy' is not defined

标签 python numpy

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

从 poolAggregator 连接形成整个特征列表

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

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

import numpy as np
def featureExtractor(path):

或者在函数中:

def featureExtractor(path):
    import numpy as np

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

import numpy as np
from featureExtractor import featureExtractor

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

脚本如下:

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

然后我在主文件中命令:

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

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

import numpy as np

最佳答案

简单尝试

import numpy

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

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

关于python - 名称错误 : global name 'numpy' is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25239016/

相关文章:

python - numpy.concatenate 如何在列表上工作

python - 使用crew或pip安装程序在Chromebook上安装Python的matplotlib

python - pandas 数据帧上的 apply 函数可以生成标量吗?

python - 给定 2 个整数列表如何找到不重叠的范围?

python - numpy.float64 对象不可迭代...但我不想

python - 使用 pandas 缓存 CSV 读取数据以进行多次运行

python - 使用 ImageGrid 为每个网格添加标题

python - 在Python中循环遍历图像中每个像素的更快方法?

python - 绘制给定 bin 端点和值的直方图

Python Pandas - 如果满足条件则更改组内的列