5.乘客数量对比分析
按照年份和月份做频数分析
(1) 对年份进行求和,随着时间的推移,乘客数量呈上升趋势
1. data['year_sum'] = data.sum(axis=1) # 按照年份求和 2. 3. data['year_sum'] = data.apply(lambda x: x.sum(), axis=1) # 按照年份求和 4. data
(2)对月份进行求和,其中7月和8月的乘客数量最多,2月和9月乘客数量相对较少
1. data.loc['month_sum'] = data.apply(lambda x:x.sum(),axis=0) # 按照月份求和 2. data
(2) 用Matplotlib绘制条形图、现状图分析每年乘客数量情况,乘客数量总体呈上升趋势
1. x = [1949,1950,1951,1952,1953,1954,1955,1956,1957,1958,1959,1960] 2. y = data['sum'] 3. 4. plt.title('每年乘客数量分析(条形图)') 5. 6. plt.xlabel('年份') 7. plt.ylabel('乘客数量') 8. 9. plt.bar(x,y,color='pink') 10. 11. plt.show()
(4)使用线状图表展示每年乘客变化趋势图
1. plt.title('每年乘客变化趋势(线状图)') 2. 3. plt.xlabel('年份') 4. plt.ylabel('乘客数量') 5. 6. plt.plot(x, y, color='r') 7. plt.show()
1. plt.plot(data.index[:-1],data['year_sum'][:-1],'ro--') 2. 3. plt.title('乘客数量变化趋势') 4. plt.xlabel('年份') 5. plt.ylabel('乘客数量') 6. plt.show()
(5)对比月度乘客总数量,7月和8月为乘客人数的高峰时期
1. x = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'] 2. y = data.loc['count'] 3. 4. plt.title('月度乘客数量分析(条形图)') 5. 6. plt.xlabel('月份') 7. plt.ylabel('乘客数量') 8. 9. plt.bar(x,y,color='hotpink') 10. 11. plt.show()
6.对所用乘客人数做描述性统计
(1)以年份维度作堆叠得出堆叠结果
1. import numpy as np 2. import matplotlib.pyplot as plt 3. 4. # 使用的数据集 5. year = data['year'] 6. population_by_continent = { 7. 'Jan': [112,118,132,129,121,135,148,148,136,119,104,118], 8. 'Feb': [115,126,141,135,125,149,170,170,158,133,114,140], 9. 'Mar': [145,150,178,163,172,178,199,199,184,162,146,166], 10. 'Apr': [171,180,193,181,183,218,230,242,209,191,172,194], 11. 'May': [196,196,236,235,229,243,264,272,237,211,180,201], 12. 'Jun': [204,188,235,227,234,264,302,293,259,229,203,229], 13. 'Jul': [242,233,267,269,270,315,364,347,312,274,237,278], 14. 'Aug': [284,277,317,313,318,374,413,405,355,306,271,306], 15. 'Sep': [315,301,356,348,355,422,465,467,404,347,305,336], 16. 'Oct': [340,318,362,348,363,435,491,505,404,359,310,337], 17. 'Nov': [340,318,362,348,363,435,491,505,404,359,310,337], 18. 'Dec': [417,391,419,461,472,535,622,606,508,461,390,432], 19. } 20. 21. # 初始化figure和axes 22. fig, ax = plt.subplots() 23. sum = np.zeros((1, len(year))).reshape(-1) 24. 25. # 绘图 26. for i in population_by_continent: 27. ax.bar(year, population_by_continent.get(i), label=i, alpha=0.8, bottom=sum, width=5) 28. sum += population_by_continent.get(i) 29. 30. # 添加图例和标题 31. ax.legend(loc='upper left') 32. ax.set_title('年份纬度') 33. ax.set_xlabel('年份') 34. ax.set_ylabel('乘客数量') 35. 36. plt.show()
(2)使用函数生成描述性统计,观察数据集分布的中心趋势,分散和形状
1. data1_stack = data1.stack() # 堆叠结果 2. data1_stack
(3)计算数据的偏度系数
1. from scipy import stats 2. 3. # 年份纬度 4. x = pd.Series(data['sum']) # 将列表x转换为pandas中的Series 5. skew = stats.skew(x) # 使用stats计算偏度 6. print('偏度:',skew)
1. # 月份纬度 2. x = pd.Series(data.loc['count']) # 将列表x转换为pandas中的Series 3. skew = stats.skew(x) # 使用stats计算偏度 4. print('偏度:',skew)
1. from scipy import stats 2. 3. # 偏度系数 4. data1_stack.skew()
(4)计算数据的峰度系数
1. # 年份纬度 2. s = pd.Series(data['sum']) # 将列表x转换为pandas中的Series 3. kurtosis = stats.kurtosis(x)#使用stats计算峰度 4. print('峰度:',kurtosis)
1. # 月份纬度 2. x = pd.Series(data.loc['count']) # 将列表x转换为pandas中的Series 3. kurtosis = stats.kurtosis(x)#使用stats计算峰度 4. print('峰度:',kurtosis)
1. from scipy import stats 2. 3. # 峰度系数 4. data1_stack.kurt()
(5)对航空乘客数量进行周期性分析,总体呈上升趋势
1. plt.figure(figsize=(7,5)) 2. 3. plt.plot(data1_stack.values,color='hotpink') 4. plt.title('每月乘客数量直方图') 5. 6. plt.show()
总结
通过本次实验对数据的基本处理,应用数据探索的方法完成案例的质量探索和规律探索。掌握了频数分布、集中和离散趋势、偏度和峰度等图表绘制方法或统计方法。这是一次有挑战性并且有趣的实验,通过不断地学习挖掘数据隐藏的信息,获取数据的价值为后续的工作提供足够的条件。