- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有:
import numpy as np
position = np.array([4, 4.34, 4.69, 5.02, 5.3, 5.7, ..., 4])
x = (B/position**2)*dt
A = np.cumsum(x)
assert A[0] == 0 # I want this to be true.
其中 B
和 dt
是标量常量。这是针对初始条件为 A[0] = 0
的数值积分问题。有没有办法设置 A[0] = 0
,然后对其他所有内容执行 cumsum
?
最佳答案
我不明白你的问题到底是什么,但是你可以做一些事情来让 A[0] = 0
。
您可以将 A 创建为长一个索引以将零作为第一个条目:
# initialize example data
import numpy as np
B = 1
dt = 1
position = np.array([4, 4.34, 4.69, 5.02, 5.3, 5.7])
# do calculation
A = np.zeros(len(position) + 1)
A[1:] = np.cumsum((B/position**2)*dt)
结果:
A = [ 0. 0.0625 0.11559096 0.16105356 0.20073547 0.23633533 0.26711403]
len(A) == len(position) + 1
或者,您可以操纵计算以减去结果的第一个条目:
# initialize example data
import numpy as np
B = 1
dt = 1
position = np.array([4, 4.34, 4.69, 5.02, 5.3, 5.7])
# do calculation
A = np.cumsum((B/position**2)*dt)
A = A - A[0]
结果:
[ 0. 0.05309096 0.09855356 0.13823547 0.17383533 0.20461403]
len(A) == len(position)
如您所见,结果具有不同的长度。其中之一是您所期望的吗?
关于python - 如何使 numpy.cumsum 在第一个值之后开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27258693/
我正在尝试在 pandas 中找到一个矢量化解决方案,该解决方案在电子表格中很常见,即在基于实际 cumsum 的结果跳过或设置固定值的情况下进行 cumsum。我有以下内容: A 1 0
假设我有一个距离数组 x=[1,2,1,3,3,2,1,5,1,1]。 我想从 x 中获取索引,其中 cumsum 达到 10,在本例中,idx=[4,9]。 所以 cumsum 在满足条件后重新启动
每当超过给定的 cumsum 阈值时,我需要有关对连续行进行分组的帮助。当超过此阈值时,也应重新启动 cumsum(为零),如下所示: Index Values Regular CumS
我对 python 还很陌生。我尝试对每个客户进行累积总和,以查看相应的不活动月份(标志:1 或 0)。因此,当我们有 0 时,需要重置 1 的累积和。当我们有新客户端时,也需要重置。请参阅下面的示例
我要重置 cumsum在一个向量上,当它达到某个值时。 例如。对于以下向量: v <- c(3, 5, 2, 5, 3, 4, 5, 3, 1, 4) 预期输出为: c(0, 0, 10, 0, 0,
我正在尝试使用reduce函数在python中编写累积和的版本。这是到目前为止我的代码: from functools import reduce def my_cum_sum(arg): r
问题:给定一个二叉搜索树,其中的键是数字,我们将定义操作“cumsum”(累积和的简写),它切换树中每个节点的键所有小于或等于它的键的总和。 例如, 在这个例子中, 根中的键 5 切换为值 10:根中
我是 pandas 的新手,我可以将 cumsum 添加为 df.cumsum(axis=1) y0 y1 y2 0 2 3 4 1 2 2 3 2 0 0
我有一个正数向量,使得 - 如果数字大于或等于 1,则相加 - 如果数字小于 1,则乘以累积和。 例如 > set.seed(0) > x x [1] 3.0 0.9 0.9 1.0 3.0 0.
我想计算cumsum从每次运行信号开始的某个值,其中 signal == 1 . 示例数据: set.seed(123) df head(df,12) Date value sig
这个问题在这里已经有了答案: Cumsum with reset when 0 is encountered and by groups (2 个回答) Cumulative sum that res
dict={"asset":["S3","S2","E4","E1","A6","A8"], "Rank":[1,2,3,4,5,6],"number_of_attributes":[2,1,2,2,
这个问题在这里已经有了答案: Restart cumsum and get index if cumsum more than value (3 个答案) 关闭 2 年前。 我想在每次超过阈值 10
我想修改 cumsum 函数。我想将负值更改为 0。并且当有一个不同于 0 的新 D 值时,则使用 D-S。 下面的例子: TD % group_by(product) %>% mutate(res
是否可以在一列中迭代使用 cumsum() 并在另一列上以开始 - 停止为条件: 给定数据帧 df 和一列 X 其中值是递增的。 cumsum() 应在达到 10 或 10 的倍数时停止(例如 20、
带有数据框: df <- data.frame(id = rep(1:3, each = 5) , hour = rep(1:5, 3)
In [46]: d = np.random.randn(10, 1) * 2 In [47]: df = pd.DataFrame(d.astype(int), columns=['data'])
我目前正在尝试创建一个累积总和列,该列将根据 Game_ID 创建一个累积总和,但一次只计算与 Game_ID 相关的值。例如,玩家 A 在 Game_ID == 1 中拍摄 20 次,在 Game_
我正在尝试计算每组从最后一行到第一行的cumsum。 示例数据: t1 ts var val val2 1 a 0 2 2 a 0 2 3 a 0
我有一个矩阵,例如: A= [ 1 2 3 4 3 5 6 6 4 1 2 3 ] 我想以另一个矩阵(或数据框)的形式获取列的累积和。例如,这个矩阵会给出: B= [1 2 3 4
我是一名优秀的程序员,十分优秀!