python - 将矩阵展平为包含值索引位置的数组

标签 python python-3.x numpy

给定一个 numpy 矩阵,my_matrix

import numpy as np

my_matrix = np.array([[1.2,2.3,None],[4.5,3.4,9.3]])

如何有效地将其展平为以下包含 my_matrix 索引位置的数组?

[[0,0],[0,1],[0,2],[1,0],[1,1],[1,2]]

最佳答案

你可以尝试:

rows, cols = my_matrix.shape
[[i, j] for i in range(rows) for j in range(cols)]

输出:

[[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2]]

关于python - 将矩阵展平为包含值索引位置的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57598824/

相关文章:

python - 如何将 json 对象列表转换为单个 pyspark 数据帧?

c++ - qextserialport 在 Windows 上丢失数据 — 我该怎么办?

python - 如何将 turtle 的 y 坐标提取为 float - Python 3.x

numpy - 简单的 ML 算法无法工作 : ValueError: Error when checking input: expected dense_4_input to have shape (None, 5) 但得到了形状为 (5, 1) 的数组

python - 最有效的 groupby 方式 => 聚合 pandas 中的大型数据框

python - 值错误 : Failed to convert a NumPy array to a Tensor (Unsupported object type numpy. ndarray)。试图预测特斯拉股票

python - 刷新或检测 conda 环境

python - 使用 DNNRegressor 的损失函数是什么?

python - Pandas :折叠多索引数据框中的行

python - 使用字典和数组将点符号字符串转换为嵌套的 Python 对象