Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NameError: global name 'numpy' is not defined

Tags:

python

numpy

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

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.

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

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
like image 605
Francis Avatar asked Aug 11 '14 08:08

Francis


People also ask

How do I fix NameError name NP not defined?

The Python "NameError: name 'numpy' is not defined" occurs when we use the numpy module without importing it first. To solve the error, install the module and import it ( import numpy ) before using it. Open your terminal in your project's root directory and install the numpy module.

Why is my import NumPy not working?

Python import numpy is not working that means eithers the module is not installed or the module is corrupted. To fix the corrupted module, uninstall it first then reinstall it.

Why is NP not defined?

In Python, NameError: name 'np' is not defined occurs when you import the NumPy library but fail to provide the alias as np while importing it.


Video Answer


1 Answers

Try simply

import numpy

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

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

like image 100
Pierre de Buyl Avatar answered Oct 09 '22 09:10

Pierre de Buyl