简体   繁体   English

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

[英]How to convert a matrix to 3D arrays or vice versa?

I want to transform a matrix to 3D arrays, or transforming a 3D arrays to a matrix. 我想将矩阵转换为3D数组,或将3D数组转换为矩阵。 How to input the data and how to do the transformation work in Python? 如何在Python中输入数据以及如何进行转换?

I've searched for many places, but there is no answer. 我搜索了很多地方,但没有答案。 please help me 请帮我

matrix a: 矩阵a:

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

array b: 数组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

can i use stack() to achieve my goal? 我可以使用stack()实现我的目标吗?

like: Python pandas - pd.melt a dataframe with datetime index results in NaN 例如: Python pandas-pd.naN中使用日期时间索引结果融化数据框

So your data is not actually 3 dimensional, but 2 dimensional. 因此,您的数据实际上不是3维的,而是2维的。 You are essentially trying to unpivot your 2d data. 您实质上是在尝试取消2D数据的显示。 This is often called melt . 这通常被称为melt Your best option is to load the data into a pandas data frame. 最好的选择是将数据加载到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

I'm not very familiar with the pandas library but here is a rough solution using the python standard library: 我对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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM