返回与未排序多维数组 A 中的 B 最接近的数字的函数? [英] Function that returns the closest number to B that's in an UNSORTED multidimensional array, A?

查看:43
本文介绍了返回与未排序多维数组 A 中的 B 最接近的数字的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所述,我想创建一个函数,它接受一个多维数组 A 和一个数字 B,最终返回 A 中与 B 最接近的数字.如果数字 B 在 A 中,然后返回它.如果 A 中有 2 个数字与 B 的距离相等,则通过逐行计数来选择第一个.

As the title states, I want to create a function that'll take a multidimensional array A, and a number B, that ultimately returns the number in A that is the closest to B. If the number B is in A, then return it. If there's 2 numbers in A that are equally distant from B, choose the first one by counting from row to row.

这是我目前的代码:

import numpy as np
def g_C(A,B):
  A = np.asanyarray(A)
  assert A.ndim == 2 # to assert that A is a multidimensional array.
  get = (np.abs(A-B)).argmin()
  return (A[get])

但是根据我的理解,我认为 (np.abs(M-N)).argmin() 真的只对排序数组有效吗?我不允许在这个问题中对数组进行排序;我必须处理它的表面价值,逐行检查,并抓取最接近 B 的数字的第一个实例.

However from my understanding, I think (np.abs(M-N)).argmin() really only effectively works for sorted arrays? I'm not allowed to sort the array in this problem; I have to work on it for face value, examining row by row, and grabbing the first instance of the closest number to B.

例如,g_C([[1,3,6,-8],[2,7,1,0],[4,5,2,8],[2,3,7,10]],9) 应该返回 8

另外,我得到了 numpy.argmin 会有所帮助的提示,我看到它的目的是提取第一次发生的事情,这在这个问题中是有道理的,但我不确定如何准确地将其融入我目前拥有的代码中.

Also, I was given the hint that numpy.argmin would help, and I see that it's purpose is to extract the first occurrence something occurs, which makes sense in this problem, but I'm not sure how exactly to fit that into the code I have at the moment.

编辑平坦的建议工作得很好.谢谢大家.

EDIT The flat suggestion works perfectly fine. Thank you everyone.

我正在尝试 RagingRoosevelt 的第二个建议,但我被卡住了.

I'm trying RagingRoosevelt's second suggestion, and I'm stuck.

def g_C(A,B):
  A = np.asanyarray(A)
  D = np.full_like(A, B) # created an array D with same qualities as array A, but just filled with values of B
  diffs = abs(D-A) # finding absolute value differences between D and A
  close = diffs.argmin(axis=1) # find argmin of 'diffs', row by row
  close = np.asanyarray(close) # converted the argmins of 'diff' into an array
  closer = close.argmin() # the final argmin ??
  return closer

我正在尝试这个建议,因为我有另一个与此相关的问题,我必须提取总和最接近 B 的行.我认为无论如何这是一个很好的做法.

I'm trying out this suggestion because I have another problem related to this where I have to extract the row who's sum is the closest number to B. And I figure this is good practice anyway.

推荐答案

您现有的代码很好,除了默认情况下 argmin 返回扁平数组的索引.所以你可以这样做

Your existing code is fine except, by default, argmin returns an index to the flattened array. So you could do

return A.flat[abs(A - B).argmin()]

从 A 中获取正确的值.

to get the right value from A.

对于你的另一个问题 - 在二维数组 A 中找到总和最接近 B 的行 - 你可以这样做:

For your other problem - finding the row in a 2-dimensional array A whose sum is closest to B - you can do:

return A[abs(A.sum(axis=1) - B).argmin()]

在任何一种情况下,我都认为不需要创建 B 数组.

In either case I don't see any need to create an array of B.

这篇关于返回与未排序多维数组 A 中的 B 最接近的数字的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆