【问题标题】:Difficulty displaying histogram for every variable难以显示每个变量的直方图
【发布时间】:2016-09-02 05:02:53
【问题描述】:

我需要为numeric 变量绘制直方图,以确定它们的分布是否偏斜。下面是函数定义,以及被调用的函数。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import sys

def variable_type(df, nominal_level = 3):
    categorical, numeric, nominal = [],[],[]
    for variable in df.columns.values:
        if np.issubdtype(np.array(df[variable]).dtype, int) or np.issubdtype(np.array(df[variable]).dtype, float): #if srray variable is of type int or float
            if len(np.unique(np.array(df[variable]))) <= nominal_level:
                nominal.append(variable)
            else:
                numeric.append(variable)
        else:
            categorical.append(variable)
    return numeric,categorical,nominal
def draw_histograms(df, variables, n_rows, n_cols):
    fig = plt.figure()
    import math
    for i in range(min(n_rows * n_cols, len(variables))):
        index = n_rows * 100 + n_cols * 10 + i + 1
        ax = fig.add_subplot(index)
        df[variables[i]].hist(bins = 20, ax = ax)
        plt.title(variables[i]+' distribution')
        #plt.xlabel(variables[i])
        #plt.ylabel('Count')
    plt.show()

def main():
    df = read_data()
    col_names = df.columns.tolist()
    numeric,categorical,nominal = variable_type(df) 
    util.draw_histograms(df, numeric, 3, 3)
if __name__ == "__main__":
    main()

我的程序只有在调用函数中使用 3、3 来表示 n_rowsn_cols 时才有效,这是一个问题,因为它只绘制了 20 个变量中的 9 个。如果我尝试任何其他数字,我会得到ValueError: num must be 1 &lt;= num &lt;= 18, not 0 或其他范围,具体取决于我选择的n_rowsn_cols。我该怎么做才能将所有 20 个数值变量绘制为一个图上的子图?还是我应该把它分成不同的数字?这是我的数据框的示例。

   TARGET_B      ID  GiftCnt36  GiftCntAll  GiftCntCard36  GiftCntCardAll  \
0         0   14974          2           4              1               3   
1         0    6294          1           8              0               3   
2         1   46110          6          41              3              20   
3         1  185937          3          12              3               8   
4         0   29637          1           1              1               1   

   GiftAvgLast  GiftAvg36  GiftAvgAll  GiftAvgCard36      ...       \
0           17      13.50        9.25          17.00      ...        
1           20      20.00       15.88            NaN      ...        
2            6       5.17        3.73           5.00      ...        
3           10       8.67        8.50           8.67      ...        
4           20      20.00       20.00          20.00      ...        

   PromCntCardAll  StatusCat96NK  StatusCatStarAll  DemCluster  DemAge  \
0              13              A                 0           0     NaN   
1              24              A                 0          23      67   
2              22              S                 1           0     NaN   
3              16              E                 1           0     NaN   
4               6              F                 0          35      53   

   DemGender  DemHomeOwner  DemMedHomeValue DemPctVeterans  DemMedIncome  
0          F             U              $0               0           $0   
1          F             U        $186,800              85           $0   
2          M             U         $87,600              36      $38,750   
3          M             U        $139,200              27      $38,942   
4          M             U        $168,100              37      $71,509 

【问题讨论】:

  • util?这是你定义的吗?
  • 是的。 variable_type 在实用程序类中定义。让我编辑我的问题以避免任何疑问。

标签: python matplotlib dataframe histogram data-mining


【解决方案1】:

您的第 10 个属性中有一个 NaN。你的代码能处理这个吗? 你绘制第 10 个属性?

【讨论】:

  • 这是另一个问题,我需要将 NaN 替换为 np.Nan。我尝试了 df.replace('Nan', np.NaN),但没有奏效。我将如何替换那些 NaN 值?
  • 那时他们仍然是np.NaN,可能会导致问题。那么,你能绘制直方图#10吗?
  • 不,我不能。这是我需要绘制的变量。 ['ID', 'GiftCnt36', 'GiftCntAll', 'GiftCntCard36', 'GiftCntCardAll', 'GiftAvgLast', 'GiftAvg36', 'GiftAvgAll', 'GiftAvgCard36', 'GiftTimeLast', 'GiftTimeFirst', 'PromCnt12', 'PromCnt36', 'PromCntAll', 'PromCntCard12', 'PromCntCard36', 'PromCntCardAll', 'DemCluster', 'DemAge', 'DemPctVeterans'],我只能从“ID”到“GiftAvgCard36”进行绘图。尽管有 Nan 值,但这九个变量确实会绘制。
  • 这很令人困惑,对于作业,我需要绘制一个带有缺失值的直方图以进行质量检查。一旦我将直方图用于 .识别具有偏态分布并需要对数或 sqrt 转换的数字变量,然后我需要用虚拟值来估算缺失的插补。
猜你喜欢
  • 2021-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多