我有一个2-Dim数组,包含给定拟合的剩余平方和(这里不重要)。
RSS[i,j] = np.sum((spectrum_theo - sp_exp_int) ** 2)我想找到矩阵元素的最小值和它在矩阵中的位置(i,j)。 找到最小元素就可以了:
RSS_min = RSS[RSS != 0].min()但对于索引,我试过:
ij_min = np.where(RSS == RSS_min)这给了我:
ij_min = (array([3]), array([20]))我想取而代之的是:
ij_min =(3,20)
如果我尝试:
ij_min = RSS.argmin()我获得:
ij_min = 0,这是一个错误的结果。
它是否存在于Scipy或其他地方的函数,可以做到吗? 我在网上搜索过,但我发现只有1-Dim阵列的答案,而不是2-D-Dim。
谢谢!
I've a 2-Dim array containing the residual sum of squares of a given fit (unimportant here).
RSS[i,j] = np.sum((spectrum_theo - sp_exp_int) ** 2)I would like to find the matrix element with the minimum value AND its position (i,j) in the matrix. Find the minimum element is OK:
RSS_min = RSS[RSS != 0].min()but for the index, I've tried:
ij_min = np.where(RSS == RSS_min)which gives me:
ij_min = (array([3]), array([20]))I would like to obtain instead:
ij_min = (3,20)
If I try :
ij_min = RSS.argmin()I obtain:
ij_min = 0,which is a wrong result.
Does it exist a function, in Scipy or elsewhere, that can do it? I've searched on the web, but I've found answers leading only with 1-Dim arrays, not 2- or N-Dim.
Thanks!
最满意答案
基于您现在所拥有的最简单的修复方法就是从数组中提取元素作为最后一步:
# ij_min = (array([3]), array([20])) ij_min = np.where(RSS == RSS_min) ij_min = tuple([i.item() for i in ij_min])The easiest fix based on what you have right now would just be to extract the elements from the array as a final step:
# ij_min = (array([3]), array([20])) ij_min = np.where(RSS == RSS_min) ij_min = tuple([i.item() for i in ij_min])更多推荐
发布评论