使用数据文件中的多列
Working with multiple columns from a data file
我有一个文件,我需要在其中使用第一列。剩余的列需要相对于第一列进行整合。假设我的文件如下所示:
100 1.0 1.1 1.2 1.3 0.9
110 1.8 1.9 2.0 2.1 2.2
120 1.8 1.9 2.0 2.1 2.2
130 2.0 2.1 2.3 2.4 2.5
我能否编写一段代码,将第二列与第一列集成,然后与第三列集成,然后再与第一列集成,依此类推?对于我的代码,我有:
import scipy as sp
first_col=dat[:,0] #first column from data file
cols=dat[:,1:] #other columns from data file
col2 = cols[:,0] # gets the first column from variable cols
I = sp.integrate.cumtrapz(col2, first_col, initial = 0) #integration step
这仅适用于变量 col 的第一行,但是,我不想为所有其他列写出它,它看起来像是在讨论(想到它让我不寒而栗)。我见过类似的问题,但无法将答案与我的答案联系起来,而那些或多或少相同的问题的答案也很模糊。有什么想法吗?
函数 cumtrapz
接受一个 axis
参数。例如,假设您将第一列放在 x
中,将其余列放在 y
中,并且它们具有以下值:
In [61]: x
Out[61]: array([100, 110, 120, 130])
In [62]: y
Out[62]:
array([[ 1.1, 2.1, 2. , 1.1, 1.1],
[ 2. , 2.1, 1. , 1.2, 2.1],
[ 1.2, 1. , 1.1, 1. , 1.2],
[ 2. , 1.1, 1.2, 2. , 1.2]])
您可以针对 x
整合 y
的每一列,如下所示:
In [63]: cumtrapz(y, x=x, axis=0, initial=0)
Out[63]:
array([[ 0. , 0. , 0. , 0. , 0. ],
[ 15.5, 21. , 15. , 11.5, 16. ],
[ 31.5, 36.5, 25.5, 22.5, 32.5],
[ 47.5, 47. , 37. , 37.5, 44.5]])
我有一个文件,我需要在其中使用第一列。剩余的列需要相对于第一列进行整合。假设我的文件如下所示:
100 1.0 1.1 1.2 1.3 0.9
110 1.8 1.9 2.0 2.1 2.2
120 1.8 1.9 2.0 2.1 2.2
130 2.0 2.1 2.3 2.4 2.5
我能否编写一段代码,将第二列与第一列集成,然后与第三列集成,然后再与第一列集成,依此类推?对于我的代码,我有:
import scipy as sp
first_col=dat[:,0] #first column from data file
cols=dat[:,1:] #other columns from data file
col2 = cols[:,0] # gets the first column from variable cols
I = sp.integrate.cumtrapz(col2, first_col, initial = 0) #integration step
这仅适用于变量 col 的第一行,但是,我不想为所有其他列写出它,它看起来像是在讨论(想到它让我不寒而栗)。我见过类似的问题,但无法将答案与我的答案联系起来,而那些或多或少相同的问题的答案也很模糊。有什么想法吗?
函数 cumtrapz
接受一个 axis
参数。例如,假设您将第一列放在 x
中,将其余列放在 y
中,并且它们具有以下值:
In [61]: x
Out[61]: array([100, 110, 120, 130])
In [62]: y
Out[62]:
array([[ 1.1, 2.1, 2. , 1.1, 1.1],
[ 2. , 2.1, 1. , 1.2, 2.1],
[ 1.2, 1. , 1.1, 1. , 1.2],
[ 2. , 1.1, 1.2, 2. , 1.2]])
您可以针对 x
整合 y
的每一列,如下所示:
In [63]: cumtrapz(y, x=x, axis=0, initial=0)
Out[63]:
array([[ 0. , 0. , 0. , 0. , 0. ],
[ 15.5, 21. , 15. , 11.5, 16. ],
[ 31.5, 36.5, 25.5, 22.5, 32.5],
[ 47.5, 47. , 37. , 37.5, 44.5]])