NameError: global name numpy is not defined

2024/10/12 16:22:16

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 essentiadef featureExtractor(path):loader = MonoLoader(filename = path)x = loader()pool = essentia.Pool()spectrum = Spectrum()w = Windowing(type = 'hann')# Create needed objectsmfcc = MFCC()centroid = Centroid()for frame in FrameGenerator(x, frameSize = 1024, hopSize = 512):    mfcc_bands, mfcc_coeffs = mfcc(spectrum(w(frame))) # output: vector_realspec_centroid = centroid(spectrum(w(frame))) # output: realpool.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 arraylist = ['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 == arrayfeature_vec = np.concatenate([feature_vec,feature], axis = 0)else: # for those type == floatfeature_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 featureExtractor2 
----> 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 == array69                 feature_vec = np.concatenate([feature_vec,feature], axis = 0)70         else: # for those type == floatNameError: 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
Answer

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.

https://en.xdnf.cn/q/69633.html

Related Q&A

Django exclude from annotation count

I have following application:from django.db import modelsclass Worker(models.Model):name = models.CharField(max_length=60)def __str__(self):return self.nameclass Job(models.Model):worker = models.Forei…

How to use yield function in python

SyntaxError: yield outside function>>> for x in range(10): ... yield x*x ... File "<stdin>", line 2 SyntaxError: yield outside functionwhat should I do? when I try to use …

Tkinter looks different on different computers

My tkinter window looks very different on different computers (running on the same resolution!):windows 8windows 7I want it to look like it does in the first one. Any ideas?My code looks like this:cla…

Sort when values are None or empty strings python

I have a list with dictionaries in which I sort them on different values. Im doing it with these lines of code:def orderBy(self, col, dir, objlist):if dir == asc:sorted_objects = sorted(objlist, key=la…

How to tell if you have multiple Djangos installed

In the process of trying to install django, I had a series of failures. I followed many different tutorials online and ended up trying to install it several times. I think I may have installed it twice…

Python Numerical Integration for Volume of Region

For a program, I need an algorithm to very quickly compute the volume of a solid. This shape is specified by a function that, given a point P(x,y,z), returns 1 if P is a point of the solid and 0 if P i…

invalid literal for int() with base 10: on Python-Django

i am learning django from official django tutorial. and i am getting this error when vote something from form. this caused from - probably - vote function under views.py here is my views.py / vote func…

How to use HTTP method DELETE on Google App Engine?

I can use this verb in the Python Windows SDK. But not in production. Why? What am I doing wrong?The error message includes (only seen via firebug or fiddler)Malformed requestor something like thatMy…

Python heapq : How do I sort the heap using nth element of the list of lists?

So I have lists of lists getting added to the heap; eg:n = [[1, 5, 93],[2, 6, 44],[4, 7, 45],[6, 3, 12]]heapq.heapify(n)print(n)This compares and sorts according to the lists first element.My question …

How strings are stored in python memory model

I am from c background and a beginner in python. I want to know how strings are actually stored in memory in case of python.I did something likes="foo"id(s)=140542718184424id(s[0])= 140542719…