numpy how to apply interpolation all rows

Solutions on MaxInterview for numpy how to apply interpolation all rows by the best coders in the world

showing results for - "numpy how to apply interpolation all rows"
Kimberley
29 Aug 2017
1import numpy as np
2from scipy.interpolate import interp1d
3
4# generate some example data
5W = 3
6H = 10
7M = 5
8
9A2 = np.arange(W * M).reshape(W, M)
10print(A2)
11# [[ 0  1  2  3  4]
12#  [ 5  6  7  8  9]
13#  [10 11 12 13 14]]
14
15# the initial column indices for A2
16x = np.arange(M)
17
18# we create a scipy.interpolate.interp1d instance
19itp_A2 = interp1d(x, A2, kind='nearest')
20
21# the output column coordinates for A1
22xi = np.linspace(0, M - 1, H)
23
24# we get the interpolated output by calling the interp1d instance with the
25# output coordinates
26A1 = itp_A2(xi)
27print(A1)
28# [[  0.   0.   1.   1.   2.   2.   3.   3.   4.   4.]
29#  [  5.   5.   6.   6.   7.   7.   8.   8.   9.   9.]
30#  [ 10.  10.  11.  11.  12.  12.  13.  13.  14.  14.]]