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

0

I am analyzing an audio and my ML model of scikit learn generates an array of probabilities that a certain element in the array corresponds to an "A" category 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 turn this array of size 497305 into one of size 4998 or 4999 with the average probability of each second matching the "A" category of training.

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

    
asked by anonymous 11.12.2017 / 19:48

1 answer

0

Considering that you only have to average 100 values per 100, you can simply make a new matrix with an average of 100 points.

To do this you need to take some precautions because of the number of points. Below is 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:]))
    
12.12.2017 / 18:34