October 22, 2024
Chicago 12, Melborne City, USA
python

MemoryError in creating large numpy array


My objective is to plot a histogram given values and counts. hist only takes an array of data as input. I have tried to recreat data using np.repeat, but this gives MemoryError: Unable to allocate 15.9 GiB for an array with shape (2138500000,) and data type float64.

Wanted to know if there is a smarter way of doing this.

import numpy as np 
import matplotlib.pyplot as plt 

values = [ 1, 2, 2.5, 4, 5, 5.75, 6.5]
counts = [10**8, 10**9, 1.5*10**7, 1.25*10**7, 10**6, 10**7,10**9]

data_recreated = np.repeat(values, counts)

f1, ax = plt.subplots(1,1)

ax.hist(data_recreated, bins=5)



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video