- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我测量了我想要在特定范围内积分的峰值。
我要整合的数据是具有波数和强度的 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-limits 集成,例如从 19559.52 到 19559.78?
def integratePeak(yvals, xvals, xlower, xupper):
'''integrate y over x from xlower to xupper'''
return I
我当然可以通过将数组元素显式引用为 peakQ1_2500_smoothened[7:33,0]
和 peakQ1_2500_smoothened[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
最佳答案
让我们先看看 np.trapz
是什么确实如此。 i
的面积第一个梯形是平均高度乘以宽度: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
已排序,您可以使用 np.searchsorted
将限制转换为索引到数据:
limits = np.array([xlower, xupper])
indices = np.searchsorted(x, limits)
如果限制始终落在 x
的精确值上, 你可以使用 indices
直接:
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])
由于几乎永远不会出现这种情况,您可以尝试下一个最简单的方法:四舍五入到最接近的值。您可以使用花式索引为您可以应用的每个潜在边界获取二维数组 np.argmin
到:
candidates = x[np.stack((indices - 1, indices), axis=0)]
offset = np.abs(candidates - limits).argmin(axis=0) - 1
indices += offset
candidates
是一个 2x2 数组,列代表每个边界的候选者,行代表较小和较大的候选者。 offset
将是您需要修改索引以获得最近邻居的数量。下面是根据积分限制选择最近的 bin 的积分器版本:
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])
最终版本是对 y
的值进行插值基于 x
.该版本可以通过两种方式之一实现。您可以计算目标 y 值并将它们传递给 np.trapz
与适当的 x
,或者您可以使用 integrate_peak0
中定义的函数自己做手术。
给定一个元素 x[i] < xn <= x[i + 1]
, 你可以估计 yn = y[i] + (y[i + 1] - y[i]) * (x[n] - x[i]) / (x[i + 1] - x[i])
.在这里,x[i]
和 x[i + 1]
是 candidates
的值如上所示。 y[i]
和 y[i + 1]
是y
的对应元素. xn
是limits
.因此,您可以通过几种不同的方式计算插值。
一种方法是将输入调整为 trapz
:
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))
当然,您可以将输入运行到 np.trapz
在integrate_peak3a
通过 integrate_peak0
中的“手动”计算.
在所有这些情况下,检查集成的限制是否在可接受的范围内并且以正确的顺序留给读者作为练习。
关于python - Numpy:具有积分限制的数值积分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64760180/
我正在处理一组标记为 160 个组的 173k 点。我想通过合并最接近的(到 9 或 10 个组)来减少组/集群的数量。我搜索过 sklearn 或类似的库,但没有成功。 我猜它只是通过 knn 聚类
我有一个扁平数字列表,这些数字逻辑上以 3 为一组,其中每个三元组是 (number, __ignored, flag[0 or 1]),例如: [7,56,1, 8,0,0, 2,0,0, 6,1,
我正在使用 pipenv 来管理我的包。我想编写一个 python 脚本来调用另一个使用不同虚拟环境(VE)的 python 脚本。 如何运行使用 VE1 的 python 脚本 1 并调用另一个 p
假设我有一个文件 script.py 位于 path = "foo/bar/script.py"。我正在寻找一种在 Python 中通过函数 execute_script() 从我的主要 Python
这听起来像是谜语或笑话,但实际上我还没有找到这个问题的答案。 问题到底是什么? 我想运行 2 个脚本。在第一个脚本中,我调用另一个脚本,但我希望它们继续并行,而不是在两个单独的线程中。主要是我不希望第
我有一个带有 python 2.5.5 的软件。我想发送一个命令,该命令将在 python 2.7.5 中启动一个脚本,然后继续执行该脚本。 我试过用 #!python2.7.5 和http://re
我在 python 命令行(使用 python 2.7)中,并尝试运行 Python 脚本。我的操作系统是 Windows 7。我已将我的目录设置为包含我所有脚本的文件夹,使用: os.chdir("
剧透:部分解决(见最后)。 以下是使用 Python 嵌入的代码示例: #include int main(int argc, char** argv) { Py_SetPythonHome
假设我有以下列表,对应于及时的股票价格: prices = [1, 3, 7, 10, 9, 8, 5, 3, 6, 8, 12, 9, 6, 10, 13, 8, 4, 11] 我想确定以下总体上最
所以我试图在选择某个单选按钮时更改此框架的背景。 我的框架位于一个类中,并且单选按钮的功能位于该类之外。 (这样我就可以在所有其他框架上调用它们。) 问题是每当我选择单选按钮时都会出现以下错误: co
我正在尝试将字符串与 python 中的正则表达式进行比较,如下所示, #!/usr/bin/env python3 import re str1 = "Expecting property name
考虑以下原型(prototype) Boost.Python 模块,该模块从单独的 C++ 头文件中引入类“D”。 /* file: a/b.cpp */ BOOST_PYTHON_MODULE(c)
如何编写一个程序来“识别函数调用的行号?” python 检查模块提供了定位行号的选项,但是, def di(): return inspect.currentframe().f_back.f_l
我已经使用 macports 安装了 Python 2.7,并且由于我的 $PATH 变量,这就是我输入 $ python 时得到的变量。然而,virtualenv 默认使用 Python 2.6,除
我只想问如何加快 python 上的 re.search 速度。 我有一个很长的字符串行,长度为 176861(即带有一些符号的字母数字字符),我使用此函数测试了该行以进行研究: def getExe
list1= [u'%app%%General%%Council%', u'%people%', u'%people%%Regional%%Council%%Mandate%', u'%ppp%%Ge
这个问题在这里已经有了答案: Is it Pythonic to use list comprehensions for just side effects? (7 个答案) 关闭 4 个月前。 告
我想用 Python 将两个列表组合成一个列表,方法如下: a = [1,1,1,2,2,2,3,3,3,3] b= ["Sun", "is", "bright", "June","and" ,"Ju
我正在运行带有最新 Boost 发行版 (1.55.0) 的 Mac OS X 10.8.4 (Darwin 12.4.0)。我正在按照说明 here构建包含在我的发行版中的教程 Boost-Pyth
学习 Python,我正在尝试制作一个没有任何第 3 方库的网络抓取工具,这样过程对我来说并没有简化,而且我知道我在做什么。我浏览了一些在线资源,但所有这些都让我对某些事情感到困惑。 html 看起来
我是一名优秀的程序员,十分优秀!