gpt4 book ai didi

python - 找到我的数据中斜率变化的位置作为可以轻松索引和提取的参数

转载 作者:行者123 更新时间:2023-12-05 02:30:52 30 4
gpt4 key购买 nike

我有以下数据:

0.8340502011561366 0.84234916002189220.85134560216544670.84581923885530840.84401112760141950.84895896714231430.87380881204919720.88451299007052790.89882989989266880.9246339646926930.95447907340651570.99080344312468751.02364304665431381.0616197730279151.10500382498354141.13714498024901261.19211826103713681.27522076590225761.3440476202551761.41981173506683531.5079430671437411.6221379682037451.68140984295020851.76468100542805951.84854574357756941.9195911247575541.98431442205931452.0301580146402262.0181841224761752.03234660126242072.01792004090238742.03169329508537232.0136838700898982.030107035065142.02161516237269772.0388554677865052.04539235224660932.037590316427532.0194249967522782.04418061064286062.06075213694151362.0593100673183732.06611579751624852.0532164295398642.07151239712255642.05804734133620752.0558145127217122.08082785606889642.06016370293771132.05394293651560032.06096486135137542.05851357126126462.0876746258144532.0624829619666472.0664761002107772.05684441789449672.05879039432822662.0506399365756396

绘制的数据如下所示:

Plotted data

我想找到斜率符号变化的点(我用黑色圈了它。应该在索引 26 左右):

Point I am trying to extract

我需要为数百个文件找到这个更改点。到目前为止,我尝试了这篇文章中的建议:

Finding the point of a slope change as a free parameter- Python

我认为由于我的数据有点嘈杂,所以我无法在斜率变化中实现平滑过渡。

这是我到目前为止尝试过的代码:

import numpy as np

#load 1-D data file
file = str(sys.argv[1])
y = np.loadtxt(file)

#create X based on file length
x = np.linspace(1,len(y), num=len(y))

Find first derivative:

m = np.diff(y)/np.diff(x)
print(m)

#Find second derivative
b = np.diff(m)
print(b)
#find Index

index = 0
for difference in b:
index += 1
if difference < 0:
print(index, difference)

因为我的数据有噪音,所以我在我想要的索引之前得到了一些负值。在这种情况下,我希望它检索的索引大约是 26(这是我的数据变为常量的地方)。有人对我可以做些什么来解决这个问题有什么建议吗?谢谢!

最佳答案

在这种情况下,梯度方法是无用的,因为您不关心速度或向量场。梯度的知识不会添加额外的信息来定位最大值,因为 run 总是正的,因此不会影响梯度的符号。建议使用完全基于raise 的方法。

检测数据正在减少的索引,找到它们与最大值的位置之间的差异。然后通过索引操作,您可以找到数据具有最大值的值。

data = '0.8340502011561366 0.8423491600218922 0.8513456021654467 0.8458192388553084 0.8440111276014195 0.8489589671423143 0.8738088120491972 0.8845129900705279 0.8988298998926688 0.924633964692693 0.9544790734065157 0.9908034431246875 1.0236430466543138 1.061619773027915 1.1050038249835414 1.1371449802490126 1.1921182610371368 1.2752207659022576 1.344047620255176 1.4198117350668353 1.507943067143741 1.622137968203745 1.6814098429502085 1.7646810054280595 1.8485457435775694 1.919591124757554 1.9843144220593145 2.030158014640226 2.018184122476175 2.0323466012624207 2.0179200409023874 2.0316932950853723 2.013683870089898 2.03010703506514 2.0216151623726977 2.038855467786505 2.0453923522466093 2.03759031642753 2.019424996752278 2.0441806106428606 2.0607521369415136 2.059310067318373 2.0661157975162485 2.053216429539864 2.0715123971225564 2.0580473413362075 2.055814512721712 2.0808278560688964 2.0601637029377113 2.0539429365156003 2.0609648613513754 2.0585135712612646 2.087674625814453 2.062482961966647 2.066476100210777 2.0568444178944967 2.0587903943282266 2.0506399365756396'

data = data.split()
import numpy as np

a = np.array(data, dtype=float)

diff = np.diff(a)

neg_indeces = np.where(diff<0)[0]
neg_diff = np.diff(neg_indeces)

i_max_dif = np.where(neg_diff == neg_diff.max())[0][0] + 1

i_max = neg_indeces[i_max_dif] - 1 # because aise as a difference of two consecutive values

print(i_max, a[i_max])

输出

26 1.9843144220593145

一些细节

print(neg_indeces) # all indeces of the negative values in the data
# [ 2 3 27 29 31 33 36 37 40 42 44 45 47 48 50 52 54 56]
print(neg_diff) # difference between such indices
# [ 1 24 2 2 2 3 1 3 2 2 1 2 1 2 2 2 2]
print(neg_diff.max()) # value with highest difference
# 24
print(i_max_dif) # location of the max index of neg_indeces -> 27
# 2
print(i_max) # index of the max of the origonal data
# 26

关于python - 找到我的数据中斜率变化的位置作为可以轻松索引和提取的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71743487/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com