a = np.array([[ 20., 20.],
[ 20., 30.],
[ 30., 30.],
[ 30., 20.],
[ 20., 20.]])
Then you run some code def _densify_2D(a, fact=2):
"""Densify a 2D array using np.interp.
"""
a = np.squeeze(a)
n_fact = len(a) * fact
b = np.arange(0, n_fact, fact)
b_new = np.arange(n_fact - 1)
c0 = np.interp(b_new, b, a[:, 0])
c1 = np.interp(b_new, b, a[:, 1])
n = c0.shape[0]
c = np.zeros((n, 2))
c[:, 0] = c0
c[:, 1] = c1
return c
| And you get this. a_dens = _densify_2D(a, fact=2)
a_dens
array([[ 20., 20.],
[ 20., 25.],
[ 20., 30.],
[ 25., 30.],
[ 30., 30.],
[ 30., 25.],
[ 30., 20.],
[ 25., 20.],
[ 20., 20.]])
a.shape
%timeit _densify_2D(a, fact=2)
5.25 ms ± 323 µs per loop
(mean ± std. dev. of 7 runs,
100 loops each)
|