简体   繁体   English

重塑numpy中的矩阵

[英]Reshaping matrix in numpy

I am currently trying to reshape a triangular matrix into a different form like this 我目前正在尝试将三角矩阵重塑成这样的不同形式

              S&P 500   Russel 2000     Nasdaq Composite
S&P 500             1             0     0
Russel 2000         4             5     0
Nasdaq Composite    7             8     9

    0                           1                  
0   S&P 500                     S&P 500      
1   Russel 2000                 S&P 500
2   Nasdaq Composite            S&P 500
3   S&P 500                     Russel 2000
4   Russel 2000                 Russel 2000
...

That is, I am trying to make repetitive columns for each index or vice versa. 也就是说,我正在尝试为每个索引创建重复的列,反之亦然。

I have used an array-oriented language called J, but I am pretty new to python numpy. 我使用了一种称为J的面向数组的语言,但是对于python numpy来说,我还是一个新手。 Can you guys help me out on this problem? 你们能帮我解决这个问题吗?

Thanks in advance = ] 在此先感谢=]

First of all this looks like a problem more easily tackled by pandas . 首先,这看起来是熊猫更容易解决的问题。

I have set something up which might help, but if you are really stuck with using Numpy then it won't be a proper solution to your problem. 我已经进行了设置,可能会有所帮助,但是如果您真的不喜欢使用Numpy,那么它将不是解决问题的适当方法。

First I have set up a similar DataFrame object to your numpy array: 首先,我为您的numpy数组设置了类似的DataFrame对象:

$ import pandas as pd
$ df = pd.DataFrame(columns=['a','b','c'], index=['a','b','c'])
    a    b    c
a  NaN  NaN  NaN
b  NaN  NaN  NaN
c  NaN  NaN  NaN

Then I have filled it with the values you requested, although I don't use them. 然后,尽管我没有使用它们,但仍用您要求的值填充了它。

$ df['a'] = [1, 4, 7]
$ df['b'] = [0, 5, 8]
$ df['c'] = [0, 0, 9]
   a  b  c
a  1  0  0
b  4  5  0
c  7  8  9

Finally I have simply taken the index and the columns and used the product function from itertools to get every combination into a list of tuples. 最后,我只是简单地获取了索引和列,并使用了itertools中乘积函数将每个组合添加到元组列表中。

$ from itertools import product
$ p = product(df.columns.values, df.index.values)
$ new_df = pd.DataFrame([i for i in p])
   0  1
0  a  a
1  a  b
2  a  c
3  b  a
4  b  b
5  b  c
6  c  a
7  c  b
8  c  c

I would have a look at the Pandas documentation. 我会看一下Pandas文档。 They have many functions to manipulate dataframes in ways that you might be interested in. 它们具有许多功能,可以按您感兴趣的方式操作数据框。

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

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