2017-11-14 17 views
0

おはよう pythonでstruct arrayのような matlabを作成する方法を考えてみることを徹底的に見てきました。私の入力の.csvファイルが matlabに似た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で以下

マイ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で同じロジックと構造を実装することができます。

おかげ

答えて

1

>> 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; 

scipy.io.loadmatとでnumpyこと

>> 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 

読み込み製造:squeeze_me

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')])} 

や負荷が特異寸法を除去するために

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つのフィールドで構造化された配列に翻訳された

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)である(MATLABの名前は?ということです)。 Bもオブジェクトdtypeですが、配列を含んでいます。これは、loadmatがMATLABセルを処理する方法です。

MATLAB構造体は、属性が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.]]) 

1つは(5、)形状の配列であり、もう1つ(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,))]) 
関連する問題