城市气候与海洋的关系研究
- 导入包
import numpy as npimport pandas as pdfrom pandas import Series,DataFrameimport matplotlib.pyplot as pltfrom pylab import mplmpl.rcParams['font.sans-serif'] = ['FangSong'] # 指定默认字体mpl.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题
- 导入数据各个海滨城市数据
ferrara1 = pd.read_csv('./ferrara_150715.csv')ferrara2 = pd.read_csv('./ferrara_250715.csv')ferrara3 = pd.read_csv('./ferrara_270615.csv')ferrara=pd.concat([ferrara1,ferrara1,ferrara1],ignore_index=True)torino1 = pd.read_csv('./torino_150715.csv')torino2 = pd.read_csv('./torino_250715.csv')torino3 = pd.read_csv('./torino_270615.csv')torino = pd.concat([torino1,torino2,torino3],ignore_index=True) mantova1 = pd.read_csv('./mantova_150715.csv')mantova2 = pd.read_csv('./mantova_250715.csv')mantova3 = pd.read_csv('./mantova_270615.csv')mantova = pd.concat([mantova1,mantova2,mantova3],ignore_index=True) milano1 = pd.read_csv('./milano_150715.csv')milano2 = pd.read_csv('./milano_250715.csv')milano3 = pd.read_csv('./milano_270615.csv')milano = pd.concat([milano1,milano2,milano3],ignore_index=True) ravenna1 = pd.read_csv('./ravenna_150715.csv')ravenna2 = pd.read_csv('./ravenna_250715.csv')ravenna3 = pd.read_csv('./ravenna_270615.csv')ravenna = pd.concat([ravenna1,ravenna2,ravenna3],ignore_index=True)asti1 = pd.read_csv('./asti_150715.csv')asti2 = pd.read_csv('./asti_250715.csv')asti3 = pd.read_csv('./asti_270615.csv')asti = pd.concat([asti1,asti2,asti3],ignore_index=True)bologna1 = pd.read_csv('./bologna_150715.csv')bologna2 = pd.read_csv('./bologna_250715.csv')bologna3 = pd.read_csv('./bologna_270615.csv')bologna = pd.concat([bologna1,bologna2,bologna3],ignore_index=True)piacenza1 = pd.read_csv('./piacenza_150715.csv')piacenza2 = pd.read_csv('./piacenza_250715.csv')piacenza3 = pd.read_csv('./piacenza_270615.csv')piacenza = pd.concat([piacenza1,piacenza2,piacenza3],ignore_index=True)cesena1 = pd.read_csv('./cesena_150715.csv')cesena2 = pd.read_csv('./cesena_250715.csv')cesena3 = pd.read_csv('./cesena_270615.csv')cesena = pd.concat([cesena1,cesena2,cesena3],ignore_index=True)faenza1 = pd.read_csv('./faenza_150715.csv')faenza2 = pd.read_csv('./faenza_250715.csv')faenza3 = pd.read_csv('./faenza_270615.csv')faenza = pd.concat([faenza1,faenza2,faenza3],ignore_index=True)
# 显示数据cesena.head(5)# temp 该城市的 温度 # humidity 湿度# pressure 压强# description 描述# dt 时间# wind_speed 风速# wind_deg 风向# city 城市名称# day 采集日期# dist 距离 (海里)
temp | humidity | pressure | description | dt | wind_speed | wind_deg | city | day | dist | |
---|---|---|---|---|---|---|---|---|---|---|
0 | 29.15 | 83 | 1015 | moderate rain | 1436863101 | 3.62 | 94.001 | Cesena | 2015-07-14 10:38:21 | 14 |
1 | 29.37 | 74 | 1015 | moderate rain | 1436866691 | 3.60 | 20.000 | Cesena | 2015-07-14 11:38:11 | 14 |
2 | 29.51 | 78 | 1015 | moderate rain | 1436870392 | 3.60 | 70.000 | Cesena | 2015-07-14 12:39:52 | 14 |
3 | 29.88 | 70 | 1016 | moderate rain | 1436874000 | 4.60 | 60.000 | Cesena | 2015-07-14 13:40:00 | 14 |
4 | 30.12 | 70 | 1016 | moderate rain | 1436877549 | 4.10 | 70.000 | Cesena | 2015-07-14 14:39:09 | 14 |
# 去除没用的列city_list = [ferrara,torino,mantova,milano,ravenna,asti,bologna,piacenza,cesena,faenza]for city in city_list: city.drop('Unnamed: 0',axis=1,inplace=True)
显示最高温度于离海远近的关系(观察多个城市)
# 收集每个城市的最高温度max_temp = []# 每个城市 的海里数dists = []for city in city_list: max_temp.append(city['temp'].max()) dists.append(city['dist'][0])# x(自变量) 轴 海里 y(因变量) 轴 温度plt.scatter(dists,max_temp)
观察发现,离海近的可以形成一条直线,离海远的也能形成一条直线。
- 分别以100公里和50公里为分界点,划分为离海近和离海远的两组数据(近海:小于100 远海:大于50)
#将温度和距离的数据封装到numpy数组中np_city_max_temp = np.array(max_temps)np_city_dist = np.array(dists)#生成一个判定近海城市的条件near_condition = np_city_dist < 100#近海城市的最高温度near_max_temp = np_city_max_temp[near_condition]#近海城市的距离near_dist = np_city_dist[near_condition]# 生成 图片plt.scatter(near_dist,near_max_temp)
- 算法模型:
- 概念:本质来讲就是一个特殊的对象。该对象中已经包含或者集成了一种形式的方程(还没有解的方程)。
- 作用:预测,分类
- 样本数据:(二维的df,numpy)
- 特征数据:自变量(必须是二维)
- 目标数据:因变量
- 模型对象的分类:
- 有监督学习:需要的样本数据中需要包含特征和目标数据
- 无监督学习:需要的样本数据中值需要包含特征数据
导入 sklearn 建立线性回归算法模型
from sklearn.linear_model import LinearRegression#实例化了一个算法模型对象Linear = LinearRegression()#提取样本数据x = near_dist #特征数据y = near_max_temp #目标数据#模型的训练# 参数 X 特征数据 必须是二维的 y 是一维的 x.reshape((-1,1)) 转成二维Linear.fit(x.reshape((-1,1)),y)#测试:预测Linear.predict(37)# 模型评分Linear.score(x.reshape((-1,1)),y)#绘线xx = np.linspace(0,80,num=100)yy = Linear.predict(xx.reshape((-1,1)))plt.scatter(near_dist,near_max_temp)plt.scatter(xx,yy)