如何在NumPy数组的特定列中将标量乘以每个元素?

35

我需要对一份大型水文地质领域的数据集进行分析。我正在使用NumPy。我想知道如何:

  1. 将数组的第2列乘以一个数字(例如5.2)。并且

  2. 计算该列数字的累加和。

正如我所提到的,我只想处理特定的列而不是整个数组。

3个回答

43
 you can do this in two simple steps using NumPy:

>>> # multiply column 2 of the 2D array, A, by 5.2
>>> A[:,1] *= 5.2

>>> # assuming by 'cumulative sum' you meant the 'reduced' sum:
>>> A[:,1].sum()

>>> # if in fact you want the cumulative sum (ie, returns a new column)
>>> # then do this for the second step instead:
>>> NP.cumsum(A[:,1])

使用一些模拟数据:

>>> A = NP.random.rand(8, 5)
>>> A
  array([[ 0.893,  0.824,  0.438,  0.284,  0.892],
         [ 0.534,  0.11 ,  0.409,  0.555,  0.96 ],
         [ 0.671,  0.817,  0.636,  0.522,  0.867],
         [ 0.752,  0.688,  0.142,  0.793,  0.716],
         [ 0.276,  0.818,  0.904,  0.767,  0.443],
         [ 0.57 ,  0.159,  0.144,  0.439,  0.747],
         [ 0.705,  0.793,  0.575,  0.507,  0.956],
         [ 0.322,  0.713,  0.963,  0.037,  0.509]])

>>> A[:,1] *= 5.2

>>> A
  array([[ 0.893,  4.287,  0.438,  0.284,  0.892],
         [ 0.534,  0.571,  0.409,  0.555,  0.96 ],
         [ 0.671,  4.25 ,  0.636,  0.522,  0.867],
         [ 0.752,  3.576,  0.142,  0.793,  0.716],
         [ 0.276,  4.255,  0.904,  0.767,  0.443],
         [ 0.57 ,  0.827,  0.144,  0.439,  0.747],
         [ 0.705,  4.122,  0.575,  0.507,  0.956],
         [ 0.322,  3.71 ,  0.963,  0.037,  0.509]])

>>> A[:,1].sum()
  25.596156138451427

了解NumPy中的元素选择(索引)只需要遵循几个简单的规则:

  • NumPy和Python一样是从0开始计数,所以例如下面的“1”表示第二列

  • 方括号内用逗号分隔维度,如[行,列],例如A [2,3] 表示第三行第四列的项目(“单元格”)

  • 冒号表示沿着该维度的所有元素,例如A [:, 1] 创建了A的第二列视图; A [3,:] 指的是第四行


@Mary Jane:不用谢。顺便说一句,这是一个很棒的简短教程:http://www.scipy.org/Cookbook/BuildingArrays。如果你发现其中某个答案比其他答案更有帮助,请通过点击“勾选标记”来接受该答案,当你将鼠标悬停在每个用户的分数上时,该标记会显示出来。 - doug
当你想要进行除法运算时,需要使用//而不是/ - 飞走了。 - mrk

8

当然:

import numpy as np
# Let a be some 2d array; here we just use dummy data 
# to illustrate the method
a = np.ones((10,5))
# Multiply just the 2nd column by 5.2 in-place
a[:,1] *= 5.2

# Now get the cumulative sum of just that column
csum = np.cumsum(a[:,1])

如果您不想原地进行此操作,您需要采用稍微不同的策略:

b = 5.2*a[:,1]
csum = np.cumsum(b)

第二个片段(b = 5.2a[:,1])不会按预期工作,因为a[:,1]是一个一维数组。为了保留第一列的值,你可以这样做:b = anp.array([1, 5.2])。 - undefined

0

要将常数与特定列或行相乘:

import numpy as np;
X=np.ones(shape=(10,10),dtype=np.float64);
X;

### this is our default matrix
array([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
   [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
   [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
   [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
   [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
   [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
   [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
   [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
   [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
   [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])



 ## now say we want to multiple it with 10

 X=X*10;

array([[10., 10., 10., 10., 10., 10., 10., 10., 10., 10.],
   [10., 10., 10., 10., 10., 10., 10., 10., 10., 10.],
   [10., 10., 10., 10., 10., 10., 10., 10., 10., 10.],
   [10., 10., 10., 10., 10., 10., 10., 10., 10., 10.],
   [10., 10., 10., 10., 10., 10., 10., 10., 10., 10.],
   [10., 10., 10., 10., 10., 10., 10., 10., 10., 10.],
   [10., 10., 10., 10., 10., 10., 10., 10., 10., 10.],
   [10., 10., 10., 10., 10., 10., 10., 10., 10., 10.],
   [10., 10., 10., 10., 10., 10., 10., 10., 10., 10.],
   [10., 10., 10., 10., 10., 10., 10., 10., 10., 10.]])

### Now if, we want to mulitply 3,5, 7 column with 5

X[:,[3,5,7]]=X[:,[3,5,7]]*5

 array([[10., 10., 10., 50., 10., 50., 10., 50., 10., 10.],
   [10., 10., 10., 50., 10., 50., 10., 50., 10., 10.],
   [10., 10., 10., 50., 10., 50., 10., 50., 10., 10.],
   [10., 10., 10., 50., 10., 50., 10., 50., 10., 10.],
   [10., 10., 10., 50., 10., 50., 10., 50., 10., 10.],
   [10., 10., 10., 50., 10., 50., 10., 50., 10., 10.],
   [10., 10., 10., 50., 10., 50., 10., 50., 10., 10.],
   [10., 10., 10., 50., 10., 50., 10., 50., 10., 10.],
   [10., 10., 10., 50., 10., 50., 10., 50., 10., 10.],
   [10., 10., 10., 50., 10., 50., 10., 50., 10., 10.]])

同样地,我们可以对任何列进行操作。 希望这能澄清问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接