【问题标题】:How are discrete time systems (with negative indices) represented in numpy?numpy 中如何表示离散时间系统(具有负索引)?
【发布时间】:2021-10-09 13:29:48
【问题描述】:

所以我试图用 numpy 表示离散时间系统,这意味着数组中的某些元素可能具有负索引,例如。

n x[n]
-2 4
-1 5
0 3
1 1

对于上述数组,x[-2] = 4x[-1] = 5。这有点像字典。有没有办法在 numpy 中实现这一点?我知道我可以在进行操作时将所有内容移动 2 个位置并将其移回,我只是想知道是否还有其他理想的处理方式。

【问题讨论】:

  • numpy 中,索引纯粹是位置性的。您可能需要维护一个单独的索引数组。这就是pandas 所做的。

标签: python numpy discrete-mathematics


【解决方案1】:

大熊猫正是为了这种事情。它建立在 numpy 之上,在很多方面都非常相似,应该做你想做的事。

import pandas as pd

index = [-2, -1, 0, 1]
data = [4, 5, 3, 1]

df = pd.DataFrame({'x[n]':data},index=index)

给你

    x[n]
-2     4
-1     5
 0     3
 1     1

你可以像使用 numpy 一样使用它,例如

df.loc[-2:-1]

给你

    x[n]
-2     4
-1     5

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-21
    • 2021-01-20
    相关资源
    最近更新 更多