Python、データサイエンス、および選択:パート5

最後のポスト#5のための 初心者がされて選挙データの比較の視覚化に専念します。





選挙データの比較視覚化

ここで、2011年に今回はロシアで行われた他の総選挙のデータセットについて考えてみましょう。ロシアははるかに大きな国であるため、有権者に関する膨大なデータがあります。これを行うには、タブ区切りのデータフィールドを含む1つの大きなTSVファイルをRAMにロードします。





def load_ru():
    '''   '''
    return pd.read_csv('data/ch01/Russia2011.tsv', '\t')
      
      



ロシアのデータで使用できる列名を見てみましょう。





def ex_1_29():
    '''    
         '''
    return load_ru().columns
      
      



次の列のリストが表示されます。





Index([' ', ' №', ' ',
' ,    ',
...
'   ',
'   - -  ',
'  " "',
'     ',
'  "    ""',
'  " "',
'   " "'],
dtype='object')
      
      



ロシアのデータセットの列名は非常にわかりやすいですが、おそらく必要以上に長くなっています。また、英国の選挙データですでに見たのと同じ属性(たとえば、勝者のシェアと投票率)を表す列が、両方のデータセットで同じ方法でラベル付けされていると便利です。それらの名前を適切に変更しましょう。





データセットとともに、pandasライブラリ関数rename



は、現在の列名のキーが新しい名前の値にマップされている辞書を想定しています。これをすでに調べたデータと組み合わせると、次のようになります。





def load_ru_victors():
    '''   , 
       ,    '''
    new_cols_dict = {
        ' ,    ':'',
        '   ':
                                             ' ',
        '  " "':''      
        }
    newcols = list(new_cols_dict.values())    
    df = load_ru().rename( columns=new_cols_dict )[newcols]  
    df[' '] = df[''] / df[' '
    df['']            = df[' '] / df['']              
    return df 
      
      



pandas divide



, /



, . (nan



) , fill_value



. nan



, . :





 df[ ' ' ] = \
         df[ '' ].divide( df[ ' ' ], \
                                                        fill_value=1 )
      
      



, ( ). , , , :





def ex_1_30():
    '''  
          '''
    load_ru_victors()[''].hist(bins=20)
    plt.xlabel('  ') 
    plt.ylabel('')
    plt.show()
      
      



:





, . , 80% 100% — , .





, (), . :





def ex_1_31():
    '''   
           '''
    qqplot( load_ru_victors()[' '].dropna() )
    plt.show()
      
      



:





, , S- . , . , , .





, , , : 0.5 1.0 , 0.7 1.0. , 100% , ( , ), 1.0 .





, , 100% . .





, . , , , .





, , :





  • , , ,





  • ,





, , .





(), . Probability Mass Function (PMF), , . , , , , , . , , 0 1, ( 1 ), 1.





, . — , . — ( ).





, , 0 1. , , :





def plot_as_pmf(dt, label, ax):
    '''     
       (   )'''
    s   = pd.cut(dt, bins=40, labels=False)      #   40 
    pmf = s.value_counts().sort_index() / len(s) #    
    newax = pmf.plot(label=label, grid=True, ax=ax)   
    return newax
      
      



, :





def ex_1_32():
    '''      ,
             '''
    ax = plot_as_pmf(load_uk_victors()[''], '', None)
    plot_as_pmf(load_ru_victors()[''], '', ax)
    plt.xlabel('  ')   #  
    plt.ylabel('')
    plt.legend(loc='best')
    plt.show()
      
      



:





. , — , (0.6366 0.6523) — 100%. , . , , , , ( ), .





, , , 50% . (Peter Klimek) , .





, , , . , . , , , , - . , , , .





, , .





, , . : , , , . pandas scatter



, plot



.





def ex_1_33():
    '''   
         '''
    df = load_uk_victors()[ ['', ' '] ]
    df.plot.scatter(0, 1, s=3)
    plt.xlabel('')
    plt.ylabel(' ')
    plt.show()
      
      



:





, . — , , , : , , .





, 2010 . : «» . , «» , , . .





, :





def ex_1_34():
    '''     '''
    df = load_ru_victors()[ ['', ' '] ]
    df.plot.scatter(0, 1, s=3)
    plt.xlabel('')
    plt.ylabel(' ')
    plt.show()
      
      



:





, . , , .





, , , . , , , .





-, pandas alpha



scatter 0 1, 1 , 0 — .





def ex_1_35():
    '''   ( )   '''
    df = load_ru_victors()[ ['', ' '] ]
    rows = sp.random.choice(df.index.values, 10000)
    df.loc[rows].plot.scatter(0, 1, s=3, alpha=0.1)
    plt.xlabel('')
    plt.ylabel(' ')
    plt.axis([0, 1.05, 0, 1.05])
    plt.show()
      
      



:





. « » , 100% 100%- . , . , , 2011 . .





. - - , , .





Github. .





. , .





, , . , Python scipy. , pandas, , . .





. - , . , , .








All Articles