识别数字急剧下降点的方法

2024-05-15 06:20:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一系列的数字:

numbers = [100, 101, 99, 102, 99, 98, 100,  97.5, 98, 99, 95, 93, 90, 85, 80]

plot of numbers

用肉眼很容易看出,数字开始急剧下降,大约在10左右,但是有没有一种简单的方法来确定x轴上的那个点(或接近它的点)?你知道吗

这是在retrospect中完成的,因此可以使用整个数字列表来选择衰减加速的x轴点。你知道吗

首选Python解决方案,但伪代码或通用方法也可以。你知道吗


Tags: 方法代码列表数字解决方案numbers轴点肉眼
1条回答
网友
1楼 · 发布于 2024-05-15 06:20:37

好吧,这符合我的需要。我从t分布计算一个运行平均值、标准偏差和cdf,告诉我每个连续值的可能性有多大。你知道吗

这只适用于减少,因为我只检查cdf<;0.05,但效果非常好。你知道吗

import numpy as np
from scipy import stats
import matplotlib.pyplot as plt

numbers = np.array([100, 101, 99, 102, 99, 98, 100,  97.5, 98, 99, 95, 93, 90, 85, 80])

# Calculate a running mean
cum_mean = numbers.cumsum() / (np.arange(len(numbers)) + 1)

# Calculate a running standard deviation
cum_std = np.array([numbers[:i].std() for i in range(len(numbers))])

# Calculate a z value 
cum_z =  (numbers[1:] - cum_mean[:-1]) / cum_std[:-1]

# Add in NA vals to account for records without sample size
z_vals = np.concatenate((np.zeros(1+2), cum_z[2:]), axis=0)

# Calculate cdf 
cum_t = np.array([stats.t.cdf(z, i) for i, z in enumerate(z_vals)])

# Identify first number to fall below threshold
first_deviation = np.where(cum_t < 0.05)[0].min()

fig, ax = plt.subplots()

# plot the numbers and the point immediately prior to the decrease
ax.plot(numbers)
ax.axvline(first_deviation-1, color='red')

numbers with drop detected

相关问题 更多 >

    热门问题