如何通过numpy数组找到最小MSE的值?

时间:2019-03-02 22:49:36

标签: python numpy mean-square-error

我可能的值是:

0: [0 0 0 0]
1: [1 0 0 0]
2: [1 1 0 0]
3: [1 1 1 0]
4: [1 1 1 1]

我有一些值:

[[0.9539342  0.84090066 0.46451256 0.09715253],
 [0.9923432  0.01231235 0.19491441 0.09715253]
 ....

我想弄清楚哪个值与我的新值最接近。理想情况下,我想避免执行for循环并想知道是否存在某种矢量化方法来搜索最小均方误差?

我希望它返回一个类似于[2, 1 ....

的数组

3 个答案:

答案 0 :(得分:1)

让我们假设您的输入数据是字典。然后,您可以将NumPy用于矢量化解决方案。首先,将输入列表转换为NumPy数组,并使用axis=1参数将其转换为RMSE。

# Input data
dicts = {0: [0, 0, 0, 0], 1: [1, 0, 0, 0], 2: [1, 1, 0, 0], 3: [1, 1, 1, 0],4: [1, 1, 1, 1]}
new_value = np.array([0.9539342, 0.84090066, 0.46451256, 0.09715253])

# Convert values to array
values = np.array(list(dicts.values()))

# Compute the RMSE and get the index for the least RMSE 
rmse = np.mean((values-new_value)**2, axis=1)**0.5
index = np.argmin(rmse)    

print ("The closest value is %s" %(values[index]))
# The closest value is [1 1 0 0]

答案 1 :(得分:1)

您可以使用np.argmin来获得可用np.linalg.norm计算的均方根值的最低索引

import numpy as np
a = np.array([[0, 0, 0, 0], [1, 0, 0, 0], [1, 1, 0, 0],[1, 1, 1, 0], [1, 1, 1, 1]])
b = np.array([0.9539342, 0.84090066, 0.46451256, 0.09715253])
np.argmin(np.linalg.norm(a-b, axis=1))
#outputs 2 which corresponds to the value [1, 1, 0, 0]

如编辑中所述,b可以有多行。 op希望避免for循环,但是我似乎找不到找到避免for循环的方法。这是一种列表填充方式,但可能会有更好的方法

[np.argmin(np.linalg.norm(a-i, axis=1)) for i in b] 
#Outputs [2, 1]

答案 2 :(得分:0)

纯粹的numpy:

val1 = np.array ([
   [0, 0, 0, 0],
   [1, 0, 0, 0],
   [1, 1, 0, 0],
   [1, 1, 1, 0],
   [1, 1, 1, 1]
  ])

print val1
val2 = np.array ([0.9539342, 0.84090066, 0.46451256, 0.09715253], float)
val3 = np.round(val2, 0)
print val3

print np.where((val1 == val3).all(axis=1)) # show a match on row 2 (array([2]),)
相关问题