• 数据的4个等级


    除了可以将数据分为定量和定性的,数据还可以分为以下4个等级,每个等级都有不同的控制和数学操作等级;
    定类等级(nominal level)
    定序等级(ordinal level)
    定距等级(interval level)
    定比等级(ratio level)

    定类等级是数据的第一个等级,其结构最弱。这个等级的数据只按名称分类。例如,血型(A、B、O和AB型)、动物物种和人名。这些数据都是定性的。

    在这个等级上,虽然不能执行任何定量数学操作(例如加法或除法),但是我们可以用Pandas的value_counts方法进行计数;

    print(salary_ranges['Grade'].value_counts())
    
    # Grade
    # 0        61
    # 7450     12
    # 6870      9
    # 7420      9
    # 7170      9
    #          ..
    # 4310F     1
    # 1063F     1
    # 4290F     1
    # 4267F     1
    # 6465      1
    # Name: count, Length: 688, dtype: int64
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    我们可以在定类等级上进行计数,因此可以绘制图表(如条形图)

    import pandas as pd
    import matplotlib.pyplot as plt
    
    salary_ranges = pd.read_csv('./data/Salary_Ranges_by_Job_Classification.csv')
    salary_ranges['Grade'].value_counts().sort_values(ascending=False).head(20).plot(kind = 'bar')
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    定序等级继承了定类等级的所有属性,同时具有以下重要附加属性:
    定序等级的数据可以自然排序;
    可以认为列中的某些数据比其他数据更好或更大。

    和定类等级一样,定序等级的天然数据属性仍然是类别;

    在定序等级,我们可以像定类等级那样进行计数,也可以引入比较和排序。因此,可以使用新的图表了。不仅可以继续使用条形图和饼图,而且因为能排序和比较,所以能计算中位数和百分位数。对于中位数和百分位数,我们可以绘制茎叶图和箱线图。

    我们引入一个新的数据集来解释定序等级的数据。这个数据集表示多少人喜欢旧金山国际机场(IATA代码:SFO);可以看到数据集有3535行,每行有95个字段的数据;其中有一个定序字段是Q7A_ART,它有0-6七个数字值;

    customer = pd.read_csv('./data/2013_SFO_Customer_survey.csv')
    print(customer.shape)
    print(customer['Q7A_ART'].value_counts().sort_values())
    
    # (3535, 95)
    # Q7A_ART
    # 1      20
    # 2      71
    # 0     100
    # 3     696
    # 6     779
    # 5     803
    # 4    1066
    # Name: count, dtype: int64
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    pandas默认会将Q7A_ART视为普通的数字字段;

    art_ratings = customer['Q7A_ART']
    print(art_ratings.describe())
    # count    3535.000000
    # mean        4.300707
    # std         1.341445
    # min         0.000000
    # 25%         3.000000
    # 50%         4.000000
    # 75%         5.000000
    # max         6.000000
    # Name: Q7A_ART, dtype: float64
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    Pandas把这个列当作数值处理了,因为这个列充满数。然而我们需要知道,虽然这些值是数,但每个数其实代表的是类别,所以该数据是定性的,更具体地说,是属于定序等级。如果删除0和6这两个类别,剩下的5个有序类别类似于餐厅的评分

    art_ratings = art_ratings[(art_ratings >=1) & (art_ratings <=5)]
    art_ratings = art_ratings.astype(str)
    print(art_ratings.describe())
    # count     2656
    # unique       5
    # top          4
    # freq      1066
    # Name: Q7A_ART, dtype: object
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    定序等级除了可以可视化数据为饼图、条形图之外,还可以可视化为箱线图;

    art_ratings.value_counts().plot(kind='pie')
    plt.show()
    
    art_ratings.value_counts().plot(kind='bar')
    plt.show()
    
    art_ratings.value_counts().plot(kind='box')
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在定距等级,数值数据不仅可以像定序等级的数据一样排序,而且值之间的差异也有意义。这意味着,在定距等级,我们不仅可以对值进行排序和比较,而且可以加减。

    在定距等级上可以进行加减,这改变了整个游戏规则。既然可以把值加在一起,就能引入两个熟悉的概念:算术平均数(就是均值)和标准差。

    我们引入新的数据集,这个数据集有860万行,每行代表某个城市某月的平均温度,上溯到18世纪。

    pd.options.display.max_columns = None
    pd.set_option('expand_frame_repr', False)
    climate = pd.read_csv('./data/GlobalLandTemperaturesByCity.csv')
    print(climate.head())
    #            dt  AverageTemperature  AverageTemperatureUncertainty   City  Country Latitude Longitude
    # 0  1743-11-01               6.068                          1.737  Århus  Denmark   57.05N    10.33E
    # 1  1743-12-01                 NaN                            NaN  Århus  Denmark   57.05N    10.33E
    # 2  1744-01-01                 NaN                            NaN  Århus  Denmark   57.05N    10.33E
    # 3  1744-02-01                 NaN                            NaN  Århus  Denmark   57.05N    10.33E
    # 4  1744-03-01                 NaN                            NaN  Århus  Denmark   57.05N    10.33E
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    删除有缺失值的记录

    print(climate.info())
    
    climate.dropna(axis= 0 , inplace=True)
    print(climate.info())
    
    # 
    # RangeIndex: 8599212 entries, 0 to 8599211
    # Data columns (total 7 columns):
    #  #   Column                         Dtype
    # ---  ------                         -----
    #  0   dt                             object
    #  1   AverageTemperature             float64
    #  2   AverageTemperatureUncertainty  float64
    #  3   City                           object
    #  4   Country                        object
    #  5   Latitude                       object
    #  6   Longitude                      object
    # dtypes: float64(2), object(5)
    # memory usage: 459.2+ MB
    # None
    # 
    # Index: 8235082 entries, 0 to 8599210
    # Data columns (total 7 columns):
    #  #   Column                         Dtype
    # ---  ------                         -----
    #  0   dt                             object
    #  1   AverageTemperature             float64
    #  2   AverageTemperatureUncertainty  float64
    #  3   City                           object
    #  4   Country                        object
    #  5   Latitude                       object
    #  6   Longitude                      object
    # dtypes: float64(2), object(5)
    # memory usage: 502.6+ MB
    # None
    #
    # Process finished with exit code 0
    
    print(climate.isnull().sum())
    # dt                               0
    # AverageTemperature               0
    # AverageTemperatureUncertainty    0
    # City                             0
    # Country                          0
    # Latitude                         0
    # Longitude                        0
    # dtype: int64
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    AverageTemperature的不同值太多,无法像定序等级那样可视化饼图和条形图;

    print(climate['AverageTemperature'].nunique())
    # 103481
    
    • 1
    • 2

    这个级别开始,最常用的图是直方图。直方图是条形图的“近亲”,用不同的桶包含不同的数据,对数据的频率进行可视化。

    climate['AverageTemperature'].hist()
    plt.show()
    
    • 1
    • 2

    查看美国各个世纪的平均气温

    climate['dt'] = pd.to_datetime(climate['dt'])
    climate['year'] = climate['dt'].map(lambda value:value.year)
    
    climate_sub_us = climate.loc[climate['Country'] == 'United States']
    climate_sub_us['century'] = climate_sub_us['year'].map(lambda value:int(value/100 + 1))
    climate_sub_us['AverageTemperature'].hist(by=climate_sub_us['century'], sharex=True, sharey=True, figsize=(10,10), bins=20)
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    通过直线图也可以查看对应的平均气温变化;

    climate['dt'] = pd.to_datetime(climate['dt'])
    climate['year'] = climate['dt'].map(lambda value:value.year)
    
    climate_sub_us = climate.loc[climate['Country'] == 'United States']
    climate_sub_us['century'] = climate_sub_us['year'].map(lambda value:int(value/100 + 1))
    climate_sub_us.groupby('century')['AverageTemperature'].mean().plot(kind = 'line')
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    因为差值在这个等级是有意义的,所以我们可以回答美国从18世纪至今平均温度上升多少这个问题

    climate['dt'] = pd.to_datetime(climate['dt'])
    climate['year'] = climate['dt'].map(lambda value:value.year)
    
    climate_sub_us = climate.loc[climate['Country'] == 'United States']
    climate_sub_us['century'] = climate_sub_us['year'].map(lambda value:int(value/100 + 1))
    century_changes = climate_sub_us.groupby('century')['AverageTemperature'].mean()
    print(century_changes)
    print(century_changes[21] - century_changes[18])
    # century
    # 18    12.073243
    # 19    13.662870
    # 20    14.386622
    # 21    15.197692
    # Name: AverageTemperature, dtype: float64
    # 3.124449115460898
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    定距及更高等级的一大好处是,我们可以使用散点图:在两个轴上绘制两列数据,将数据点可视化为图像中真正的点。在气候变化数据集中,year和averageTemperature列都属于定距等级,因为它们的差值是有意义的。

    x = climate_sub_us['year']
    y = climate_sub_us['AverageTemperature']
    fig, ax = plt.subplots(figsize=(10, 5))
    ax.scatter(x, y)
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5

    和预期一样,里面有很多噪声。考虑到每年每个城镇都会报告好几个平均气温,在图上每年有很多点也是理所应当的。我们用groupby清除年份的大部分噪声:

    x = climate_sub_us['year'].unique()
    y = climate_sub_us.groupby('year')['AverageTemperature'].mean()
    fig, ax = plt.subplots(figsize=(10, 5))
    ax.scatter(x, y)
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5

    对于等比等级,我们拥有最高程度的控制和数学运算能力。和定距等级一样,我们在定比等级上处理的也是定量数据。这里不仅继承了定距等级的加减运算,而且有了一个绝对零点的概念,可以做乘除运算。

    通过旧金山的工资数据,可以看到Biweekly High Rate列处于定比等级,我们可以看一下最高的工资

    fig = plt.figure(figsize=(15, 5))
    ax = fig.gca()
    
    salary_ranges.groupby('Grade')[['BiweeklyHighRate']].mean().sort_values('BiweeklyHighRate', ascending=False).head(20).plot.bar(stacked=False, ax=ax,color='darkorange')
    ax.set_title('Top 20 Grade by Mean Biweekly High Rate')
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    我们用同样的办法查看工资最低的工作;

    fig = plt.figure(figsize=(15, 5))
    ax = fig.gca()
    
    salary_ranges.groupby('Grade')[['BiweeklyHighRate']].mean().sort_values('BiweeklyHighRate', ascending=False).tail(20).plot.bar(stacked=False, ax=ax,color='darkorange')
    ax.set_title('Bottom 20 Grade by Mean Biweekly High Rate')
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    我们可以计算最高工资和最低工资的比值;

    sorted_df = salary_ranges.groupby('Grade')[['BiweeklyHighRate']].mean().sort_values('BiweeklyHighRate', ascending=False)
    print(sorted_df.iloc[0][0]/sorted_df.iloc[-1][0])
    # 13.931919540229886
    
    • 1
    • 2
    • 3
  • 相关阅读:
    第一季:3类和实例初始化【Java面试题】
    SpringBoot之:SpringBoot的HATEOAS基础
    [scratch][列表]全国青少年软件编程等级考试-四级-统计身高
    Tensorflow 2.x 模型-部署与实践
    使用相同版本的vncviewer登录到opensuse15.4的远程桌面tigervnc-1.10.1
    Rocky Linux团队组建的超豪华领导人团队
    IoT 设备物联网通信中 NB-IoT、Cat.1、Cat.M 如何选型?
    [ACP云计算]组件介绍
    MySql 表 转为C#实体类 ,sql语句
    sqli-labs/Less-58
  • 原文地址:https://blog.csdn.net/hou478410969/article/details/134544659