本文介绍了如何在 NumPy 数组中的特定列中乘以标量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要对来自水文地质实地工作的大型数据集进行一些分析.我正在使用 NumPy.我想知道我该怎么做:

  1. 乘以例如我的数组的第二列按一个数字(例如 5.2).然后

  2. 计算该列中数字的累积总和.

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

解决方案
 你可以使用 NumPy 通过两个简单的步骤来完成:>>># 将二维数组 A 的第 2 列乘以 5.2>>>A[:,1] *= 5.2>>># 假设累积总和"是指减少"的总和:>>>A[:,1].sum()>>># 如果实际上你想要累积总和(即,返回一个新列)>>># 然后在第二步执行此操作:>>>NP.cumsum(A[:,1])

一些模拟数据:

>>>A = NP.random.rand(8, 5)>>>一种数组([[ 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>>>一种数组([[ 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 的第 2 列的视图;A[3,:] 指的是第四行

I need to do some analysis on a large dataset from a hydrolgeology field work. I am using NumPy. I want to know how I can:

  1. multiply e.g. the 2nd column of my array by a number (e.g. 5.2). And then

  2. calculate the cumulative sum of the numbers in that column.

As I mentioned I only want to work on a specific column and not the whole array.

解决方案
 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])


with some mocked data:

>>> 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


just a few simple rules are required to grok element selection (indexing) in NumPy:

  • NumPy, like Python, is 0-based, so eg, the "1" below refers to the second column

  • commas separate the dimensions inside the brackets, so [rows, columns], eg, A[2,3] means the item ("cell") at row three, column four

  • a colon means all of the elements along that dimension, eg, A[:,1] creates a view of A's column 2; A[3,:] refers to the fourth row

这篇关于如何在 NumPy 数组中的特定列中乘以标量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 19:33