Saya ingin menginterpolasi matriks dengan kolom acak yang hilang.

Saya membuat matriks dengan entri acak (bisa berupa gambar), memusatkan beberapa kolom dan mereka mencoba untuk menginterpolasi kolom yang nol.

import numpy as np 
from scipy.interpolate import griddata

l = 2000
c = 340

tdgn = np.random.rand(l, c) #array with 2000 lines and 240 columns, or some grayscale image, binary file... np.load(...)


#sort some column indexes to "decimate"
maxindex=tdgn.shape[1]
p_dec = 0.3
index = np.arange(maxindex)
index_dec = np.sort(np.random.choice(maxindex, int(np.round(maxindex*p_dec)), replace=False))
index_nondec = np.array(list(filter(lambda x: x not in index_dec, index)))

#decimate
tdgn_dec = np.copy(tdgn)
tdgn_dec[:, index_nondec] = 0

#grid points to interpolate
grid_coods_dec_x, grid_coods_dec_y = np.meshgrid(np.arange(tdgn_dec.shape[0]), index_dec )
grid_coods_x, grid_coods_y = np.meshgrid(np.arange(tdgn.shape[0]), np.arange(tdgn.shape[1]) )


#interpolate
tdgn_interp = griddata( (grid_coods_dec_x, grid_coods_dec_y), tdgn[ grid_coods_dec_x, grid_coods_dec_y], (grid_coods_x, grid_coods_y), method='linear')

'tdgn_interp' harus memiliki dimensi yang sama dengan 'tdgn_dec', tetapi dengan nilai nolnya sedekat mungkin dengan matriks aslinya, 'tdgn'.

Tapi, "ValueError: bentuk tidak valid untuk titik data input" dikembalikan...

0
Marcelo Silva 19 September 2019, 22:30

1 menjawab

Jawaban Terbaik
def interp_traces(tdgn_dec, col_nondec, method='linear'):

    grid_coods_x, grid_coods_y = np.meshgrid(np.arange(tdgn_dec.shape[1]), np.arange(tdgn_dec.shape[0]) )

    grid_coods_nondec_x, grid_coods_nondec_y = np.meshgrid(col_nondec, np.arange(tdgn_dec.shape[0]) )

    f_grid_x = grid_coods_nondec_x.flatten()
    f_grid_y = grid_coods_nondec_y.flatten()

    f_points = tdgn_dec[grid_coods_nondec_y, grid_coods_nondec_x].flatten()

    interp = griddata((f_grid_x, f_grid_y), f_points, (grid_coods_x, grid_coods_y), method=method)

    return( interp )

Menggunakan 'interp_traces' seperti di atas...

tdgn_interp = interp_traces(tdgn_dec, index_nondec)
0
Marcelo Silva 24 September 2019, 18:11