Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Numpy:具有积分极限的数值积分_Python_Arrays_Numpy_Integration - Fatal编程技术网

Python Numpy:具有积分极限的数值积分

Python Numpy:具有积分极限的数值积分,python,arrays,numpy,integration,Python,Arrays,Numpy,Integration,我已经测量了我想要在一定范围内积分的峰值 我要集成的数据是以numpy阵列的形式,具有波数和强度: peakQ1_2500_smoothened = array([[ 1.95594400e+04, -3.70074342e-17, 3.26000000e+00], [ 1.95594500e+04, 1.66666667e-03, 4.81500000e+00], [ 1.95594600e+04, 2.83333333e-02, 4.80833333e+

我已经测量了我想要在一定范围内积分的峰值

我要集成的数据是以numpy阵列的形式,具有波数和强度:

peakQ1_2500_smoothened =
array([[ 1.95594400e+04, -3.70074342e-17,  3.26000000e+00],
       [ 1.95594500e+04,  1.66666667e-03,  4.81500000e+00],
       [ 1.95594600e+04,  2.83333333e-02,  4.80833333e+00],
       [ 1.95594700e+04,  1.33333333e-02,  4.82166667e+00],
       [ 1.95594800e+04,  5.00000000e-03,  4.92416667e+00],
       [ 1.95594900e+04,  5.55555556e-04,  4.99305556e+00],
       [ 1.95595100e+04, -7.77777778e-03,  5.03972222e+00],
       [ 1.95595200e+04, -5.55555556e-03,  4.96888889e+00],
       [ 1.95595300e+04, -1.77777778e-02,  4.91333333e+00],
       [ 1.95595400e+04,  1.38888889e-02,  4.82500000e+00],
       [ 1.95595500e+04,  7.05555556e-02,  4.85722222e+00],
       [ 1.95595600e+04,  1.43888889e-01,  4.86638889e+00],
       [ 1.95595700e+04,  1.98888889e-01,  4.85138889e+00],
       [ 1.95595800e+04,  2.84444444e-01,  4.90694444e+00],
       [ 1.95595900e+04,  4.64444444e-01,  4.93611111e+00],
       [ 1.95596000e+04,  6.61111111e-01,  4.98166667e+00],
       [ 1.95596100e+04,  9.61666667e-01,  4.96722222e+00],
       [ 1.95596200e+04,  1.23222222e+00,  4.94388889e+00],
       [ 1.95596400e+04,  1.43555556e+00,  5.02166667e+00],
       [ 1.95596500e+04,  1.53222222e+00,  5.00500000e+00],
       [ 1.95596600e+04,  1.59833333e+00,  5.03666667e+00],
       [ 1.95596700e+04,  1.66388889e+00,  4.94555556e+00],
       [ 1.95596800e+04,  1.60111111e+00,  4.92777778e+00],
       [ 1.95596900e+04,  1.42333333e+00,  4.94666667e+00],
       [ 1.95597000e+04,  1.14111111e+00,  5.00777778e+00],
       [ 1.95597100e+04,  9.52222222e-01,  5.08555556e+00],
       [ 1.95597200e+04,  7.25555556e-01,  5.09222222e+00],
       [ 1.95597300e+04,  5.80555556e-01,  5.08055556e+00],
       [ 1.95597400e+04,  3.92777778e-01,  5.09611111e+00],
       [ 1.95597500e+04,  2.43222222e-01,  5.01655556e+00],
       [ 1.95597600e+04,  1.36555556e-01,  4.99822222e+00],
       [ 1.95597700e+04,  6.32222222e-02,  4.87044444e+00],
       [ 1.95597800e+04,  3.88888889e-02,  4.91944444e+00],
       [ 1.95597900e+04,  3.22222222e-02,  4.93611111e+00],
       [ 1.95598000e+04,  2.44444444e-02,  5.10277778e+00],
       [ 1.95598100e+04,  5.11111111e-02,  5.11277778e+00],
       [ 1.95598200e+04,  4.44444444e-02,  5.21944444e+00],
       [ 1.95598300e+04,  4.33333333e-02,  5.05333333e+00],
       [ 1.95598400e+04,  3.58333333e-02,  5.08750000e+00],
       [ 1.95598500e+04,  7.50000000e-03,  5.12750000e+00],
       [ 1.95598600e+04,  4.16666667e-03,  5.22916667e+00],
       [ 1.95598800e+04, -1.33333333e-02,  3.51000000e+00]])

我发现我可以通过以下方式对整个阵列进行集成:

def integratePeak(yvals, xvals):
    I = np.trapz(yvals, x = xvals)
    return I
但是我如何与x极限进行积分,例如从19559.52到19559.78

def integratePeak(yvals, xvals, xlower, xupper):
    '''integrate y over x from xlower to xupper'''
    return I
当然,我可以通过将数组元素显式引用为
peakQ1\u 2500\u平滑[7:33,0]
peakQ1\u 2500\u平滑[7:33,1]来给出x和y值
但显然,我不想提及阵列元素,而是将积分极限定义为波数,因为不同的测量峰值具有不同的阵列长度


用于将每个波数减少到一个数据点,然后获取运行平均值的函数:

def averagePerWavenumber(data):
    wavenum, intensity, power = data[:,0], data[:,1], data[:,2]
    wavenum_unique, intensity_mean = npi.group_by(wavenum).mean(intensity)
    wavenum_unique, power_mean = npi.group_by(wavenum).mean(power)
    output = np.zeros(shape=(len(wavenum_unique), 3))
    output[:,0] = wavenum_unique
    output[:,1] = intensity_mean
    output[:,2] = power_mean
    return output

def smoothening(data, bins):
    output = np.zeros(shape=(len(data[:,0]), 3))
    output[:,0] = data[:,0]
    output[:,1] = np.convolve(data[:,1], np.ones(bins), mode='same') / bins
    output[:,2] = np.convolve(data[:,2], np.ones(bins), mode='same') / bins
    return output
另一方面,使用pandas进行表格数据可能会让您受益匪浅—它与numpy阵列可以很好地互操作,最重要的是,它可以让您标记数据:

import pandas as pd
df = pd.DataFrame(peakQ1_2500_smoothened, columns=["wave_num", "intensity", "col3"])

integratePeak(yvals=df.intensity, xvals=df.wave_num, xlower=19559.52, xupper=19559.78)

# 0.18853555549577536

让我们从实际做什么开始。
i
th梯形的面积是平均高度乘以宽度:
0.5*(y[i+1]+y[i])*(x[i+1]-x[i])
。如果有一个固定的
dx
而不是
x
数组,则最后一个项只是一个标量。让我们重写第一个函数:

def integrate_peak0(y, x):
    """ x can be array of same size as y or a scalar """
    dx = x if x.size <= 1 else np.diff(x)
    return np.sum(0.5 * (y[1:] + y[:-1]) * dx)
如果限值总是落在
x
的精确值上,您可以直接使用
索引

def integrate_peak1(y, x, xlower, xupper):
    indices = np.searchsorted(x, [xlower, xupper])
    s = slice(indices[0], indices[1] + 1)
    return np.trapz(y[s], x[s])
由于这种情况几乎永远不会发生,您可以尝试下一种最简单的方法:四舍五入到最接近的值。您可以使用奇特的索引为可以应用到的每个潜在边界获取二维数组:

candidates
是一个2x2数组,列表示每个绑定的糖果,行表示较小和较大的候选<代码>偏移量将是您需要修改索引以获取最近邻的量。以下是积分器的一个版本,它根据积分限制选择最近的箱子:

def integrate_peak2(y, x, xlower, xupper):
    limits = np.array([xlower, xupper])
    indices = np.searchsorted(x, limits)
    candidates = x[np.stack((indices - 1, indices), axis=0)]
    indices += np.abs(candidates - limits).argmin(axis=0) - 1

    s = slice(indices[0], indices[1] + 1)
    return np.trapz(y[s], x[s])
最终版本是基于
x
y
的值进行插值。此版本可以通过以下两种方式之一实现。您可以使用适当的
x
计算目标y值并将其传递给
np.trapz
,也可以使用
integrate\u peak0
中定义的函数自行执行操作


给定一个元素
x[i]peakQ1_2500_平滑[7:33,0]和peakQ1_2500_平滑[7:33,1]
在Youngkim中
peakQ1_2500_平滑[start:end,0]
@重点是我想给出的积分极限是波数(我的x轴),而不是数组中的数值。因为某个x值在数组中并不总是相同的位置。“我认为这是非常常见的事情。”anon01添加了数组。我做了一个函数,每个波数只有一个数据点,另一个函数,然后对数据点进行运行平均。我还加了这些。虽然这与问题无关:如何利用积分极限进行数值积分。找到你想要积分的点的指数,并积分适当的切片。感谢你的详细解释。直接使用索引就足够了,因为只使用最接近的值是可以的,而且由于我在积分之前使用的是运行平均值,因此,我真正需要的是
np.searchsorted
np.argmin()
。我试着在谷歌上搜索“带极限的numpy数值积分”的所有可能的措辞,但奇怪的是没有发现任何有用的东西。我所发现的要么是与特定指数的积分,要么是与函数的分析积分。@Wulfram。我不确定哪种情况适用于你,所以我给了你所有的选择。一旦你知道外面有什么工具,情况就不会太糟了。另外,仅供参考,这里的代码都没有经过测试:)谢谢。我知道关于熊猫的事,但还没有花时间去了解它,到目前为止,我还是坚持使用numpy。奇怪的是,当我在谷歌上搜索“带极限的numpy数值积分”时,使用
np.argmin()。作为旁白:积分值中的误差似乎首先由样本数据决定,然后可能是平滑技术。在这些改进之前,我不会为了性能或准确性而使集成算法复杂化。
def integrate_peak1(y, x, xlower, xupper):
    indices = np.searchsorted(x, [xlower, xupper])
    s = slice(indices[0], indices[1] + 1)
    return np.trapz(y[s], x[s])
candidates = x[np.stack((indices - 1, indices), axis=0)]
offset = np.abs(candidates - limits).argmin(axis=0) - 1
indices += offset
def integrate_peak2(y, x, xlower, xupper):
    limits = np.array([xlower, xupper])
    indices = np.searchsorted(x, limits)
    candidates = x[np.stack((indices - 1, indices), axis=0)]
    indices += np.abs(candidates - limits).argmin(axis=0) - 1

    s = slice(indices[0], indices[1] + 1)
    return np.trapz(y[s], x[s])
def integrate_peak3a(y, x, xlower, xupper):
    limits = np.array([xlower, xupper])
    indices = np.searchsorted(x, limits)
    indices = np.stack((indices - 1, indices), axis=0)
    xi = x[indices]
    yi = y[indices]
    yn = yi[0] + np.diff(yi, axis=0) * (limits - xi[0]) / np.diff(xi, axis=0)

    indices = indices[[1, 0], [0, 1]]
    s = slice(indices[0], indices[1] + 1)
    return np.trapz(np.r_[yn[0, 0], y[s], yn[0, 1]], np.r_[xlower, x[s], xupper])
def integrate_peak3b(y, x, xlower, xupper):
    limits = np.array([xlower, xupper])
    indices = np.searchsorted(x, limits)
    indices = np.stack((indices - 1, indices), axis=0)
    xi = x[indices]
    yi = y[indices]
    yn = yi[0] + np.diff(yi, axis=0) * (limits - xi[0]) / np.diff(xi, axis=0)

    indices = indices[[1, 0], [0, 1]]
    s = slice(indices[0], indices[1] + 1)
    return np.trapz(y[s], x[s]) - 0.5 * np.diff((yn + y[indices]) * (x[indices] - limits))