如何使用类似于matlab的python结构数组(How to use python structure array similar to matlab)

编程入门 行业动态 更新时间:2024-10-26 14:35:15
如何使用类似于matlab的python结构数组(How to use python structure array similar to matlab)

早上好,我已经仔细研究过,试图找出一种方法来创建一个像python中的struct array这样的matlab。 我输入的.csv文件少了标题

我的matlab代码

dumpdata = csvread('dumpdata.csv'); N_dumpdata_samples = length(dumpdata); rec_sample_1second = struct('UTC_time',{},'sv_id_set',{},'pseudorange', {},'state',{}); for s=1:1:N_dumpdata_samples rec_sample_1second(s).UTC_time = dumpdata(s,1); rec_sample_1second(s).UTC_time = round(rec_sample_1second(s). UTC_time * 10); rec_sample_1second(s).UTC_time = rec_sample_1second(s). UTC_time / 10; for t=1:1:15 rec_sample_1second(s).sv_id_set(t) = dumpdata(s,t+1); rec_sample_1second(s).pseudorange(t) = dumpdata(s,t+16); rec_sample_1second(s).state(t) = dumpdata(s,t+31); end; end;

试图在python中实现

import numpy as np import pandas as pd df = pd.read_csv('path'/Dumpdata.csv',header=None) N_dumpdata_samples=len(df) structure={} structure["parent1"] = {} UTC_time=[] for s in range(N_dumpdata_samples): # structure['parent1']['UTC_time']=df[s,0] -> this line give error UTC_time=df['s',0] .......

我的问题是:如何在Python中实现相同的逻辑和结构。

谢谢

Good morning I have thoroughly looked around to try figuring out a way to create a matlab like struct array in python. My input .csv file is header less

My matlab code

dumpdata = csvread('dumpdata.csv'); N_dumpdata_samples = length(dumpdata); rec_sample_1second = struct('UTC_time',{},'sv_id_set',{},'pseudorange', {},'state',{}); for s=1:1:N_dumpdata_samples rec_sample_1second(s).UTC_time = dumpdata(s,1); rec_sample_1second(s).UTC_time = round(rec_sample_1second(s). UTC_time * 10); rec_sample_1second(s).UTC_time = rec_sample_1second(s). UTC_time / 10; for t=1:1:15 rec_sample_1second(s).sv_id_set(t) = dumpdata(s,t+1); rec_sample_1second(s).pseudorange(t) = dumpdata(s,t+16); rec_sample_1second(s).state(t) = dumpdata(s,t+31); end; end;

Trying to implement in python

import numpy as np import pandas as pd df = pd.read_csv('path'/Dumpdata.csv',header=None) N_dumpdata_samples=len(df) structure={} structure["parent1"] = {} UTC_time=[] for s in range(N_dumpdata_samples): # structure['parent1']['UTC_time']=df[s,0] -> this line give error UTC_time=df['s',0] .......

My question is : How can I implement same logic and structure in python.

Thanks

最满意答案

在八度:

>> data = struct('A',{}, 'B', {}); >> for s=1:1;5 data(s).A = s for t=1:1:3 data(s).B(t) = s+t end; end;

生产

>> data.A ans = 1 ans = 2 ans = 3 ans = 4 ans = 5 >> data.B ans = 2 3 4 ans = 3 4 5 ans = 4 5 6 ans = 5 6 7 ans = 6 7 8 >> save -7 stack47277436.mat data

使用scipy.io.loadmat加载到numpy :

In [17]: res = loadmat('stack47277436.mat') In [18]: res Out[18]: {'__globals__': [], '__header__': b'MATLAB 5.0 MAT-file, written by Octave 4.0.0, 2017-11-14 04:48:21 UTC', '__version__': '1.0', 'data': array([[(array([[ 1.]]), array([[ 2., 3., 4.]])), (array([[ 2.]]), array([[ 3., 4., 5.]])), (array([[ 3.]]), array([[ 4., 5., 6.]])), (array([[ 4.]]), array([[ 5., 6., 7.]])), (array([[ 5.]]), array([[ 6., 7., 8.]]))]], dtype=[('A', 'O'), ('B', 'O')])}

或者用squeeze_me加载以除去单数尺寸

In [22]: res = loadmat('stack47277436.mat',squeeze_me=True) In [24]: res['data'] Out[24]: array([(1.0, array([ 2., 3., 4.])), (2.0, array([ 3., 4., 5.])), (3.0, array([ 4., 5., 6.])), (4.0, array([ 5., 6., 7.])), (5.0, array([ 6., 7., 8.]))], dtype=[('A', 'O'), ('B', 'O')]) In [25]: _.shape Out[25]: (5,)

该struct已被翻译成具有2个字段的结构化数组,对应于struct字段(是MATLAB名称吗?)

In [26]: res['data']['A'] Out[26]: array([1.0, 2.0, 3.0, 4.0, 5.0], dtype=object) In [27]: res['data']['B'] Out[27]: array([array([ 2., 3., 4.]), array([ 3., 4., 5.]), array([ 4., 5., 6.]), array([ 5., 6., 7.]), array([ 6., 7., 8.])], dtype=object)

A是一个数组(对象dtype)。 B也是对象dtype,但包含数组。 这就是loadmat处理MATLAB单元的方式。

MATLAB struct也可以实现为具有属性A和B自定义类,或者作为具有这些键的字典。

我比pandas更了解numpy ,但是我们试着把这个数组放到一个数据框中:

In [28]: import pandas as pd In [29]: df = pd.DataFrame(res['data']) In [30]: df Out[30]: A B 0 1 [2.0, 3.0, 4.0] 1 2 [3.0, 4.0, 5.0] 2 3 [4.0, 5.0, 6.0] 3 4 [5.0, 6.0, 7.0] 4 5 [6.0, 7.0, 8.0] In [31]: df.dtypes Out[31]: A object B object dtype: object

在numpy ,字段可以清理并分配给变量:

In [37]: A = res['data']['A'].astype(int) In [38]: B = np.stack(res['data']['B']) In [39]: A Out[39]: array([1, 2, 3, 4, 5]) In [40]: B Out[40]: array([[ 2., 3., 4.], [ 3., 4., 5.], [ 4., 5., 6.], [ 5., 6., 7.], [ 6., 7., 8.]])

一个是(5,)形状数组,另一个是(5,3)。

我可以将它们打包成一个结构更好的dtype:

In [48]: C = np.empty((5,), [('A',int), ('B', int, (3,))]) In [49]: C['A'] = A In [50]: C['B'] = B In [51]: C Out[51]: array([(1, [2, 3, 4]), (2, [3, 4, 5]), (3, [4, 5, 6]), (4, [5, 6, 7]), (5, [6, 7, 8])], dtype=[('A', '<i4'), ('B', '<i4', (3,))])

In Octave:

>> data = struct('A',{}, 'B', {}); >> for s=1:1;5 data(s).A = s for t=1:1:3 data(s).B(t) = s+t end; end;

producing

>> data.A ans = 1 ans = 2 ans = 3 ans = 4 ans = 5 >> data.B ans = 2 3 4 ans = 3 4 5 ans = 4 5 6 ans = 5 6 7 ans = 6 7 8 >> save -7 stack47277436.mat data

Loading that in numpy with the scipy.io.loadmat:

In [17]: res = loadmat('stack47277436.mat') In [18]: res Out[18]: {'__globals__': [], '__header__': b'MATLAB 5.0 MAT-file, written by Octave 4.0.0, 2017-11-14 04:48:21 UTC', '__version__': '1.0', 'data': array([[(array([[ 1.]]), array([[ 2., 3., 4.]])), (array([[ 2.]]), array([[ 3., 4., 5.]])), (array([[ 3.]]), array([[ 4., 5., 6.]])), (array([[ 4.]]), array([[ 5., 6., 7.]])), (array([[ 5.]]), array([[ 6., 7., 8.]]))]], dtype=[('A', 'O'), ('B', 'O')])}

Or load with squeeze_me to remove the singular dimensions

In [22]: res = loadmat('stack47277436.mat',squeeze_me=True) In [24]: res['data'] Out[24]: array([(1.0, array([ 2., 3., 4.])), (2.0, array([ 3., 4., 5.])), (3.0, array([ 4., 5., 6.])), (4.0, array([ 5., 6., 7.])), (5.0, array([ 6., 7., 8.]))], dtype=[('A', 'O'), ('B', 'O')]) In [25]: _.shape Out[25]: (5,)

The struct has been translated into a structured array with 2 fields, corresponding to the struct fields (is that the MATLAB name?)

In [26]: res['data']['A'] Out[26]: array([1.0, 2.0, 3.0, 4.0, 5.0], dtype=object) In [27]: res['data']['B'] Out[27]: array([array([ 2., 3., 4.]), array([ 3., 4., 5.]), array([ 4., 5., 6.]), array([ 5., 6., 7.]), array([ 6., 7., 8.])], dtype=object)

A is an array (object dtype). B is also object dtype, but contains arrays. That's how loadmat handles MATLAB cells.

MATLAB struct could also be implemented as custom class with attributes A and B, or as a dictionary with those keys.

I know numpy better than pandas, but lets try to put this array into a dataframe:

In [28]: import pandas as pd In [29]: df = pd.DataFrame(res['data']) In [30]: df Out[30]: A B 0 1 [2.0, 3.0, 4.0] 1 2 [3.0, 4.0, 5.0] 2 3 [4.0, 5.0, 6.0] 3 4 [5.0, 6.0, 7.0] 4 5 [6.0, 7.0, 8.0] In [31]: df.dtypes Out[31]: A object B object dtype: object

In numpy the fields could be cleaned up and assigned to variables:

In [37]: A = res['data']['A'].astype(int) In [38]: B = np.stack(res['data']['B']) In [39]: A Out[39]: array([1, 2, 3, 4, 5]) In [40]: B Out[40]: array([[ 2., 3., 4.], [ 3., 4., 5.], [ 4., 5., 6.], [ 5., 6., 7.], [ 6., 7., 8.]])

One is a (5,) shape array, the other (5,3).

I could pack those back into a structured array with a prettier dtype:

In [48]: C = np.empty((5,), [('A',int), ('B', int, (3,))]) In [49]: C['A'] = A In [50]: C['B'] = B In [51]: C Out[51]: array([(1, [2, 3, 4]), (2, [3, 4, 5]), (3, [4, 5, 6]), (4, [5, 6, 7]), (5, [6, 7, 8])], dtype=[('A', '<i4'), ('B', '<i4', (3,))])

更多推荐

本文发布于:2023-08-02 19:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1380022.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   类似于   如何使用   结构   array

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!