如何对NumPy数组中特定列的每个元素乘以一个标量?
我需要对一个来自水文地质领域的大数据集进行一些分析。我正在使用NumPy。我想知道我该如何:
把我的数组中的第二列乘以一个数字(比如5.2)。然后
计算那一列数字的累计和。
正如我提到的,我只想处理特定的列,而不是整个数组。
3 个回答
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.]])
同样的,我们也可以对任何列进行这样的操作。希望这样能让你更明白。
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)
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,:]则表示第四行的所有元素