python - 如何在 Python 中将单例数组转换为标量值?

标签 python numpy multidimensional-array

假设我有 1x1x1x1x... 数组并希望将其转换为标量?

我该怎么做?

挤压 没有帮助。

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 - 如何在 Python 中将单例数组转换为标量值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35157742/

相关文章:

c - 带有 malloc() 错误的二维数组

php - 将多个表单输入组合成键对值数组并为值添加维度

javascript - array.sort() 函数中的 "Cannot read property ' formatTime ' of undefined"错误

python - Django 多数据库(完整性检查)

python - 连接并删除 numpy 数组中的重复元素

python - 通过 PIL 将 Numpy 数组保存为实验室图像

python - 将 WinAPI 屏幕截图转换为 OpenCV 兼容形式

python - PyMySQL INSERT INTO 查询忽略/切断未运行的查询

python - Django settings.py 未更新

javascript - 喀拉斯-JS "Error: [Model] Model configuration does not contain any layers."