Flatten numpy array but also keep index of value positions?

前端 未结 9 1268
北荒
北荒 2021-02-04 13:58

I have several 2D numpy arrays (matrix) and for each one I would like to convert it to vector containing the values of the array and a vector containing each row/column index.

9条回答
  •  一整个雨季
    2021-02-04 14:36

    You can simply use loops.

    x = np.array([[3, 1, 4],
                  [1, 5, 9],
                  [2, 6, 5]])
    values = []
    coordinates = []
    data_frame = []
    for v in xrange(len(x)):
        for h in xrange(len(x[v])):
            values.append(x[v][h])
            coordinates.append((h, v))
            data_frame.append(x[v][h], h, v)
            print '%s | %s | %s' % (x[v][h], v, h)
    

提交回复
热议问题