如何使用熊猫或Numpy计算离散时间序列的“倍增时间”?

下面是一个数据集。每一行都是一个时间片。第一列是读数。第二个是多少个时间段前的读数是它的50%。我通过盯着它来手工计算它,所以数字并不完全正确。


197 

218 

256 

328     4

413     4

525     4

646     4

777     5

1159    4

1838    2

2417    2

3240    2.5

4257    3

4955    4

5752    5.5

6620    5

7738    5.5

8966    4.5

10402   5

因此,假设我有一个数据帧,如下所示:


df = pd.DataFrame({'val': [197,218,256,328,413,525,646,777,1159,1838,2417,3240,4257,4955,5752,6620,7738,8966,10402]})

如何计算 df.倍增?我可以想象从最后开始,向后工作,每次扫描一个值的50%。但是有更好的方法。我认为这与Log2有关,但不知道该怎么做!


慕莱坞森
浏览 59回答 3
3回答

MM们

您是否正在将Covid-19感染时间加倍?请仔细检查结果。我忘了你正在使用熊猫,所以你可能需要这个:y = df['val'].to_numpy()这是第一次拍摄:import numpy as npfrom scipy.interpolate import interp1dy = np.array([197, 218, 256, 328, 413,525, 646, 646, 777,              1159, 1838, 2417, 3240, 4257, 4955, 4955,              5752, 6620, 7738, 8966, 10402],              dtype=float)# get the deltas to check if there was no increase# between two consecutive data points        dy = np.diff(y)# these are the places without increaseidx = np.argwhere(dy) #could also be np.where(dy == 0.0)[0]y_fixed = y.copy()# create the x axis, probably days x = np.arange(y.shape[0])# Hack: increase the second identical value be a# small amount so the interpolation works# increase the indices by one to increment the second valuey_fixed[idx + 1] += 0.001# you need scipy > 0.17 for extrapolation to workf = interp1d(y_fixed, x, fill_value="extrapolate")# there are the values you need?y_half = y / 2.0# get the according x values by interpolationx_interp = f(y_half)# delta between the current day and the date when# the value was halfdbl = x - x_interp# this already looks quite good, but double check!print(dbl)也许 x 轴需要移动。或者也许它毕竟是正确的。我明天会用新鲜的大脑来思考这个问题。下图显示了两种算法,其中包含计算出的指数数据,其中两个位置设置为非递增值。

不负相思意

可能最终会看起来像这样。ACCURACY = 0cases = [197, 218, 256, 328, 413,525, 646, 646, 777,          1159, 1838, 2417, 3240, 4257, 4955, 4955,          5752, 6620, 7738, 8966, 10402]doubling = []for t in range(len(cases)):    found = False    for t_2 in range(t):        if cases[t_2] - (cases[t] // 2) > ACCURACY:            doubling.append(t - t_2)            found = True            break    # Append nothing if value not found    if not found:        doubling.append(None)

拉丁的传说

我正在处理这些数据。这是一个老派的解决方案。我跟踪了您的解决方案,但无法完全遵循它。但是我的不是那么优雅,但我认为这是正确的...而且图表看起来与您的图表非常相似...!import numpy as npreadings = np.array([197, 218, 256, 328, 413,525, 646, 646, 777,          1159, 1838, 2417, 3240, 4257, 4955, 4955,          5752, 6620, 7738, 8966, 10402],          dtype=float)   readingsLength = len(readings)double = np.zeros(readingsLength)for i in range( readingsLength - 1, -1, -1):    target = readings[i]    count = 0    for j in range(i, -1, -1):        diffsofar = target-readings[j]        exact = target / 2        if diffsofar  > exact:            f = (exact - readings[j]) / (readings[j]-readings[j+1]) + count            double[i] = f            break        else:            count = count+1print(double)  
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python