如何将矩阵转换为3D阵列,反之亦然?

时间:2017-04-08 02:57:37

标签: python arrays matrix

我想将矩阵转换为3D数组,或将3D数组转换为矩阵。如何输入数据以及如何在Python中进行转换工作?

我搜索了很多地方,但没有答案。请帮帮我

矩阵a:

    a   b   c
d   1   2   3
e   2   3   4
f   4   3   2

数组b:

a   d   1
a   e   2
a   f   4
b   d   2
b   e   3
b   f   3
c   d   3
c   e   4
c   f   2

我可以使用stack()来实现我的目标吗?

喜欢:Python pandas - pd.melt a dataframe with datetime index results in NaN

2 个答案:

答案 0 :(得分:0)

所以你的数据实际上不是三维的,而是二维的。您实际上是在试图取消您的2D数据。这通常称为melt。您最好的选择是将数据加载到pandas数据框中。

import pandas as pd
df = pd.DataFrame([['d',1,2,3],['e',2,3,4],['f',4,3,2]], columns=['idx','a','b','c'])

df
# returns:
  idx  a  b  c
0   d  1  2  3
1   e  2  3  4
2   f  4  3  2

pd.melt(df, id_vars='index', value_vars=list('abc'))
# returns:
  idx variable  value
0   d        a      1
1   e        a      2
2   f        a      4
3   d        b      2
4   e        b      3
5   f        b      3
6   d        c      3
7   e        c      4
8   f        c      2

答案 1 :(得分:0)

我对pandas库不太熟悉,但这是使用python标准库的粗略解决方案:

#!/usr/bin/env python2
"""
Convert a matrix to 2D arrays and vice versa
http://stackoverflow.com/questions/43289673
"""

from collections import OrderedDict


TEST_MATRIX = """\
    a   b   c
d   1   2   3
e   2   3   4
f   4   3   2
"""


def parse_matrix(matrix_string):
    """Parse a matrix string and return list of tuples representing data"""
    matrix_string = matrix_string.strip()
    list_of_lines = matrix_string.splitlines()
    parsed_list = []
    y_headers = list_of_lines[0].split()
    data_rows = [i.split() for i in list_of_lines[1:]]
    for y in y_headers:
        for row in data_rows:
            parsed_list.append((y, row[0], row[y_headers.index(y) + 1]))
    return parsed_list


def convert_to_matrix(data):
    """
    Convert a parsed matrix (in the form of a list of tuples) to a matrix
    (string)
    """
    # Messes up ordering
    # y_headers = set(i[0] for i in data)
    # x_headers = set(i[1] for i in data)

    y_headers = OrderedDict()
    x_headers = OrderedDict()
    [(y_headers.setdefault(i[0]), x_headers.setdefault(i[1])) for i in data]

    matrix_string = "    " + "   ".join(y_headers)  # header
    for x in x_headers:
        row = [x]
        for y in y_headers:
            val = [i[-1] for i in data if i[0] == y and i[1] == x][0]
            row.append(val)
        row_string = "   ".join(row)
        matrix_string += "\n" + row_string
    return matrix_string


def main():
    print("Test matrix:")
    print(TEST_MATRIX)

    # parse the test matrix string to a list of tuples
    parsed_test_matrix = parse_matrix(TEST_MATRIX)
    # print the parsed matrix
    print("Parsed matrix:")
    for row in parsed_test_matrix:
        print "   ".join(row)
    print

    # convert parsed matrix back to the original matrix and print
    print("Convert parsed matrix back to matrix:")
    print(convert_to_matrix(parsed_test_matrix))


if __name__ == "__main__":
    main()