简体   繁体   中英

How can I interpolate unsorted 2D numpy arrays in Python and compare the interpolated values to the original one?

I am trying to interpolate the mode coefficients of an SVD decomposition. I have a quadratic equation ax^2 + bx , with random a and b . I then fill a matrix y the values of the quadratic, and then use svd . Now I need to interpolate the resulting coefficients of the first mode, given by c1 in the code, for a and b between 0 and 1. I have tried many things over the last couple of days, but none is working, and the other questions on interpolation are not helping me. My interpolant gives exact c1 values at the a and b and c1 I specified, but nonsense at the other known c1 values.

Also, how would I be able to check the c1_interpolated array for values at a specific a and b ? ie c1(a = 0.05, b = 0.08) without calling the function every time?

I'd be very grateful for any help, since I've been stuck on this issue for several days.

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import griddata


def f(x, a, b):
    return a*x*x + b*x + 1

end = 1.01
begin = 0
x = np.arange(begin,end,0.1)
npoints = int(1/0.1+1)

a = (np.random.uniform(0,1,npoints))
b = (np.random.uniform(0,1,npoints))
#c = np.random.uniform(0,1,npoints)

cols = 5

y = np.zeros((cols,a.size))

for i in range(cols):
        y[i] = f(x, a[i], b[i])

u, s, vh = np.linalg.svd(y, full_matrices=True)


c1 = (vh[0])
c2 = vh[1]
c3 = vh[2]

sample  = 8
input_arr = list(zip(a[:sample], b[:sample]))

A= np.linspace(min(a[:sample]), max(a[:sample]))
B = np.linspace(min(b[:sample]), max(b[:sample]))
A, B = np.meshgrid(A, B)

interp = scipy.interpolate.LinearNDInterpolator(input_arr, c1[:sample], fill_value=0)

c1_interpolated = interp(A, B)

I will just try to sum up here what I have understood because this sounds extremely confusing.

You have a matrix. You are performing a singular value decomposition on it, which gives you back three arrays. You then wish to interpolate through the third array, which corresponds to the right-singular vectors of the matrix and is a set of orthonormal eigenvectors of the product of the hermitian of the matrix with itself. Not even asking how to perform this interpolation, what will it give you? If we consider the first row of your matrix, each of its elements is given by the product of the first row of the first array by the first element of the second array by each column of the third array. If you interpolate through the columns of the third array, this is going to give you a first row of the matrix with more elements. So why don't you just interpolate the matrx right away? I really do not understand what you want to achieve here...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM