Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MemoryError when running Numpy Meshgrid

I have 8823 data points with x,y coordinates. I'm trying to follow the answer on how to get a scatter dataset to be represented as a heatmap but when I go through the

X, Y = np.meshgrid(x, y)

instruction with my data arrays I get MemoryError. I am new to numpy and matplotlib and am essentially trying to run this by adapting the examples I can find.

Here's how I built my arrays from a file that has them stored:

XY_File = open ('XY_Output.txt', 'r')
XY = XY_File.readlines()
XY_File.close()

Xf=[]
Yf=[]
for line in XY:
    Xf.append(float(line.split('\t')[0]))
    Yf.append(float(line.split('\t')[1]))
x=array(Xf)
y=array(Yf)

Is there a problem with my arrays? This same code worked when put into this example but I'm not too sure.

Why am I getting this MemoryError and how can I fix this?

like image 954
greye Avatar asked Mar 17 '10 08:03

greye


People also ask

What does Meshgrid do in numpy?

The numpy. meshgrid function is used to create a rectangular grid out of two given one-dimensional arrays representing the Cartesian indexing or Matrix indexing.

What is Meshgrid in Python?

In python, meshgrid is a function that creates a rectangular grid out of 2 given 1-dimensional arrays that denotes the Matrix or Cartesian indexing. It is inspired from MATLAB. This meshgrid function is provided by the module numpy. Coordinate matrices are returned from the coordinate vectors.

Why do we need Meshgrid?

The purpose of meshgrid is to create a rectangular grid out of an array of x values and an array of y values. So, for example, if we want to create a grid where we have a point at each integer value between 0 and 4 in both the x and y directions.


1 Answers

Your call to meshgrid requires a lot of memory -- it produces two 8823*8823 floating point arrays. Each of them are about 0.6 GB.

But your screen can't show (and your eye can't really process) that much information anyway, so you should probably think of a way to smooth your data to something more reasonable like 1024*1024 before you do this step.

like image 148
Andrew Jaffe Avatar answered Oct 06 '22 09:10

Andrew Jaffe