问题描述
我有一个实验观察结果:
I have experimental observations in a volume:
import numpy as np
# observations are not uniformly spaced
x = np.random.normal(0, 1, 10)
y = np.random.normal(5, 2, 10)
z = np.random.normal(10, 3, 10)
xx, yy, zz = np.meshgrid(x, y, z, indexing='ij')
# fake temperatures at those coords
tt = xx*2 + yy*2 + zz*2
# sample distances
dx = np.diff(x)
dy = np.diff(y)
dz = np.diff(z)
grad = np.gradient(tt, [dx, dy, dz]) # returns error
这给了我错误:
根据@ jay-kominek在以下评论中:
according to @jay-kominek in the comments below:
我已经更新了问题.有什么功能可以进行计算吗?
I've updated the question. Is there any function which can can do my computation?
推荐答案
注意两点:首先,标量是单个值,而不是数组.其次,函数的签名为numpy.gradient(f, *varargs, **kwargs)
.注意varargs
前的*.这意味着如果varargs
是列表,则传递*varargs
.或者,您可以只提供varargs
的元素作为单独的参数.
Two things to note: First, scalars are single values, not arrays. Second, the signature of the function is numpy.gradient(f, *varargs, **kwargs)
. Note the * before varargs
. That means if varargs
is a list, you pass *varargs
. Or you can just provide the elements of varargs
as separate arguments.
因此,np.gradient
希望沿每个维度的距离为单个值,例如:
So, np.gradient
wants a single value for the distance along each dimension, like:
np.gradient(tt, np.diff(x)[0], np.diff(y)[0], np.diff(z)[0])
或:
distances = [np.diff(x)[0], np.diff(y)[0], np.diff(z)[0]]
np.gradient(tt, *distances)
这篇关于用于在样品位置不均匀的情况下计算3D梯度的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!