'Numbers are off when calculating cumulative average
I'm trying to calculate the cumulative average (element-wise) of a numpy array in python. Here is the code:
class Prediction:
# irrelevant code omitted
cumulative_average = np.ones((1,494))
for i, clip in enumerate(clip_paths):
if glob.glob(f'{clip}.png'):
async_to_sync(self.channel_layer.send)(self.channel_name, {
'type': 'websocket.send',
'text': f'skipping {clip}'})
continue
y,sr = librosa.load(clip, duration=3)
mels = librosa.feature.melspectrogram(y=y, sr=sr)
fig = plt.Figure()
canvas = FigureCanvas(fig)
p = plt.imshow(librosa.power_to_db(mels,ref=np.max))
plt.axis('off')
p.axes.get_xaxis().set_visible(False)
p.axes.get_yaxis().set_visible(False)
filename = clip.split('/')[-1].split('.mp3')[0]
spectrogram_path = f'/tmp/{filename}.png'
plt.savefig(spectrogram_path, bbox_inches='tight', pad_inches=0)
spectrogram = PIL.Image.open(spectrogram_path)
img = np.array(spectrogram)[:, :, :3]
cumulative_average = (self.query_model(img) + (i * cumulative_average)) / (i + 1)
# self.query_model(img) returns an array of shape (1,494)
async_to_sync(self.channel_layer.send)(self.channel_name, {
'type': 'websocket.send',
'text': f'converted and got predictions for spectrogram image {spectrogram_path}'})
I omitted the irrelevant bits of the code, but comment if you need more information. The forumla for the cumulative average is on wikipedia: https://en.wikipedia.org/wiki/Moving_average#Cumulative_average
The numbers I'm getting are off. All of the numbers in the array are around .93
Can anyone help?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
