简体   繁体   English

在Python中,如何获取2D数组中“列”的最大值?

[英]In Python how do I get the max value of a 'column' in a 2D array?

In Python how do I get the max value of a 'column' in a 2D array? 在Python中,如何获取2D数组中“列”的最大值?

I can see that trainingData has a max and min which is the max and min values of the entire 2D array but I would like the max value for each "column". 我可以看到trainingData具有最大值和最小值,这是整个2D数组的最大值和最小值,但是我希望每个“列”的最大值。

I've tried several things and this is my last attempt but it didn't seem to work: 我已经尝试了几件事,这是我的最后一次尝试,但是似乎没有用:

creating it: 创建它:

trainingData = []

filling it: 填充它:

for row in csv_reader:
    trainingData.append(row)

converting to np 转换为np

npTrainingData = np.array(trainingData)

printing it: 打印:

for x in range(1, 12):
    print(np.max(npTrainingData[:x]))

I've scoured stack overflow but nobody seems to solve this at all but have a ton of similar questions. 我已经检查了堆栈溢出,但是似乎没有人解决所有问题,但是有很多类似的问题。

Thanks, John 谢谢,约翰

instead of explicitly iterating over the elements (or sub-arrays) of an array like 而不是显式遍历数组的元素(或子数组),例如

for x in range(1, 12):
    print(np.max(npTrainingData[:x]))

there are numpy functions, which can be controlled regarding on which axis they are applied. 有一些numpy函数,可以对其应用在哪个轴上进行控制。 Eg np.max : 例如np.max

np.max(npTrainingData, 0)

or equivalently 或同等

npTrainingData.max(0)

where the 0 here is the axis parameter, which can be 0 or 1 for 2D-arrays. 其中0axis参数,对于2D数组可以为0或1。
When left blank, the maximum of the flattened array, ie a single value for the whole dataset is returned. 当留空时,将返回展平数组的最大值,即整个数据集的单个值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM