擅长:python、mysql、java
<p>假设您的输入数据是字典。然后,可以使用NumPy作为矢量化解决方案。首先将输入列表转换为NumPy数组,然后使用<code>axis=1</code>参数来获取RMSE。在</p>
<pre><code># 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]
</code></pre>