I have data points that represent a coordinates for a 2D array (matrix). The points are regularly gridded, except that data points are missing from some grid positions.
For example, consider some XYZ data that fits on a regular 0.1 grid with shape (3, 4). There are gaps and missing points, so there are 5 points, and not 12:
import numpy as np
X = np.array([0.4, 0.5, 0.4, 0.4, 0.7])
Y = np.array([1.0, 1.0, 1.1, 1.2, 1.2])
Z = np.array([3.3, 2.5, 3.6, 3.8, 1.8])
# Evaluate the regular grid dimension values
Xr = np.linspace(X.min(), X.max(), np.round((X.max() - X.min()) / np.diff(np.unique(X)).min()) + 1)
Yr = np.linspace(Y.min(), Y.max(), np.round((Y.max() - Y.min()) / np.diff(np.unique(Y)).min()) + 1)
print('Xr={0}; Yr={1}'.format(Xr, Yr))
# Xr=[ 0.4 0.5 0.6 0.7]; Yr=[ 1. 1.1 1.2]
What I would like to see is shown in this image (backgrounds: black=base-0 index; grey=coordinate value; colour=matrix value; white=missing).
Here's what I have, which is intuitive with a for loop:
ar = np.ma.array(np.zeros((len(Yr), len(Xr)), dtype=Z.dtype), mask=True)
for x, y, z in zip(X, Y, Z):
j = (np.abs(Xr - x)).argmin()
i = (np.abs(Yr - y)).argmin()
ar[i, j] = z
print(ar)
# [[3.3 2.5 -- --]
# [3.6 -- -- --]
# [3.8 -- -- 1.8]]
Is there a more NumPythonic way of vectorising the approach to return a 2D array ar
? Or is the for loop necessary?
The
sparse
matrix is the first solution that came to mind, but sinceX
andY
are floats, it's a little messy:It still needs, in one way or other, to match those coordinates with [0,1,2...] indexes. My quick cheat was to just scale the values linearly. Even so I had to take care when converting floats to ints.
sparse.coo_matrix
works because a natural way of defining a sparse matrix is with(i, j, data)
tuples, which of course can be translated toI
,J
,Data
lists or arrays.I rather like the historgram solution, even though I haven't had occasion to use it.
You can do it on one line with
np.histogram2d
You could use a scipy coo_matrix. It allows you to construct a sparse matrix from coordinates and data. See examples on the attached link.
http://docs.scipy.org/doc/scipy-0.15.1/reference/generated/scipy.sparse.coo_matrix.html
Hope that helps.
You can use
X
andY
to create the X-Y coordinates on a0.1
spaced grid extending from themin to max of X
andmin to max of Y
and then insertingZ's
into those specific positions. This would avoid usinglinspace
to getXr
andYr
and as such must be quite efficient. Here's the implementation -Runtime tests -
This section compare the
indexing-based
approach against the othernp.histogram2d
based solution for performance -