【问题标题】:How to convert a matrix to 3D arrays or vice versa?如何将矩阵转换为 3D 数组,反之亦然?
【发布时间】:2017-04-08 02:57:37
【问题描述】:

我想将矩阵转换为 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

【问题讨论】:

    标签: python arrays matrix


    【解决方案1】:

    所以您的数据实际上不是 3 维的,而是 2 维的。您实际上是在尝试对 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
    

    【讨论】:

    • 非常感谢您的具体回答!但是我从 pd.melt 得到的回报中的 idx 都是 NaN,你能检查一下吗?
    • 我可以使用 stack() 来实现我的目标吗?喜欢stackoverflow.com/questions/30984167/…
    【解决方案2】:

    我对 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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-15
      • 1970-01-01
      • 2015-02-16
      相关资源
      最近更新 更多