python - 使用numpy从二维数组的行之和中找到数组的最大值
问题描述
我正在使用 pyqt5 创建一个 GUI 应用程序。但我无法获得最大值。为什么此代码打印最后一行值而不是最大值。
这是代码:
def submit(self):
rowCount = self.tableWidgetInput.rowCount()
columnCount = self.tableWidgetInput.columnCount()
if (rowCount==columnCount):
size=rowCount
print("The size of the matrxi is %d * %d "%(size,size))
print("The Given matrxi is", "SUM of Row" )
for row in range(size):
rowData =[]
for column in range (size):
widegetItem = self.tableWidgetInput.item(row,column)
if(widegetItem and widegetItem.text):
rowData.append(widegetItem.text())
else:
rowData.append('NULL')
inputArray =[]
inputArray = np.array(rowData,dtype=np.float64)
#sumofRow = np.sum(inputArray, axis = 0,dtype='float')
sumofRow = inputArray.sum(axis = 0,dtype='float')
maxInRows = np.amax(sumofRow,axis=0)
array_sum_max = max([np.sum(x,axis=0) for x in inputArray])
print(rowData, sumofRow)
print("The Maximum in the sum of row is " )
print( sumofRow )
else:
print("The input is not a Square matrix")
print("Data is not Submitted Sucessfully")
我想使用 numpy 在 Pyqt5 表小部件数据中找到总和或行,然后是其最大值。
这是输出:
Size of the Matrix is Increased
The size of the matrxi is 3 * 3
The Given matrxi is SUM of Row
[5. 5. 5.] 15.0
[1. 1. 1.] 3.0
[2. 2. 2.] 6.0
The Maximum in the sum of row is
6.0
解决方案
我认为你重写了 sumofRow 的值。例如,您可以创建另一个名为 max_sum 的变量并执行以下操作:
if max_sum>=sumofRow:
max_sum = sumofRow
最后,您可以使用该 max_sum 变量来获得最大值。
推荐阅读
- javascript - 在模态上单击 x 时如何停止网络摄像头流
- ios - 如何在容器(UIView)中居中对齐 2 个 UIView,而不将它们放在 UIView 中
- kubernetes-ingress - 如何更新 ngnix 入口控制器的 ngnix 配置
- ember.js - @import 不能在带有 ember-cli-sass 的插件中工作
- python-3.x - 清除 tkinter 中的文本框
- python - 检查关键字是否存在于字符串列中
- javascript - 从javascript中的2个数组对象中获取不匹配的数据
- php - 我有问题从mysql按年分组数据值
- vim - Vim 获取/设置缓冲区名称而不输入缓冲区
- notepad++ - Notepad++ - 将来自 SQL Server 的大查询变成一行