我试图弄清楚如何将numpy数组中的每个值除以值的总数。并将结果写入新数组。我尝试了for循环,但在新数组中得到了全零。也许数组需要展平。有什么建议么?谢谢你的帮助。
tf = np.array([[1,0,2],[0,2,0]])
array([[1, 0, 2],
[0, 2, 0]])
这是我想要的输出
array([[0.333, 0. , 0.667],
[0. , 0.667, 0. ]])
您可以获取tf
using的数组尺寸tf.shape
,内部尺寸为tf.shape[1]
。因此,除以:
tf = np.array([[1,0,2],[0,2,0]])
tf = tf / tf.shape[1]
tf
输出:
array([[0.33333333, 0. , 0.66666667],
[0. , 0.66666667, 0. ]])
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句