Pandasxフーリエ


pandas階層インデックス
まずjupyter notebookにライブラリを導入します
import numpy as np
import pandas as pd
from pandas import Series,DataFrame
import matplotlib.pyplot as plt

1.複数行インデックスの作成
  • 暗黙構造で最も一般的な方法は、DataFrame構造関数のindexパラメータに2つ以上の配列
  • を渡すことです.
    Seriesは多層インデックスを作成することもできます
    In[]:
    s = Series(np.random.randint(0,150,size=6),index=[['a','a','b','b','c','c'],['  ','  ','  ','  ','  ','  ']])
    s
    Out[]:
    a        59
             43
    b        28
             99
    c        92
             58
    dtype: int32
    
    In[]:
    df = DataFrame(s,columns=['python'])
    df
    Out[]:
        python
    a       59
        43
    b       28
        99
    c       92
        58
    

    DataFrame 2段カラムインデックスの作成
    ```python
    In[]:
    df1 = DataFrame(np.random.randint(0,150,size=(4,6)),
                   index = list('    '),
                   columns=[['python','python','math','math','En','En'],['  ','  ','  ','  ','  ','  ']])
    df1
    Out[]:
    python  math    En
                          
        45  48  74  142 81  22
        27  61  7   146 42  129
        25  36  99  14  143 62
        9   107 146 33  31  0
    

    構造pdを表示する.MultiIndex
    配列の使用
    構造pdを表示する.MultiIndex
  • 使用配列
  • #     pd.MultiIndex
    
    - **    **
    ```python
    In[]:
    df2 = DataFrame(np.random.randint(0,150,size=(4,6)),
                   index = list('    '),
                   columns=[['python','python','math','math','En','En'],['  ','  ','  ','  ','  ','  ']])
    df2
    Out[]:
    python  math    En
                          
        139 55  22  14  77  95
        9   90  131 114 78  131
        26  86  27  117 142 91
        53  1   128 67  12  63
    
    
    
    
    <div class="se-preview-section-delimiter"></div>
    
    

    tupleの使用
    - **  tuple**
    ```python
    In[]:
    df3 = DataFrame(np.random.randint(0,150,size=(4,6)),
                   index = list('    '),
                   columns =pd.MultiIndex.from_tuples([('python','  '),('python','  '),
                                                      ('math','  '),('math','  '),
                                                      ('En','  '),('En','  ')]))
    df3
    Out[]:
    python  math    En
                          
        32  72  2   102 136 128
        124 0   145 83  69  50
        120 128 84  129 130 134
        143 70  76  112 124 75
    
    
    
    
    <div class="se-preview-section-delimiter"></div>
    

    productを使うのが一番簡単で、お勧めです.
    複数カラムインデックス
    行インデックスindexに加えて、カラムインデックスcolumnsも同様の方法で多層インデックスを作成できます.
  • productの使用が最も簡単で、推奨
  • 複数カラムインデックス
         index,   columns              
    ```python
    In[]:
    df4 = DataFrame(np.random.randint(0,150,size=(8,12)),
                   columns = pd.MultiIndex.from_product([['   ','   '],
                                                       ['  ','  ','  ','  ','  ','  ']]),
                   index = pd.MultiIndex.from_product([['  ','  '],
                                                       ['  ','  '],
                                                      ['   ','   ']]))
    df4
    Out[]:
           
                                                  
                61  90  97  6   118 127 143 35  79  4   90  54
        96  128 146 107 148 62  68  7   104 103 78  2
            67  14  115 81  33  76  123 26  45  100 138 91
        32  85  149 47  15  33  8   131 138 45  95  91
                65  83  100 17  63  115 74  8   116 121 107 42
        125 93  146 25  99  115 134 5   13  72  119 119
            4   144 114 79  22  47  44  111 116 124 11  54
        16  109 1   104 62  128 104 1   16  19  97  38
    
    
    
    
    <div class="se-preview-section-delimiter"></div>
    

    3.多層索引オブジェクトの索引とスライス操作
    1)Seriesの操作【重要】Seriesにとって、直接中括弧[]と使用する.loc()はまったく同じなので、中括弧インデックスとスライスを推奨します.(1)インデックス
    #3.多層インデックスオブジェクトのインデックスとスライス操作
    1)Series   
    【  】  Series  ,     []   .loc()    ,  ,            。
    (1)   
    ```python
    In[]: s
    Out[]:
    a        59
             43
    b        28
             99
    c        92
             58
    dtype: int32
    
    
    
    
    <div class="se-preview-section-delimiter"></div>
    
    ```python
    In[]:
    s['a','  ']
    Out[]:
    59
    
    In[]:
    #         ,       
    s[['a','b']]
    Out[]:
    a        59
             43
    b        28
             99
    dtype: int32
    
    In[]:
    s.a.  
    Out[]:
    59
    
    In[]:
    s['a'][['  ','  ']]['  ']
    Out[]:
    59
    

    以上が3つの異なる方法の取期中の値です
    In[]:
    s.loc['a']
    Out[]:
          59
          43
    dtype: int32
    
    In[]:
    #iloc         
    s.iloc[:5]
    Out[]:
    a        59
             43
    b        28
             99
    c        92
    dtype: int32
    

    2)DataFrameの操作
    (1)カラム名を直接使用してカラムインデックスを行うことができます(2)ローインデックスを使用するにはix(),loc()などの関数を使用する必要があります【極めて重要】loc()関数を使用することをお勧めします.ローインデックスを使用する場合、1レベルのローインデックスが複数ある場合、2レベルのローインデックスに問題が発生します.つまり、二次インデックスを直接インデックスすることはできません.二次インデックスを一次インデックスにしてからインデックスする必要があります.
    In[]:
    df4
    Out[]:
                                   
                                                              
                61  90  97  6   118 127 143 35  79  4   90  54
        96  128 146 107 148 62  68  7   104 103 78  2
            67  14  115 81  33  76  123 26  45  100 138 91
        32  85  149 47  15  33  8   131 138 45  95  91
                65  83  100 17  63  115 74  8   116 121 107 42
        125 93  146 25  99  115 134 5   13  72  119 119
            4   144 114 79  22  47  44  111 116 124 11  54
        16  109 1   104 62  128 104 1   16  19  97  38
    
    In[]:
    df4['   '][['  ','  ']]
    Out[]:
                      
                90  61
        128 96
            14  67
        85  32
                83  65
        93  125
            144 4
        109 16
    
    In[]:
    df4.loc['  ','  ','   ']['   ','  ']
    Out[]:
                        61
                90
                97
                 6
               118
               127
               143
                35
                79
                 4
                90
                54
    Name: (  ,   ,    ), dtype: int32
    
    In[]:
    #iloc          
    df4.iloc[0]
    Out[]:
                61
                90
                97
                 6
               118
               127
               143
                35
                79
                 4
                90
                54
    Name: (  ,   ,    ), dtype: int32
    
    In[]:
    # .        ,      ,     ,       
    #       
    df4.   .  .  .  .   
    Out[]:
         61
    
    In[]:
    a=df4['   ']
    a
    Out[]:
                                          
                143 35  79  4   90  54
        68  7   104 103 78  2
            123 26  45  100 138 91
        8   131 138 45  95  91
                74  8   116 121 107 42
        134 5   13  72  119 119
            44  111 116 124 11  54
        104 1   16  19  97  38
    

    インデックススタック
    stack()
    unstack() 
    【   】  stack()   ,level     ,      ,     。 
    【   】  unstack()   ,level     ,      ,     。
    

    5.集約操作
    注意】axis【テクニック】とunstack()を逆に指定する必要があります.集約するとき、axisがどれに等しいか、どれが保持されます.いわゆる集約操作:平均数、分散、最大値、最小値......
    フーリエ
    import numpy as np
    from PIL import Image 
    # fft      ,      
    from numpy.fft import fft, ifft
    import matplotlib.pyplot as plt
    
    #      :   ,   ,  
    #     :   ,  ,      
    #     :  ,   ,                  
    #      :    ->       
    
    cat = plt.imread('./cat.jpeg')
    #         
    cat.tobytes()
    #           
    #   2  
    # uint8 255
    cat_data = np.fromstring(cat.tobytes(),np.int8)
    cat_data
    Out[]:
        array([ -25,  -70, -125, ...,  -68,   95,   62], dtype=int8)
    
    cat_fft = fft(cat_data)
    #          --      
    cat_fft
    Out[]:
    array([-1569123.               +0.j        ,
           -7005918.98362136+12604783.28030717j,
           -2819481.22749804 +8877463.78907501j, ...,
             622731.5619853  -2775345.73593521j,
           -2819481.22749811 -8877463.789075j  ,
           -7005918.98362145-12604783.28030712j])
    
    #       
    cond=np.abs(cat_fft) < 1e5
    #           
    cat_fft[cond] = 0
    #         
    cat_ifft = ifft(cat_fft)
    cat_ifft
    Out[]:
        array([-31.23561646+8.36461177e-13j,  39.44677252-4.54933103e-13j,
           -31.5575318 -3.42927554e-13j, ..., -17.97882673+8.64438000e-13j,
            56.94689652-3.02996114e-13j,  -4.95576222-8.46613566e-13j])
    
    #        
    cat_real = np.real(cat_ifft)
    cat_real
    Out[]:
    array([-31.23561646,  39.44677252, -31.5575318 , ..., -17.97882673,
            56.94689652,  -4.95576222])
    
    #jpg,       
    cat_result = np.int8(cat_real)
    cat_result
    Out[]:
    array([-31,  39, -31, ..., -17,  56,  -4], dtype=int8)
    
    luokuo = Image.frombytes(mode='RGB', size=(730,456), data=cat_result)
    luokuo
    

    最後に、出力された画像は以下のようになります.