如何在Python中将单例数组转换为标量值? [英] How to convert singleton array to a scalar value in Python?
本文介绍了如何在Python中将单例数组转换为标量值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有1x1x1x1x ...数组,并希望将其转换为标量?
Suppose I have 1x1x1x1x... array and wish to convert it to scalar?
我该怎么做?
squeeze
没有帮助.
import numpy as np
matrix = np.array([[1]])
s = np.squeeze(matrix)
print type(s)
print s
matrix = [[1]]
print type(s)
print s
s = 1
print type(s)
print s
推荐答案
您可以使用item()
函数:
import numpy as np
matrix = np.array([[[[7]]]])
print(matrix.item())
输出
7
这篇关于如何在Python中将单例数组转换为标量值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文