简体   繁体   English

在Python中将3d数组关联为n乘2d行

[英]Correlate a 3d array into n times 2d rows in Python

Was just wondering if there was a simple way to flatten a 2d array into correlation rows. 只是想知道是否有一种简单的方法可以将2d数组展平为相关行。 Ok so here's an example: I have n numbers of a 2d array as such: 好的,所以这是一个示例:我有n个2d数组,例如:

在此处输入图片说明

So say example here a 3d (6x10x n) How can i correlate a1:a10, b1:b10 till f1:f10 so call them feature a1,a2,a3....f8,f9,f10 and do this for n rows. 所以在这里说一个3d(6x10x n)的示例如何将a1:a10,b1:b10关联到f1:f10,所以称它们为a1,a2,a3 .... f8,f9,f10并针对n行执行此操作。 N rows of 60 features (a1,a2,a3...). N行60个特征(a1,a2,a3 ...)。

Answer for n rows is: n行的答案是: 在此处输入图片说明

import numpy as np 
from numpy import array
from numpy import vstack
from numpy import hstack
import scipy.io
mat = scipy.io.loadmat('x.mat')

I've only been able to load the .mat file to python. 我只能将.mat文件加载到python。 Are there any functions i can use in Python? 我可以在Python中使用任何功能吗? Thank you for any help. 感谢您的任何帮助。

Kevin 凯文

You can do something like this : 您可以执行以下操作:

import numpy as np
import pandas as pd

Array = np.array(2d_array)
columns = Array[0, 1:]
rows = Array[1:, 0:1]
values = Array[1:,1:]

results = dict()
for row, column, value in np.nditer((rows, columns, values)):
    feature = "".join(map(str, [row, column]))
    if feature not in results:
       results[feature] = []
    results[feature].append(value)
df = pd.DataFrame.from_dict(results)

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

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