首页 > 解决方案 > 使用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

标签: pythonarrayslistnumpypyqt5

解决方案


我认为你重写了 sumofRow 的值。例如,您可以创建另一个名为 max_sum 的变量并执行以下操作:

if max_sum>=sumofRow:
    max_sum = sumofRow

最后,您可以使用该 max_sum 变量来获得最大值。


推荐阅读