How to transform a numpy.array of size 497305 into a smaller 4999 without adding new elements in the calculation?

Asked

Viewed 110 times

1

I’m analyzing an audio and my scikit ML model generates an array of probabilities that certain element in the array corresponds to a category "A" used to train the model.

Each element of the array corresponds to a probability of an analysis window obtained through several transformations (FFT, Mel...). For an audio of 4998,0487 seconds an array of probabilities is generated for 497305 analysis windows. My goal is to transform this array of size 497305 into one of size 4998 or 4999 with the average probability of each second corresponding to the training category "A".

My goal, in short, is to use the average of 100 windows for each second (497305 windows / 4998,0487 ~= 99.5). How to do this in a simple way using numpy?

1 answer

0

Considering that you only need to make an average of the values obtained every 100, you can simply make a new Matrix with average of every 100 points.

To do this it is necessary to take precautions due to the number of points. Below follows the code for this, assuming a vector.

# a é o nome do seu vetor inicial
step=100  #seu passo 
npoints=a.size  #verifica a quantidade de pontos
if npoints%step : #use este IF pra separar os últimos valores
   usedpoints=npoints-npoints%step
else : #ou apenas copia o número
   usedpoints=npoints   

#aqui eu faco a media e deixo em uma matriz
data_average=numpy.mean(a[:usedpoints].reshape(-1,step),axis=1)
#aqui eu pego a média dos últimos pontos e junta a matriz da média. 
numpy.append(data_average,numpy.mean(a[usedpoints:]))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.