a = np.array([[ 20., 20.],
[ 20., 30.],
[ 30., 30.],
[ 30., 20.],
[ 20., 20.]])<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN> Then you run some code <SPAN class="keyword token">def</SPAN> <SPAN class="token function">_densify_2D</SPAN><SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">,</SPAN> fact<SPAN class="operator token">=</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="string token">"""Densify a 2D array using np.interp.
"""</SPAN>
<SPAN class="comment token"># Y = a changed all the y's to a</SPAN>
a <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>squeeze<SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">)</SPAN>
n_fact <SPAN class="operator token">=</SPAN> len<SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">*</SPAN> fact
b <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>arange<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> n_fact<SPAN class="punctuation token">,</SPAN> fact<SPAN class="punctuation token">)</SPAN>
b_new <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>arange<SPAN class="punctuation token">(</SPAN>n_fact <SPAN class="operator token">-</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN>
c0 <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>interp<SPAN class="punctuation token">(</SPAN>b_new<SPAN class="punctuation token">,</SPAN> b<SPAN class="punctuation token">,</SPAN> a<SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
c1 <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>interp<SPAN class="punctuation token">(</SPAN>b_new<SPAN class="punctuation token">,</SPAN> b<SPAN class="punctuation token">,</SPAN> a<SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
n <SPAN class="operator token">=</SPAN> c0<SPAN class="punctuation token">.</SPAN>shape<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
c <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>zeros<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">(</SPAN>n<SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
c<SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> c0
c<SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> c1
<SPAN class="keyword token">return</SPAN> c<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN> | 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.]])<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN> a<SPAN class="punctuation token">.</SPAN>shape <SPAN class="comment token"># (49874, 2)</SPAN>
<SPAN class="operator token">%</SPAN>timeit _densify_2D<SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">,</SPAN> fact<SPAN class="operator token">=</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="number token">5.25</SPAN> ms ± <SPAN class="number token">323</SPAN> µs per loop
<SPAN class="punctuation token">(</SPAN>mean ± std<SPAN class="punctuation token">.</SPAN> dev<SPAN class="punctuation token">.</SPAN> of <SPAN class="number token">7</SPAN> runs<SPAN class="punctuation token">,</SPAN>
<SPAN class="number token">100</SPAN> loops each<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN> |