从向量中减去向量并在Python

时间:2018-04-25 08:29:34

标签: python vector subtraction

我的模型中每年都有易感区域的矢量和新感染区域的矢量。行表示不同的人口年龄,列表示不同的年份。我试图通过使用np.roll来“老化”敏感的向量。在流行病的情况下,我需要减去新感染的区域向量并“老化”剩余部分。在早期的草案中,这不是问题,但是,由于某些原因它不再起作用并导致

__main__:172: SettingWithCopyWarning:  A value is trying to be set on a copy of a slice from a DataFrame

我想要做的一个小例子是

import numpy as np
import pandas as pd

years = range(10) 
# Vector1 illustrates the susceptible individuals (n) indicates the different ages
vector1 = [100 for n in range(100)]  
# Vector2 illustrates the newly infected individuals across ages. This constant should be subtracted for every year from the remainder at t-1.
vector2 = [10 for n in range(100)] 
# In this df, I would like to store the remainder of susceptible individuals. Rows indicate the possible ages (n), columns the point in time (t).
df = pd.DataFrame([[0 for t in years] for n in range(100)])

for n in range(100):
    for t in years:
        # In the first year, no infection occurs and the column is just the vector of susceptible individuals
        if t == 0:
            df[n] = vector1 
        # In t+1 the epidemic starts and for every year I want to subtract the newly infected individuals (vector2) from the vector of susceptible (and not yet infected) individuals which I have calculated at t-1
        elif t-1 >= 0:
            df[t][n] = np.roll(np.subtract(df[t-1][n], vector2[n]), shift=1)

1 个答案:

答案 0 :(得分:0)

我尝试了你的代码,它正在我的机器上工作。然而,这是一种警告,你得到了。它不会阻止你的程序。

https://www.dataquest.io/blog/settingwithcopywarning/

这是一个很好的读取警告