数据的准备
需要从本地导入数据集
import numpy as np
import pandas as pd
# 利用Pandas抽取降雨量,放入一个NumPy数组
rainfall = pd.read_csv('data/Seattle2014.csv')['PRCP'].values
inches = rainfall / 254 # 1/10mm -> inches
inches.shape
print(inches)
展示对应降水量区间的天数
这里用到matplotlib包
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn
seaborn.set()
# 设置绘图风格In
plt.hist(inches, 40) #参数是分割的密度
分析一年的降水情况
上述操作并没有很好地传递出我们希望看到的某些信息,例如一年中有多少天在下雨,这些下雨天的平均降水量是多少,有多少天的降水量超过了半英寸
print("没有下雨的天数: ", np.sum(inches == 0))
print("下雨的天数: ", np.sum(inches != 0))
print("降水量大于0.5英寸的天数: ", np.sum(inches > 0.5))
print("下雨但降水量少于0.1英寸的天数: ", np.sum((inches > 0) &(inches < 0.2)))
分析雨季
# 为所有下雨天创建一个掩码
rainy = (inches > 0)
# 构建一个包含整个夏季日期的掩码(6月21日是第172天)
summer = (np.arange(365) - 172 < 90) & (np.arange(365) - 172 > 0)
print("2014年雨天平均降水量(英寸): ", np.median(inches[rainy]))
print("2014年夏季平均降水量(英寸): ", np.median(inches[summer]))
print("2014年夏季最大降水量(英寸): ", np.max(inches[summer]))
print("非夏季雨天平均降水量(英寸):", np.median(inches[rainy & ~summer]))
结果
2014年西雅图大多数时间的降水量都是接近0的,雨季分布在春东季节,夏季是旱季,降水量极少,但是总体来说,就算是非夏季的平均降水也不高