简体   繁体   English

如何在 Python 中插入未排序的 2D numpy 数组并将插入的值与原始值进行比较?

[英]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.我正在尝试插入 SVD 分解的模式系数。 I have a quadratic equation ax^2 + bx , with random a and b .我有一个二次方程ax^2 + bx ,随机ab I then fill a matrix y the values of the quadratic, and then use svd .然后我用二次方的值填充矩阵y ,然后使用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.现在我需要对代码中的c1给出的第一种模式的结果系数进行插值,用于 0 和 1 之间的ab 。在过去的几天里我尝试了很多东西,但都没有奏效,还有其他问题插值对我没有帮助。 My interpolant gives exact c1 values at the a and b and c1 I specified, but nonsense at the other known c1 values.我的插值器在我指定的ab以及c1处给出了准确的c1值,但在其他已知的c1值上则是胡说八道。

Also, how would I be able to check the c1_interpolated array for values at a specific a and b ?另外,我如何能够检查c1_interpolated数组中特定ab ie c1(a = 0.05, b = 0.08) without calling the function every time?c1(a = 0.05, b = 0.08)没有每次都调用函数?

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...我真的不明白你想在这里实现什么......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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