版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qingqing7/article/details/81319095
数据
数据集记录了bike sharing系统每小时自行车的出租次数。另外还包括日期、时间、天气、季节和节假日等相关信息(bike sharing数据集下载地址:http://archive.ics.uci.edu/ml/datasets/Bike+Sharing+Dataset)。
各个字段的含义如下:
- instant: record index
- dteday : date
- season : season (1:springer, 2:summer, 3:fall, 4:winter)
- yr : year (0: 2011, 1:2012)
- mnth : month ( 1 to 12)
- hr : hour (0 to 23)
- holiday : weather day is holiday or not (extracted from http://dchr.dc.gov/page/holiday-schedule)
- weekday : day of the week
- workingday : if day is neither weekend nor holiday is 1, otherwise is 0.
+ weathersit :
- 1: Clear, Few clouds, Partly cloudy, Partly cloudy
- 2: Mist + Cloudy, Mist + Broken clouds, Mist + Few clouds, Mist
- 3: Light Snow, Light Rain + Thunderstorm + Scattered clouds, Light Rain + Scattered clouds
- 4: Heavy Rain + Ice Pallets + Thunderstorm + Mist, Snow + Fog
- temp : Normalized temperature in Celsius. The values are divided to 41 (max)
- atemp: Normalized feeling temperature in Celsius. The values are divided to 50 (max)
- hum: Normalized humidity. The values are divided to 100 (max)
- windspeed: Normalized wind speed. The values are divided to 67 (max)
- casual: count of casual users
- registered: count of registered users
- cnt: count of total rental bikes including both casual and registered
示例:
instant,dteday,season,yr,mnth,hr,holiday,weekday,workingday,weathersit,temp,atemp,hum,windspeed,casual,registered,cnt
1,2011-01-01,1,0,1,0,0,6,0,1,0.24,0.2879,0.81,0,3,13,16
2,2011-01-01,1,0,1,1,0,6,0,1,0.22,0.2727,0.8,0,8,32,40
3,2011-01-01,1,0,1,2,0,6,0,1,0.22,0.2727,0.8,0,5,27,32
4,2011-01-01,1,0,1,3,0,6,0,1,0.24,0.2879,0.75,0,3,10,13
5,2011-01-01,1,0,1,4,0,6,0,1,0.24,0.2879,0.75,0,0,1,1
整个回归过程
下面在程序中逐步说明整个处理过程
#-*-coding:utf-8-*-
from pyspark import SparkContext
from pyspark.sql import SparkSession
from pyspark.sql.functions import isnull
from pyspark.ml.feature import Imputer
from pyspark.ml.feature import StringIndexer
from pyspark.ml.feature import OneHotEncoder
from pyspark.ml.feature import VectorAssembler
from pyspark.sql import types
from pyspark.sql.functions import *
from pyspark.ml.regression import LinearRegression
from pyspark.ml.regression import DecisionTreeRegressor
from pyspark.ml.regression import GBTRegressor
from pyspark.ml.evaluation import RegressionEvaluator
from pyspark.ml.tuning import ParamGridBuilder
from pyspark.ml.tuning import CrossValidator
if __name__=="__main__":
sc=SparkContext(appName='myApp')
#---------------------------------------------------
#读入数据
spark=SparkSession.builder.enableHiveSupport().getOrCreate()
df = spark.read.csv(path='/tmp/test/hour.csv', header=True, sep=',',inferSchema=True)
# ---------------------------------------------------
# 查看数据
"""
print(df.dtypes)
[('instant', 'int'), ('dteday', 'timestamp'), ('season', 'int'), ('yr', 'int'), ('mnth', 'int'), ('hr', 'int'),
('holiday', 'int'), ('weekday', 'int'), ('workingday', 'int'), ('weathersit', 'int'), ('temp', 'double'),
('atemp', 'double'), ('hum', 'double'), ('windspeed', 'double'), ('casual', 'int'), ('registered', 'int'),
('cnt', 'int')]
df.show()
+-------+-------------------+------+---+----+---+-------+-------+----------+----------+----+------+----+---------+------+----------+---+
|instant| dteday|season| yr|mnth| hr|holiday|weekday|workingday|weathersit|temp| atemp| hum|windspeed|casual|registered|cnt|
+-------+-------------------+------+---+----+---+-------+-------+----------+----------+----+------+----+---------+------+----------+---+
| 1|2011-01-01 00:00:00| 1| 0| 1| 0| 0| 6| 0| 1|0.24|0.2879|0.81| 0.0| 3| 13| 16|
| 2|2011-01-01 00:00:00| 1| 0| 1| 1| 0| 6| 0| 1|0.22|0.2727| 0.8| 0.0| 8| 32| 40|
| 3|2011-01-01 00:00:00| 1| 0| 1| 2| 0| 6| 0| 1|0.22|0.2727| 0.8| 0.0| 5| 27| 32|
| 4|2011-01-01 00:00:00| 1| 0| 1| 3| 0| 6| 0| 1|0.24|0.2879|0.75| 0.0| 3| 10| 13|
| 5|2011-01-01 00:00:00| 1| 0| 1| 4| 0| 6| 0| 1|0.24|0.2879|0.75| 0.0| 0| 1| 1|
| 6|2011-01-01 00:00:00| 1| 0| 1| 5| 0| 6| 0| 2|0.24|0.2576|0.75| 0.0896| 0| 1| 1|
| 7|2011-01-01 00:00:00| 1| 0| 1| 6| 0| 6| 0| 1|0.22|0.2727| 0.8| 0.0| 2| 0| 2|
| 8|2011-01-01 00:00:00| 1| 0| 1| 7| 0| 6| 0| 1| 0.2|0.2576|0.86| 0.0| 1| 2| 3|
| 9|2011-01-01 00:00:00| 1| 0| 1| 8| 0| 6| 0| 1|0.24|0.2879|0.75| 0.0| 1| 7| 8|
| 10|2011-01-01 00:00:00| 1| 0| 1| 9| 0| 6| 0| 1|0.32|0.3485|0.76| 0.0| 8| 6| 14|
| 11|2011-01-01 00:00:00| 1| 0| 1| 10| 0| 6| 0| 1|0.38|0.3939|0.76| 0.2537| 12| 24| 36|
| 12|2011-01-01 00:00:00| 1| 0| 1| 11| 0| 6| 0| 1|0.36|0.3333|0.81| 0.2836| 26| 30| 56|
| 13|2011-01-01 00:00:00| 1| 0| 1| 12| 0| 6| 0| 1|0.42|0.4242|0.77| 0.2836| 29| 55| 84|
| 14|2011-01-01 00:00:00| 1| 0| 1| 13| 0| 6| 0| 2|0.46|0.4545|0.72| 0.2985| 47| 47| 94|
| 15|2011-01-01 00:00:00| 1| 0| 1| 14| 0| 6| 0| 2|0.46|0.4545|0.72| 0.2836| 35| 71|106|
| 16|2011-01-01 00:00:00| 1| 0| 1| 15| 0| 6| 0| 2|0.44|0.4394|0.77| 0.2985| 40| 70|110|
| 17|2011-01-01 00:00:00| 1| 0| 1| 16| 0| 6| 0| 2|0.42|0.4242|0.82| 0.2985| 41| 52| 93|
| 18|2011-01-01 00:00:00| 1| 0| 1| 17| 0| 6| 0| 2|0.44|0.4394|0.82| 0.2836| 15| 52| 67|
| 19|2011-01-01 00:00:00| 1| 0| 1| 18| 0| 6| 0| 3|0.42|0.4242|0.88| 0.2537| 9| 26| 35|
| 20|2011-01-01 00:00:00| 1| 0| 1| 19| 0| 6| 0| 3|0.42|0.4242|0.88| 0.2537| 6| 31| 37|
+-------+-------------------+------+---+----+---+-------+-------+----------+----------+----+------+----+---------+------+----------+---+
"""
#查看样本基本统计信息
"""
df.describe().show()
+-------+-----------------+------------------+------------------+------------------+------------------+--------------------+-----------------+------------------+------------------+-------------------+------------------+-------------------+-------------------+-----------------+------------------+------------------+
|summary| instant| season| yr| mnth| hr| holiday| weekday| workingday| weathersit| temp| atemp| hum| windspeed| casual| registered| cnt|
+-------+-----------------+------------------+------------------+------------------+------------------+--------------------+-----------------+------------------+------------------+-------------------+------------------+-------------------+-------------------+-----------------+------------------+------------------+
| count| 17379| 17379| 17379| 17379| 17379| 17379| 17379| 17379| 17379| 17379| 17379| 17379| 17379| 17379| 17379| 17379|
| mean| 8690.0|2.5016399102364923|0.5025605615973301| 6.537775476149376|11.546751826917545|0.028770355026181024|3.003682605443351|0.6827205247712756| 1.425283387997008| 0.4969871684216586|0.4757751021347581| 0.6272288394038822| 0.1900976063064631|35.67621842453536|153.78686920996606|189.46308763450142|
| stddev|5017.029499614288| 1.10691813944808|0.5000078290910193|3.4387757137501724|6.9144050952644776| 0.1671652763843717|2.005771456110986|0.4654306335238818|0.6393568777542525|0.19255612124972202|0.1718502156353594|0.19292983406291458|0.12234022857279034|49.30503038705298|151.35728591258325| 181.3875990918646|
| min| 1| 1| 0| 1| 0| 0| 0| 0| 1| 0.02| 0.0| 0.0| 0.0| 0| 0| 1|
| max| 17379| 4| 1| 12| 23| 1| 6| 1| 4| 1.0| 1.0| 1.0| 0.8507| 367| 886| 977|
+-------+-----------------+------------------+------------------+------------------+------------------+--------------------+-----------------+------------------+------------------+-------------------+------------------+-------------------+-------------------+-----------------+------------------+------------------+
"""
#忽略记录中的instant、dteday,casual 和registered,最后就剩下12个自变量,1个因变量。其中前8个是类型变量,
# 后4个是归一化后的实数变量。对其中8个类型变量,我们使用之前提到的二元编码,剩下4个实数变量不做处理。
fieldIndex=["season","yr","mnth","hr","holiday","weekday","workingday","weathersit","temp","atemp","hum","windspeed","cnt"]
df=df[fieldIndex]
"""
df.show()
+------+---+----+---+-------+-------+----------+----------+----+------+----+---------+---+
|season| yr|mnth| hr|holiday|weekday|workingday|weathersit|temp| atemp| hum|windspeed|cnt|
+------+---+----+---+-------+-------+----------+----------+----+------+----+---------+---+
| 1| 0| 1| 0| 0| 6| 0| 1|0.24|0.2879|0.81| 0.0| 16|
| 1| 0| 1| 1| 0| 6| 0| 1|0.22|0.2727| 0.8| 0.0| 40|
| 1| 0| 1| 2| 0| 6| 0| 1|0.22|0.2727| 0.8| 0.0| 32|
| 1| 0| 1| 3| 0| 6| 0| 1|0.24|0.2879|0.75| 0.0| 13|
| 1| 0| 1| 4| 0| 6| 0| 1|0.24|0.2879|0.75| 0.0| 1|
| 1| 0| 1| 5| 0| 6| 0| 2|0.24|0.2576|0.75| 0.0896| 1|
| 1| 0| 1| 6| 0| 6| 0| 1|0.22|0.2727| 0.8| 0.0| 2|
| 1| 0| 1| 7| 0| 6| 0| 1| 0.2|0.2576|0.86| 0.0| 3|
| 1| 0| 1| 8| 0| 6| 0| 1|0.24|0.2879|0.75| 0.0| 8|
| 1| 0| 1| 9| 0| 6| 0| 1|0.32|0.3485|0.76| 0.0| 14|
| 1| 0| 1| 10| 0| 6| 0| 1|0.38|0.3939|0.76| 0.2537| 36|
| 1| 0| 1| 11| 0| 6| 0| 1|0.36|0.3333|0.81| 0.2836| 56|
| 1| 0| 1| 12| 0| 6| 0| 1|0.42|0.4242|0.77| 0.2836| 84|
| 1| 0| 1| 13| 0| 6| 0| 2|0.46|0.4545|0.72| 0.2985| 94|
| 1| 0| 1| 14| 0| 6| 0| 2|0.46|0.4545|0.72| 0.2836|106|
| 1| 0| 1| 15| 0| 6| 0| 2|0.44|0.4394|0.77| 0.2985|110|
| 1| 0| 1| 16| 0| 6| 0| 2|0.42|0.4242|0.82| 0.2985| 93|
| 1| 0| 1| 17| 0| 6| 0| 2|0.44|0.4394|0.82| 0.2836| 67|
| 1| 0| 1| 18| 0| 6| 0| 3|0.42|0.4242|0.88| 0.2537| 35|
| 1| 0| 1| 19| 0| 6| 0| 3|0.42|0.4242|0.88| 0.2537| 37|
+------+---+----+---+-------+-------+----------+----------+----+------+----+---------+---+
"""
# ---------------------------------------------------
#缺失值处理
# 查看各列有无缺失值
"""
for i in range(len(fieldIndex)):
df.filter('%s is null'%(fieldIndex[i])).select(fieldIndex[i]).limit(10).show()
+------+
|season|
+------+
+------+
+---+
| yr|
+---+
+---+
"""
# a=df.groupby().mean('temp').collect()
#mean方法只接受数值类型的参数,Int, Long等,如果是String, Date, Timestamp 类型的话要用agg(mean(“b”))
# meanTemp=df.agg({'temp': 'avg'}).collect()[0][0]
for i in range(len(fieldIndex)):
meanTemp = df.agg({fieldIndex[i]: 'avg'}).collect()[0][0]
df.na.fill({fieldIndex[i]:meanTemp})
#也可以采用pyspark.ml.feature.Imputer来处理缺失值;但是Imputer目前只能处理floattype和doubletype,所以要先进行类型转换,但还没找到方法。
# imputer=Imputer(strategy='mean',inputCols=['atemp'],outputCols=['out_atemp'])
# model=imputer.fit(df)
# df=model.transform(df)
"""
df.show()
+------+---+----+---+-------+-------+----------+----------+----+------+----+---------+---+---------+
|season| yr|mnth| hr|holiday|weekday|workingday|weathersit|temp| atemp| hum|windspeed|cnt|out_atemp|
+------+---+----+---+-------+-------+----------+----------+----+------+----+---------+---+---------+
| 1| 0| 1| 0| 0| 6| 0| 1|0.24|0.2879|0.81| 0.0| 16| 0.2879|
| 1| 0| 1| 1| 0| 6| 0| 1|0.22|0.2727| 0.8| 0.0| 40| 0.2727|
| 1| 0| 1| 2| 0| 6| 0| 1|0.22|0.2727| 0.8| 0.0| 32| 0.2727|
| 1| 0| 1| 3| 0| 6| 0| 1|0.24|0.2879|0.75| 0.0| 13| 0.2879|
| 1| 0| 1| 4| 0| 6| 0| 1|0.24|0.2879|0.75| 0.0| 1| 0.2879|
| 1| 0| 1| 5| 0| 6| 0| 2|0.24|0.2576|0.75| 0.0896| 1| 0.2576|
| 1| 0| 1| 6| 0| 6| 0| 1|0.22|0.2727| 0.8| 0.0| 2| 0.2727|
| 1| 0| 1| 7| 0| 6| 0| 1| 0.2|0.2576|0.86| 0.0| 3| 0.2576|
| 1| 0| 1| 8| 0| 6| 0| 1|0.24|0.2879|0.75| 0.0| 8| 0.2879|
| 1| 0| 1| 9| 0| 6| 0| 1|0.32|0.3485|0.76| 0.0| 14| 0.3485|
| 1| 0| 1| 10| 0| 6| 0| 1|0.38|0.3939|0.76| 0.2537| 36| 0.3939|
| 1| 0| 1| 11| 0| 6| 0| 1|0.36|0.3333|0.81| 0.2836| 56| 0.3333|
| 1| 0| 1| 12| 0| 6| 0| 1|0.42|0.4242|0.77| 0.2836| 84| 0.4242|
| 1| 0| 1| 13| 0| 6| 0| 2|0.46|0.4545|0.72| 0.2985| 94| 0.4545|
| 1| 0| 1| 14| 0| 6| 0| 2|0.46|0.4545|0.72| 0.2836|106| 0.4545|
| 1| 0| 1| 15| 0| 6| 0| 2|0.44|0.4394|0.77| 0.2985|110| 0.4394|
| 1| 0| 1| 16| 0| 6| 0| 2|0.42|0.4242|0.82| 0.2985| 93| 0.4242|
| 1| 0| 1| 17| 0| 6| 0| 2|0.44|0.4394|0.82| 0.2836| 67| 0.4394|
| 1| 0| 1| 18| 0| 6| 0| 3|0.42|0.4242|0.88| 0.2537| 35| 0.4242|
| 1| 0| 1| 19| 0| 6| 0| 3|0.42|0.4242|0.88| 0.2537| 37| 0.4242|
+------+---+----+---+-------+-------+----------+----------+----+------+----+---------+---+---------+
"""
# ---------------------------------------------------
# 特征变量哑变量化
#如果是决策树这一类算法,既可以处理离散值,又可以处理连续值,对特征变量不需要哑变量化,如果是线性回归这一类算法,
# 很明显,当我们用1,2,3,4去表示四季的影响时,几乎不可能夏天的影响是春的2倍,秋天的影响是春天的3倍这种关系。
# 这个时候如果进行哑变量化,样本对季节特征就变成了四个特征,均为0或1;表示是否是春天,是否是夏天,……
# 01本来是二值分布,也不是线性关系,但可以套用线性关系,因为线性关系中当自变量只能取两个值的时候,
# 因变量也只能取两个值,相当于一个二值判断。
#OneHotEncoder可以实现这一目的。
indexfieldIndex = list(map(lambda x: 'index_%s' % (x), fieldIndex[0:8]))
OHEfieldIndex=list(map(lambda x:'OHE_%s'%(x),fieldIndex[0:8]))
for i in range(8):
stringIndexer=StringIndexer(inputCol=fieldIndex[i],outputCol=indexfieldIndex[i])
# 这一部分的模型也需要存储,因为后面预测新样本的时候,也需要先进行转换,再拿转码后的数据放入模型。
model=stringIndexer.fit(df)
df=model.transform(df)
oneHotEncoder=OneHotEncoder(inputCol=indexfieldIndex[i],outputCol=OHEfieldIndex[i])
df=oneHotEncoder.transform(df)
"""
df.show()
+------+---+----+---+-------+-------+----------+----------+----+------+----+---------+---+------------+-------------+--------+---------+----------+---------------+--------+---------------+-------------+-------------+-------------+-------------+----------------+--------------+----------------+--------------+
|season| yr|mnth| hr|holiday|weekday|workingday|weathersit|temp| atemp| hum|windspeed|cnt|index_season| OHE_season|index_yr| OHE_yr|index_mnth| OHE_mnth|index_hr| OHE_hr|index_holiday| OHE_holiday|index_weekday| OHE_weekday|index_workingday|OHE_workingday|index_weathersit|OHE_weathersit|
+------+---+----+---+-------+-------+----------+----------+----+------+----+---------+---+------------+-------------+--------+---------+----------+---------------+--------+---------------+-------------+-------------+-------------+-------------+----------------+--------------+----------------+--------------+
| 1| 0| 1| 0| 0| 6| 0| 1|0.24|0.2879|0.81| 0.0| 16| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 17.0|(23,[17],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|
| 1| 0| 1| 1| 0| 6| 0| 1|0.22|0.2727| 0.8| 0.0| 40| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 19.0|(23,[19],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|
| 1| 0| 1| 2| 0| 6| 0| 1|0.22|0.2727| 0.8| 0.0| 32| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 21.0|(23,[21],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|
| 1| 0| 1| 3| 0| 6| 0| 1|0.24|0.2879|0.75| 0.0| 13| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 23.0| (23,[],[])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|
| 1| 0| 1| 4| 0| 6| 0| 1|0.24|0.2879|0.75| 0.0| 1| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 22.0|(23,[22],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|
| 1| 0| 1| 5| 0| 6| 0| 2|0.24|0.2576|0.75| 0.0896| 1| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 20.0|(23,[20],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|
| 1| 0| 1| 6| 0| 6| 0| 1|0.22|0.2727| 0.8| 0.0| 2| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 18.0|(23,[18],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|
| 1| 0| 1| 7| 0| 6| 0| 1| 0.2|0.2576|0.86| 0.0| 3| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 16.0|(23,[16],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|
| 1| 0| 1| 8| 0| 6| 0| 1|0.24|0.2879|0.75| 0.0| 8| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 12.0|(23,[12],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|
| 1| 0| 1| 9| 0| 6| 0| 1|0.32|0.3485|0.76| 0.0| 14| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 14.0|(23,[14],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|
| 1| 0| 1| 10| 0| 6| 0| 1|0.38|0.3939|0.76| 0.2537| 36| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 15.0|(23,[15],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|
| 1| 0| 1| 11| 0| 6| 0| 1|0.36|0.3333|0.81| 0.2836| 56| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 13.0|(23,[13],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|
| 1| 0| 1| 12| 0| 6| 0| 1|0.42|0.4242|0.77| 0.2836| 84| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 5.0| (23,[5],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|
| 1| 0| 1| 13| 0| 6| 0| 2|0.46|0.4545|0.72| 0.2985| 94| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 3.0| (23,[3],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|
| 1| 0| 1| 14| 0| 6| 0| 2|0.46|0.4545|0.72| 0.2836|106| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 4.0| (23,[4],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|
| 1| 0| 1| 15| 0| 6| 0| 2|0.44|0.4394|0.77| 0.2985|110| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 2.0| (23,[2],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|
| 1| 0| 1| 16| 0| 6| 0| 2|0.42|0.4242|0.82| 0.2985| 93| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 0.0| (23,[0],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|
| 1| 0| 1| 17| 0| 6| 0| 2|0.44|0.4394|0.82| 0.2836| 67| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 1.0| (23,[1],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|
| 1| 0| 1| 18| 0| 6| 0| 3|0.42|0.4242|0.88| 0.2537| 35| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 11.0|(23,[11],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 2.0| (3,[2],[1.0])|
| 1| 0| 1| 19| 0| 6| 0| 3|0.42|0.4242|0.88| 0.2537| 37| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 6.0| (23,[6],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 2.0| (3,[2],[1.0])|
+------+---+----+---+-------+-------+----------+----------+----+------+----+---------+---+------------+-------------+--------+---------+----------+---------------+--------+---------------+-------------+-------------+-------------+-------------+----------------+--------------+----------------+--------------+
"""
# ---------------------------------------------------
#将多个列合并成一个列,因为ml建模时的特殊要求
OHEfieldIndex.extend(["temp", "atemp", "hum","windspeed"])
vectorAssembler=VectorAssembler(inputCols=OHEfieldIndex,outputCol='features')
df=vectorAssembler.transform(df)
"""
df.show()
+------+---+----+---+-------+-------+----------+----------+----+------+----+---------+---+------------+-------------+--------+---------+----------+---------------+--------+---------------+-------------+-------------+-------------+-------------+----------------+--------------+----------------+--------------+--------------------+
|season| yr|mnth| hr|holiday|weekday|workingday|weathersit|temp| atemp| hum|windspeed|cnt|index_season| OHE_season|index_yr| OHE_yr|index_mnth| OHE_mnth|index_hr| OHE_hr|index_holiday| OHE_holiday|index_weekday| OHE_weekday|index_workingday|OHE_workingday|index_weathersit|OHE_weathersit| features|
+------+---+----+---+-------+-------+----------+----------+----+------+----+---------+---+------------+-------------+--------+---------+----------+---------------+--------+---------------+-------------+-------------+-------------+-------------+----------------+--------------+----------------+--------------+--------------------+
| 1| 0| 1| 0| 0| 6| 0| 1|0.24|0.2879|0.81| 0.0| 16| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 17.0|(23,[17],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,32,38,3...|
| 1| 0| 1| 1| 0| 6| 0| 1|0.22|0.2727| 0.8| 0.0| 40| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 19.0|(23,[19],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,34,38,3...|
| 1| 0| 1| 2| 0| 6| 0| 1|0.22|0.2727| 0.8| 0.0| 32| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 21.0|(23,[21],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,36,38,3...|
| 1| 0| 1| 3| 0| 6| 0| 1|0.24|0.2879|0.75| 0.0| 13| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 23.0| (23,[],[])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,38,39,4...|
| 1| 0| 1| 4| 0| 6| 0| 1|0.24|0.2879|0.75| 0.0| 1| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 22.0|(23,[22],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,37,38,3...|
| 1| 0| 1| 5| 0| 6| 0| 2|0.24|0.2576|0.75| 0.0896| 1| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 20.0|(23,[20],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|(53,[2,14,35,38,3...|
| 1| 0| 1| 6| 0| 6| 0| 1|0.22|0.2727| 0.8| 0.0| 2| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 18.0|(23,[18],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,33,38,3...|
| 1| 0| 1| 7| 0| 6| 0| 1| 0.2|0.2576|0.86| 0.0| 3| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 16.0|(23,[16],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,31,38,3...|
| 1| 0| 1| 8| 0| 6| 0| 1|0.24|0.2879|0.75| 0.0| 8| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 12.0|(23,[12],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,27,38,3...|
| 1| 0| 1| 9| 0| 6| 0| 1|0.32|0.3485|0.76| 0.0| 14| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 14.0|(23,[14],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,29,38,3...|
| 1| 0| 1| 10| 0| 6| 0| 1|0.38|0.3939|0.76| 0.2537| 36| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 15.0|(23,[15],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,30,38,3...|
| 1| 0| 1| 11| 0| 6| 0| 1|0.36|0.3333|0.81| 0.2836| 56| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 13.0|(23,[13],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,28,38,3...|
| 1| 0| 1| 12| 0| 6| 0| 1|0.42|0.4242|0.77| 0.2836| 84| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 5.0| (23,[5],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,20,38,3...|
| 1| 0| 1| 13| 0| 6| 0| 2|0.46|0.4545|0.72| 0.2985| 94| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 3.0| (23,[3],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|(53,[2,14,18,38,3...|
| 1| 0| 1| 14| 0| 6| 0| 2|0.46|0.4545|0.72| 0.2836|106| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 4.0| (23,[4],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|(53,[2,14,19,38,3...|
| 1| 0| 1| 15| 0| 6| 0| 2|0.44|0.4394|0.77| 0.2985|110| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 2.0| (23,[2],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|(53,[2,14,17,38,3...|
| 1| 0| 1| 16| 0| 6| 0| 2|0.42|0.4242|0.82| 0.2985| 93| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 0.0| (23,[0],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|(53,[2,14,15,38,3...|
| 1| 0| 1| 17| 0| 6| 0| 2|0.44|0.4394|0.82| 0.2836| 67| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 1.0| (23,[1],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|(53,[2,14,16,38,3...|
| 1| 0| 1| 18| 0| 6| 0| 3|0.42|0.4242|0.88| 0.2537| 35| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 11.0|(23,[11],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 2.0| (3,[2],[1.0])|(53,[2,14,26,38,3...|
| 1| 0| 1| 19| 0| 6| 0| 3|0.42|0.4242|0.88| 0.2537| 37| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 6.0| (23,[6],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 2.0| (3,[2],[1.0])|(53,[2,14,21,38,3...|
+------+---+----+---+-------+-------+----------+----------+----+------+----+---------+---+------------+-------------+--------+---------+----------+---------------+--------+---------------+-------------+-------------+-------------+-------------+----------------+--------------+----------------+--------------+--------------------+
"""
# ---------------------------------------------------
# 回归模型的训练和应用
##使用线性回归建模
# linearRegression=LinearRegression(featuresCol='features',labelCol='cnt')
# modelLR=linearRegression.fit(df)
# dfPLR=modelLR.transform(df)
"""
dfPLR.show()
+------+---+----+---+-------+-------+----------+----------+----+------+----+---------+---+------------+-------------+--------+---------+----------+---------------+--------+---------------+-------------+-------------+-------------+-------------+----------------+--------------+----------------+--------------+--------------------+-------------------+
|season| yr|mnth| hr|holiday|weekday|workingday|weathersit|temp| atemp| hum|windspeed|cnt|index_season| OHE_season|index_yr| OHE_yr|index_mnth| OHE_mnth|index_hr| OHE_hr|index_holiday| OHE_holiday|index_weekday| OHE_weekday|index_workingday|OHE_workingday|index_weathersit|OHE_weathersit| features| prediction|
+------+---+----+---+-------+-------+----------+----------+----+------+----+---------+---+------------+-------------+--------+---------+----------+---------------+--------+---------------+-------------+-------------+-------------+-------------+----------------+--------------+----------------+--------------+--------------------+-------------------+
| 1| 0| 1| 0| 0| 6| 0| 1|0.24|0.2879|0.81| 0.0| 16| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 17.0|(23,[17],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,32,38,3...| -69.82760315259867|
| 1| 0| 1| 1| 0| 6| 0| 1|0.22|0.2727| 0.8| 0.0| 40| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 19.0|(23,[19],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,34,38,3...| -90.56601780597488|
| 1| 0| 1| 2| 0| 6| 0| 1|0.22|0.2727| 0.8| 0.0| 32| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 21.0|(23,[21],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,36,38,3...| -99.64051354773953|
| 1| 0| 1| 3| 0| 6| 0| 1|0.24|0.2879|0.75| 0.0| 13| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 23.0| (23,[],[])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,38,39,4...|-101.97989107745629|
| 1| 0| 1| 4| 0| 6| 0| 1|0.24|0.2879|0.75| 0.0| 1| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 22.0|(23,[22],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,37,38,3...|-105.12230524074032|
| 1| 0| 1| 5| 0| 6| 0| 2|0.24|0.2576|0.75| 0.0896| 1| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 20.0|(23,[20],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|(53,[2,14,35,38,3...|-105.26471208431013|
| 1| 0| 1| 6| 0| 6| 0| 1|0.22|0.2727| 0.8| 0.0| 2| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 18.0|(23,[18],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,33,38,3...|-37.879480636533586|
| 1| 0| 1| 7| 0| 6| 0| 1| 0.2|0.2576|0.86| 0.0| 3| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 16.0|(23,[16],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,31,38,3...| 87.91825641877601|
| 1| 0| 1| 8| 0| 6| 0| 1|0.24|0.2879|0.75| 0.0| 8| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 12.0|(23,[12],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,27,38,3...| 245.9411243338609|
| 1| 0| 1| 9| 0| 6| 0| 1|0.32|0.3485|0.76| 0.0| 14| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 14.0|(23,[14],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,29,38,3...| 114.47618441083638|
| 1| 0| 1| 10| 0| 6| 0| 1|0.38|0.3939|0.76| 0.2537| 36| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 15.0|(23,[15],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,30,38,3...| 65.21445149287273|
| 1| 0| 1| 11| 0| 6| 0| 1|0.36|0.3333|0.81| 0.2836| 56| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 13.0|(23,[13],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,28,38,3...| 75.51194109500922|
| 1| 0| 1| 12| 0| 6| 0| 1|0.42|0.4242|0.77| 0.2836| 84| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 5.0| (23,[5],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,20,38,3...| 136.74367287689842|
| 1| 0| 1| 13| 0| 6| 0| 2|0.46|0.4545|0.72| 0.2985| 94| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 3.0| (23,[3],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|(53,[2,14,18,38,3...| 133.5319081633806|
| 1| 0| 1| 14| 0| 6| 0| 2|0.46|0.4545|0.72| 0.2836|106| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 4.0| (23,[4],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|(53,[2,14,19,38,3...| 118.11328636895162|
| 1| 0| 1| 15| 0| 6| 0| 2|0.44|0.4394|0.77| 0.2985|110| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 2.0| (23,[2],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|(53,[2,14,17,38,3...| 118.73778327268012|
| 1| 0| 1| 16| 0| 6| 0| 2|0.42|0.4242|0.82| 0.2985| 93| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 0.0| (23,[0],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|(53,[2,14,15,38,3...| 172.45210094866366|
| 1| 0| 1| 17| 0| 6| 0| 2|0.44|0.4394|0.82| 0.2836| 67| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 1.0| (23,[1],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|(53,[2,14,16,38,3...| 330.86006676949586|
| 1| 0| 1| 18| 0| 6| 0| 3|0.42|0.4242|0.88| 0.2537| 35| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 11.0|(23,[11],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 2.0| (3,[2],[1.0])|(53,[2,14,26,38,3...| 235.76239510966354|
| 1| 0| 1| 19| 0| 6| 0| 3|0.42|0.4242|0.88| 0.2537| 37| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 6.0| (23,[6],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 2.0| (3,[2],[1.0])|(53,[2,14,21,38,3...| 127.0948052399081|
+------+---+----+---+-------+-------+----------+----------+----+------+----+---------+---+------------+-------------+--------+---------+----------+---------------+--------+---------------+-------------+-------------+-------------+-------------+----------------+--------------+----------------+--------------+--------------------+-------------------+
"""
##使用决策树建模
# decisionTreeRegression=DecisionTreeRegressor(featuresCol='features',labelCol='cnt',maxDepth=7)
# modelDT=decisionTreeRegression.fit(df)
# dfPDT=modelDT.transform(df)
"""
dfPDT.show()
+------+---+----+---+-------+-------+----------+----------+----+------+----+---------+---+------------+-------------+--------+---------+----------+---------------+--------+---------------+-------------+-------------+-------------+-------------+----------------+--------------+----------------+--------------+--------------------+------------------+
|season| yr|mnth| hr|holiday|weekday|workingday|weathersit|temp| atemp| hum|windspeed|cnt|index_season| OHE_season|index_yr| OHE_yr|index_mnth| OHE_mnth|index_hr| OHE_hr|index_holiday| OHE_holiday|index_weekday| OHE_weekday|index_workingday|OHE_workingday|index_weathersit|OHE_weathersit| features| prediction|
+------+---+----+---+-------+-------+----------+----------+----+------+----+---------+---+------------+-------------+--------+---------+----------+---------------+--------+---------------+-------------+-------------+-------------+-------------+----------------+--------------+----------------+--------------+--------------------+------------------+
| 1| 0| 1| 0| 0| 6| 0| 1|0.24|0.2879|0.81| 0.0| 16| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 17.0|(23,[17],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,32,38,3...| 47.92076830732293|
| 1| 0| 1| 1| 0| 6| 0| 1|0.22|0.2727| 0.8| 0.0| 40| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 19.0|(23,[19],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,34,38,3...| 47.92076830732293|
| 1| 0| 1| 2| 0| 6| 0| 1|0.22|0.2727| 0.8| 0.0| 32| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 21.0|(23,[21],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,36,38,3...| 47.92076830732293|
| 1| 0| 1| 3| 0| 6| 0| 1|0.24|0.2879|0.75| 0.0| 13| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 23.0| (23,[],[])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,38,39,4...| 47.92076830732293|
| 1| 0| 1| 4| 0| 6| 0| 1|0.24|0.2879|0.75| 0.0| 1| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 22.0|(23,[22],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,37,38,3...| 47.92076830732293|
| 1| 0| 1| 5| 0| 6| 0| 2|0.24|0.2576|0.75| 0.0896| 1| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 20.0|(23,[20],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|(53,[2,14,35,38,3...| 47.92076830732293|
| 1| 0| 1| 6| 0| 6| 0| 1|0.22|0.2727| 0.8| 0.0| 2| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 18.0|(23,[18],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,33,38,3...| 47.92076830732293|
| 1| 0| 1| 7| 0| 6| 0| 1| 0.2|0.2576|0.86| 0.0| 3| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 16.0|(23,[16],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,31,38,3...| 47.92076830732293|
| 1| 0| 1| 8| 0| 6| 0| 1|0.24|0.2879|0.75| 0.0| 8| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 12.0|(23,[12],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,27,38,3...| 34.06666666666667|
| 1| 0| 1| 9| 0| 6| 0| 1|0.32|0.3485|0.76| 0.0| 14| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 14.0|(23,[14],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,29,38,3...| 79.68392737054472|
| 1| 0| 1| 10| 0| 6| 0| 1|0.38|0.3939|0.76| 0.2537| 36| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 15.0|(23,[15],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,30,38,3...|107.08616944243302|
| 1| 0| 1| 11| 0| 6| 0| 1|0.36|0.3333|0.81| 0.2836| 56| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 13.0|(23,[13],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,28,38,3...|107.08616944243302|
| 1| 0| 1| 12| 0| 6| 0| 1|0.42|0.4242|0.77| 0.2836| 84| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 5.0| (23,[5],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,20,38,3...|107.08616944243302|
| 1| 0| 1| 13| 0| 6| 0| 2|0.46|0.4545|0.72| 0.2985| 94| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 3.0| (23,[3],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|(53,[2,14,18,38,3...|107.08616944243302|
| 1| 0| 1| 14| 0| 6| 0| 2|0.46|0.4545|0.72| 0.2836|106| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 4.0| (23,[4],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|(53,[2,14,19,38,3...|107.08616944243302|
| 1| 0| 1| 15| 0| 6| 0| 2|0.44|0.4394|0.77| 0.2985|110| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 2.0| (23,[2],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|(53,[2,14,17,38,3...|107.08616944243302|
| 1| 0| 1| 16| 0| 6| 0| 2|0.42|0.4242|0.82| 0.2985| 93| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 0.0| (23,[0],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|(53,[2,14,15,38,3...|107.08616944243302|
| 1| 0| 1| 17| 0| 6| 0| 2|0.44|0.4394|0.82| 0.2836| 67| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 1.0| (23,[1],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|(53,[2,14,16,38,3...|113.33333333333333|
| 1| 0| 1| 18| 0| 6| 0| 3|0.42|0.4242|0.88| 0.2537| 35| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 11.0|(23,[11],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 2.0| (3,[2],[1.0])|(53,[2,14,26,38,3...|193.22033898305085|
| 1| 0| 1| 19| 0| 6| 0| 3|0.42|0.4242|0.88| 0.2537| 37| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 6.0| (23,[6],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 2.0| (3,[2],[1.0])|(53,[2,14,21,38,3...|107.08616944243302|
+------+---+----+---+-------+-------+----------+----------+----+------+----+---------+---+------------+-------------+--------+---------+----------+---------------+--------+---------------+-------------+-------------+-------------+-------------+----------------+--------------+----------------+--------------+--------------------+------------------+
"""
##使用GBDT建模
# gbdtRegression=GBTRegressor(featuresCol='features',labelCol='cnt',maxDepth=7)
# modelGBDT=gbdtRegression.fit(df)
# dfPGBDT=modelGBDT.transform(df)
"""
dfPGBDT.show()
+------+---+----+---+-------+-------+----------+----------+----+------+----+---------+---+------------+-------------+--------+---------+----------+---------------+--------+---------------+-------------+-------------+-------------+-------------+----------------+--------------+----------------+--------------+--------------------+-------------------+
|season| yr|mnth| hr|holiday|weekday|workingday|weathersit|temp| atemp| hum|windspeed|cnt|index_season| OHE_season|index_yr| OHE_yr|index_mnth| OHE_mnth|index_hr| OHE_hr|index_holiday| OHE_holiday|index_weekday| OHE_weekday|index_workingday|OHE_workingday|index_weathersit|OHE_weathersit| features| prediction|
+------+---+----+---+-------+-------+----------+----------+----+------+----+---------+---+------------+-------------+--------+---------+----------+---------------+--------+---------------+-------------+-------------+-------------+-------------+----------------+--------------+----------------+--------------+--------------------+-------------------+
| 1| 0| 1| 0| 0| 6| 0| 1|0.24|0.2879|0.81| 0.0| 16| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 17.0|(23,[17],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,32,38,3...| 15.410705577561586|
| 1| 0| 1| 1| 0| 6| 0| 1|0.22|0.2727| 0.8| 0.0| 40| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 19.0|(23,[19],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,34,38,3...| 16.736143146381238|
| 1| 0| 1| 2| 0| 6| 0| 1|0.22|0.2727| 0.8| 0.0| 32| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 21.0|(23,[21],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,36,38,3...| 14.014961402078214|
| 1| 0| 1| 3| 0| 6| 0| 1|0.24|0.2879|0.75| 0.0| 13| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 23.0| (23,[],[])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,38,39,4...| 34.21484096264253|
| 1| 0| 1| 4| 0| 6| 0| 1|0.24|0.2879|0.75| 0.0| 1| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 22.0|(23,[22],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,37,38,3...|-0.7990732457094731|
| 1| 0| 1| 5| 0| 6| 0| 2|0.24|0.2576|0.75| 0.0896| 1| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 20.0|(23,[20],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|(53,[2,14,35,38,3...| 6.860566741488303|
| 1| 0| 1| 6| 0| 6| 0| 1|0.22|0.2727| 0.8| 0.0| 2| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 18.0|(23,[18],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,33,38,3...| 20.94779578677474|
| 1| 0| 1| 7| 0| 6| 0| 1| 0.2|0.2576|0.86| 0.0| 3| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 16.0|(23,[16],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,31,38,3...| 35.88782058004453|
| 1| 0| 1| 8| 0| 6| 0| 1|0.24|0.2879|0.75| 0.0| 8| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 12.0|(23,[12],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,27,38,3...| 13.961641539110758|
| 1| 0| 1| 9| 0| 6| 0| 1|0.32|0.3485|0.76| 0.0| 14| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 14.0|(23,[14],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,29,38,3...| 57.89930076822208|
| 1| 0| 1| 10| 0| 6| 0| 1|0.38|0.3939|0.76| 0.2537| 36| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 15.0|(23,[15],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,30,38,3...| 77.87782484597635|
| 1| 0| 1| 11| 0| 6| 0| 1|0.36|0.3333|0.81| 0.2836| 56| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 13.0|(23,[13],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,28,38,3...| 107.94875348317422|
| 1| 0| 1| 12| 0| 6| 0| 1|0.42|0.4242|0.77| 0.2836| 84| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 5.0| (23,[5],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,20,38,3...| 142.21244844758414|
| 1| 0| 1| 13| 0| 6| 0| 2|0.46|0.4545|0.72| 0.2985| 94| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 3.0| (23,[3],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|(53,[2,14,18,38,3...| 158.29891432762287|
| 1| 0| 1| 14| 0| 6| 0| 2|0.46|0.4545|0.72| 0.2836|106| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 4.0| (23,[4],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|(53,[2,14,19,38,3...| 136.83934564245524|
| 1| 0| 1| 15| 0| 6| 0| 2|0.44|0.4394|0.77| 0.2985|110| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 2.0| (23,[2],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|(53,[2,14,17,38,3...| 186.50661398287056|
| 1| 0| 1| 16| 0| 6| 0| 2|0.42|0.4242|0.82| 0.2985| 93| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 0.0| (23,[0],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|(53,[2,14,15,38,3...| 103.4078727190075|
| 1| 0| 1| 17| 0| 6| 0| 2|0.44|0.4394|0.82| 0.2836| 67| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 1.0| (23,[1],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|(53,[2,14,16,38,3...| 80.37085593116298|
| 1| 0| 1| 18| 0| 6| 0| 3|0.42|0.4242|0.88| 0.2537| 35| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 11.0|(23,[11],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 2.0| (3,[2],[1.0])|(53,[2,14,26,38,3...| 124.77327702177108|
| 1| 0| 1| 19| 0| 6| 0| 3|0.42|0.4242|0.88| 0.2537| 37| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 6.0| (23,[6],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 2.0| (3,[2],[1.0])|(53,[2,14,21,38,3...| 59.31441970186217|
+------+---+----+---+-------+-------+----------+----------+----+------+----+---------+---+------------+-------------+--------+---------+----------+---------------+--------+---------------+-------------+-------------+-------------+-------------+----------------+--------------+----------------+--------------+--------------------+-------------------+
"""
# ---------------------------------------------------
#评估回归模型的性能
# ##线性模型
# regressionEvaluator=RegressionEvaluator(labelCol='cnt')
# meanCnt=df.agg({'cnt': 'avg'}).collect()[0][0]
# print(meanCnt)
# #189.46308763450142
# ###均方误差
# print(regressionEvaluator.evaluate(dfPLR,{regressionEvaluator.metricName:'mse'}))
# #10318.703427923037
# print(regressionEvaluator.evaluate(dfPLR, {regressionEvaluator.metricName: 'mse'}) / (meanCnt ** 2))
# #0.2874589992073829
# ###均方根误差
# print(regressionEvaluator.evaluate(dfPLR,{regressionEvaluator.metricName:'rmse'}))
# #101.58101903369072
# print(regressionEvaluator.evaluate(dfPLR, {regressionEvaluator.metricName: 'rmse'}) / meanCnt)
# #0.5361520299386946
# ###平均绝对误差
# regressionEvaluator.setMetricName('mae')
# print(regressionEvaluator.evaluate(dfPLR))
# #75.2679259295139
# print(regressionEvaluator.evaluate(dfPLR) / meanCnt)
# #0.39726960469849076
# ###R - 平方系数
# regressionEvaluator.setMetricName('r2')
# print(regressionEvaluator.evaluate(dfPLR))
# #0.6863574789309435
##决策树
###均方误差
###均方根误差
###平均绝对误差
###R - 平方系数
# ---------------------------------------------------
#改进模型性能和参数调优
##目标变量变换对模型的影响
#特征变换和选择对模型性能有巨大的影响。许多机器学习模型都会假设输入数据和目标变量的分布,
# 比如线性模型的假设为正态分布。但是实际情况中线性回归的这种假设不成立的,为了更好地理解目标变量的分布,
# 最好的方法是画出目标变量的分布直方图。会发生cnt这个数据呈长尾分布,0-40的数量特别巨大,然后逐渐衰减。
#解决该问题的一种方法是对目标变量进行变换,比如用目标值的对数代替原始值,
# 通常称为对数变换(这种变换也可以用到特征值上)。绘制对目标变量进行对数变换后的分布直方图会发现,
# 变换后呈负偏态分布,但是已经较接近正态分布。
#也可以尝试其他变换如平方根变换
#在ml中对变量进行处理的时候,我们首先会使用ml.feature下面的类,确实也能够完成很多功能;但功能毕竟是有限的,
# 比如我想对某一列做对数变换,此时ml.feature无法实现,那么我们可以利用sparl.sql.functions下的诸多函数完成
# 我们需要的转换,这种转换往往会生成一个新的列,虽然像这样df.select(log(df.cnt))单独拿出来是个含有一列的数据框。
# 但是可以如下用df.withColumn将这个列加到原数据框中,这时会生成一个新的数据框。
df=df.withColumn('cntLog',log(df.cnt))
"""
df.show()
+------+---+----+---+-------+-------+----------+----------+----+------+----+---------+---+------------+-------------+--------+---------+----------+---------------+--------+---------------+-------------+-------------+-------------+-------------+----------------+--------------+----------------+--------------+--------------------+------------------+
|season| yr|mnth| hr|holiday|weekday|workingday|weathersit|temp| atemp| hum|windspeed|cnt|index_season| OHE_season|index_yr| OHE_yr|index_mnth| OHE_mnth|index_hr| OHE_hr|index_holiday| OHE_holiday|index_weekday| OHE_weekday|index_workingday|OHE_workingday|index_weathersit|OHE_weathersit| features| cntLog|
+------+---+----+---+-------+-------+----------+----------+----+------+----+---------+---+------------+-------------+--------+---------+----------+---------------+--------+---------------+-------------+-------------+-------------+-------------+----------------+--------------+----------------+--------------+--------------------+------------------+
| 1| 0| 1| 0| 0| 6| 0| 1|0.24|0.2879|0.81| 0.0| 16| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 17.0|(23,[17],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,32,38,3...| 2.772588722239781|
| 1| 0| 1| 1| 0| 6| 0| 1|0.22|0.2727| 0.8| 0.0| 40| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 19.0|(23,[19],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,34,38,3...|3.6888794541139363|
| 1| 0| 1| 2| 0| 6| 0| 1|0.22|0.2727| 0.8| 0.0| 32| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 21.0|(23,[21],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,36,38,3...|3.4657359027997265|
| 1| 0| 1| 3| 0| 6| 0| 1|0.24|0.2879|0.75| 0.0| 13| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 23.0| (23,[],[])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,38,39,4...|2.5649493574615367|
| 1| 0| 1| 4| 0| 6| 0| 1|0.24|0.2879|0.75| 0.0| 1| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 22.0|(23,[22],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,37,38,3...| 0.0|
| 1| 0| 1| 5| 0| 6| 0| 2|0.24|0.2576|0.75| 0.0896| 1| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 20.0|(23,[20],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|(53,[2,14,35,38,3...| 0.0|
| 1| 0| 1| 6| 0| 6| 0| 1|0.22|0.2727| 0.8| 0.0| 2| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 18.0|(23,[18],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,33,38,3...|0.6931471805599453|
| 1| 0| 1| 7| 0| 6| 0| 1| 0.2|0.2576|0.86| 0.0| 3| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 16.0|(23,[16],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,31,38,3...|1.0986122886681098|
| 1| 0| 1| 8| 0| 6| 0| 1|0.24|0.2879|0.75| 0.0| 8| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 12.0|(23,[12],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,27,38,3...|2.0794415416798357|
| 1| 0| 1| 9| 0| 6| 0| 1|0.32|0.3485|0.76| 0.0| 14| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 14.0|(23,[14],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,29,38,3...|2.6390573296152584|
| 1| 0| 1| 10| 0| 6| 0| 1|0.38|0.3939|0.76| 0.2537| 36| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 15.0|(23,[15],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,30,38,3...| 3.58351893845611|
| 1| 0| 1| 11| 0| 6| 0| 1|0.36|0.3333|0.81| 0.2836| 56| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 13.0|(23,[13],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,28,38,3...| 4.02535169073515|
| 1| 0| 1| 12| 0| 6| 0| 1|0.42|0.4242|0.77| 0.2836| 84| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 5.0| (23,[5],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,20,38,3...| 4.430816798843313|
| 1| 0| 1| 13| 0| 6| 0| 2|0.46|0.4545|0.72| 0.2985| 94| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 3.0| (23,[3],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|(53,[2,14,18,38,3...| 4.543294782270004|
| 1| 0| 1| 14| 0| 6| 0| 2|0.46|0.4545|0.72| 0.2836|106| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 4.0| (23,[4],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|(53,[2,14,19,38,3...| 4.663439094112067|
| 1| 0| 1| 15| 0| 6| 0| 2|0.44|0.4394|0.77| 0.2985|110| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 2.0| (23,[2],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|(53,[2,14,17,38,3...| 4.700480365792417|
| 1| 0| 1| 16| 0| 6| 0| 2|0.42|0.4242|0.82| 0.2985| 93| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 0.0| (23,[0],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|(53,[2,14,15,38,3...| 4.532599493153256|
| 1| 0| 1| 17| 0| 6| 0| 2|0.44|0.4394|0.82| 0.2836| 67| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 1.0| (23,[1],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|(53,[2,14,16,38,3...| 4.204692619390966|
| 1| 0| 1| 18| 0| 6| 0| 3|0.42|0.4242|0.88| 0.2537| 35| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 11.0|(23,[11],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 2.0| (3,[2],[1.0])|(53,[2,14,26,38,3...|3.5553480614894135|
| 1| 0| 1| 19| 0| 6| 0| 3|0.42|0.4242|0.88| 0.2537| 37| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 6.0| (23,[6],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 2.0| (3,[2],[1.0])|(53,[2,14,21,38,3...|3.6109179126442243|
+------+---+----+---+-------+-------+----------+----------+----+------+----+---------+---+------------+-------------+--------+---------+----------+---------------+--------+---------------+-------------+-------------+-------------+-------------+----------------+--------------+----------------+--------------+--------------------+------------------+
"""
#再回归建模
# linearRegression=LinearRegression(labelCol='cntLog')
# modelLR=linearRegression.fit(df)
# dfPLR=modelLR.transform(df)
# regressionEvaluator=RegressionEvaluator(labelCol='cntLog')
# meanCntLog=df.agg({'cntLog':'avg'}).collect()[0][0]
# print(meanCntLog)
# #4.536082104443427
# print('mse:%f'%(regressionEvaluator.evaluate(dfPLR, params={regressionEvaluator.metricName: 'mse'})))
# #mse:0.388590
# print('mse比例:%f'%(regressionEvaluator.evaluate(dfPLR,params={regressionEvaluator.metricName:'mse'})/meanCntLog**2))
# #mse比例: 0.018886
# print('rmse:%f'%(regressionEvaluator.evaluate(dfPLR,params={regressionEvaluator.metricName:'rmse'})))
# #rmse:0.623370
# print('rmse比例:%f' % (regressionEvaluator.evaluate(dfPLR, params={regressionEvaluator.metricName: 'rmse'})/meanCntLog))
# #rmse比例:0.137425
# print('mae:%f' % (regressionEvaluator.evaluate(dfPLR, params={regressionEvaluator.metricName: 'mae'})))
# #mae: 0.462741
# print('mae比例:%f' % (
# regressionEvaluator.evaluate(dfPLR, params={regressionEvaluator.metricName: 'mae'}) / meanCntLog))
# #mae比例:0.102013
# print('r2:%f' % (regressionEvaluator.evaluate(dfPLR, params={regressionEvaluator.metricName: 'r2'})))
# #r2: 0.824046
# #初步从结果来看,经过对数变换,结果有所改善,当然如果要真实对比改善效果还要将对数结果变换回去,这里就先不做了。
##模型参数调优
#使用交叉验证方法来评估不同参数对模型性能的影响。
###参数设置对线性模型的影响
#下面研究不同参数设置对模型性能的影响,首先需要为线性模型设置一个评估方法
regressionEvaluator=RegressionEvaluator(predictionCol='prediction',labelCol='cntLog')
linearRegression=LinearRegression(labelCol='cntLog')
###迭代次数对模型的影响:
grid=ParamGridBuilder().addGrid(param=linearRegression.maxIter,values=list(range(3,203,10))).build()
cv=CrossValidator(estimator=linearRegression,estimatorParamMaps=grid,evaluator=regressionEvaluator)
cvModel=cv.fit(df)
#print(cvModel.params)
#[Param(parent='CrossValidatorModel_4718a451c28f350c1493', name='estimator', doc='estimator to be cross-validated'), Param(parent='CrossValidatorModel_4718a451c28f350c1493', name='estimatorParamMaps', doc='estimator param maps'), Param(parent='CrossValidatorModel_4718a451c28f350c1493', name='evaluator', doc='evaluator used to select hyper-parameters that maximize the validator metric'), Param(parent='CrossValidatorModel_4718a451c28f350c1493', name='seed', doc='random seed.')]
#获取最优模型
bestLRModel=cvModel.bestModel
#print(bestLRModel.params)
#[]#这里还需要再看看怎么获取模型的参数
dfPCV=cvModel.transform(df)
"""
dfPCV.show()
+------+---+----+---+-------+-------+----------+----------+----+------+----+---------+---+------------+-------------+--------+---------+----------+---------------+--------+---------------+-------------+-------------+-------------+-------------+----------------+--------------+----------------+--------------+--------------------+------------------+------------------+
|season| yr|mnth| hr|holiday|weekday|workingday|weathersit|temp| atemp| hum|windspeed|cnt|index_season| OHE_season|index_yr| OHE_yr|index_mnth| OHE_mnth|index_hr| OHE_hr|index_holiday| OHE_holiday|index_weekday| OHE_weekday|index_workingday|OHE_workingday|index_weathersit|OHE_weathersit| features| cntLog| prediction|
+------+---+----+---+-------+-------+----------+----------+----+------+----+---------+---+------------+-------------+--------+---------+----------+---------------+--------+---------------+-------------+-------------+-------------+-------------+----------------+--------------+----------------+--------------+--------------------+------------------+------------------+
| 1| 0| 1| 0| 0| 6| 0| 1|0.24|0.2879|0.81| 0.0| 16| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 17.0|(23,[17],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,32,38,3...| 2.772588722239781| 2.913660643731534|
| 1| 0| 1| 1| 0| 6| 0| 1|0.22|0.2727| 0.8| 0.0| 40| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 19.0|(23,[19],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,34,38,3...|3.6888794541139363|2.2631049819480697|
| 1| 0| 1| 2| 0| 6| 0| 1|0.22|0.2727| 0.8| 0.0| 32| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 21.0|(23,[21],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,36,38,3...|3.4657359027997265|1.7247879610592074|
| 1| 0| 1| 3| 0| 6| 0| 1|0.24|0.2879|0.75| 0.0| 13| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 23.0| (23,[],[])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,38,39,4...|2.5649493574615367|1.1811623643601048|
| 1| 0| 1| 4| 0| 6| 0| 1|0.24|0.2879|0.75| 0.0| 1| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 22.0|(23,[22],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,37,38,3...| 0.0|0.8853765826763564|
| 1| 0| 1| 5| 0| 6| 0| 2|0.24|0.2576|0.75| 0.0896| 1| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 20.0|(23,[20],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|(53,[2,14,35,38,3...| 0.0|1.8878961318389735|
| 1| 0| 1| 6| 0| 6| 0| 1|0.22|0.2727| 0.8| 0.0| 2| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 18.0|(23,[18],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,33,38,3...|0.6931471805599453|3.1548063035590888|
| 1| 0| 1| 7| 0| 6| 0| 1| 0.2|0.2576|0.86| 0.0| 3| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 16.0|(23,[16],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,31,38,3...|1.0986122886681098| 4.095793485261978|
| 1| 0| 1| 8| 0| 6| 0| 1|0.24|0.2879|0.75| 0.0| 8| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 12.0|(23,[12],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,27,38,3...|2.0794415416798357| 4.812437536960807|
| 1| 0| 1| 9| 0| 6| 0| 1|0.32|0.3485|0.76| 0.0| 14| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 14.0|(23,[14],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,29,38,3...|2.6390573296152584| 4.598488071209021|
| 1| 0| 1| 10| 0| 6| 0| 1|0.38|0.3939|0.76| 0.2537| 36| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 15.0|(23,[15],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,30,38,3...| 3.58351893845611| 4.301385454879343|
| 1| 0| 1| 11| 0| 6| 0| 1|0.36|0.3333|0.81| 0.2836| 56| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 13.0|(23,[13],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,28,38,3...| 4.02535169073515| 4.327839111220991|
| 1| 0| 1| 12| 0| 6| 0| 1|0.42|0.4242|0.77| 0.2836| 84| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 5.0| (23,[5],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 0.0| (3,[0],[1.0])|(53,[2,14,20,38,3...| 4.430816798843313|4.6421161763137375|
| 1| 0| 1| 13| 0| 6| 0| 2|0.46|0.4545|0.72| 0.2985| 94| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 3.0| (23,[3],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|(53,[2,14,18,38,3...| 4.543294782270004| 4.635567308713835|
| 1| 0| 1| 14| 0| 6| 0| 2|0.46|0.4545|0.72| 0.2836|106| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 4.0| (23,[4],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|(53,[2,14,19,38,3...| 4.663439094112067| 4.55911658988811|
| 1| 0| 1| 15| 0| 6| 0| 2|0.44|0.4394|0.77| 0.2985|110| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 2.0| (23,[2],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|(53,[2,14,17,38,3...| 4.700480365792417| 4.56866575872639|
| 1| 0| 1| 16| 0| 6| 0| 2|0.42|0.4242|0.82| 0.2985| 93| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 0.0| (23,[0],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|(53,[2,14,15,38,3...| 4.532599493153256| 4.784356684609951|
| 1| 0| 1| 17| 0| 6| 0| 2|0.44|0.4394|0.82| 0.2836| 67| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 1.0| (23,[1],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 1.0| (3,[1],[1.0])|(53,[2,14,16,38,3...| 4.204692619390966| 5.208998681203642|
| 1| 0| 1| 18| 0| 6| 0| 3|0.42|0.4242|0.88| 0.2537| 35| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 11.0|(23,[11],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 2.0| (3,[2],[1.0])|(53,[2,14,26,38,3...|3.5553480614894135| 4.551558625668931|
| 1| 0| 1| 19| 0| 6| 0| 3|0.42|0.4242|0.88| 0.2537| 37| 2.0|(3,[2],[1.0])| 1.0|(1,[],[])| 10.0|(11,[10],[1.0])| 6.0| (23,[6],[1.0])| 0.0|(1,[0],[1.0])| 0.0|(6,[0],[1.0])| 1.0| (1,[],[])| 2.0| (3,[2],[1.0])|(53,[2,14,21,38,3...|3.6109179126442243| 4.266378279520251|
+------+---+----+---+-------+-------+----------+----------+----+------+----+---------+---+------------+-------------+--------+---------+----------+---------------+--------+---------------+-------------+-------------+-------------+-------------+----------------+--------------+----------------+--------------+--------------------+------------------+------------------+
"""
# meanCntLog=dfPCV.agg({'cntLog':'avg'}).collect()[0][0]
# print(meanCntLog)
# #4.536082104443427
# print('mse:%f'%(regressionEvaluator.evaluate(dfPCV, params={regressionEvaluator.metricName: 'mse'})))
# #mse:0.388605
# print('mse比例:%f'%(regressionEvaluator.evaluate(dfPCV,params={regressionEvaluator.metricName:'mse'})/meanCntLog**2))
# #mse比例:0.018886
# print('rmse:%f'%(regressionEvaluator.evaluate(dfPCV,params={regressionEvaluator.metricName:'rmse'})))
# #rmse:0.623382
# print('rmse比例:%f' % (regressionEvaluator.evaluate(dfPCV, params={regressionEvaluator.metricName: 'rmse'})/meanCntLog))
# #rmse比例:0.137427
# print('mae:%f' % (regressionEvaluator.evaluate(dfPCV, params={regressionEvaluator.metricName: 'mae'})))
# #mae:0.462729
# print('mae比例:%f' % (
# regressionEvaluator.evaluate(dfPCV, params={regressionEvaluator.metricName: 'mae'}) / meanCntLog))
# #mae比例:0.102011
# print('r2:%f' % (regressionEvaluator.evaluate(dfPCV, params={regressionEvaluator.metricName: 'r2'})))
# #r2:0.824039
# #从上结果可以看到,在2-203范围内只是优化迭代次数,最优迭代次数与默认值100相近。
# 在调制模型时,也可以把所有参数可能的范围输入,一次性得到最佳参数组合,这是ml默认的方式,运行时间较久。
# 似乎ml.tuning.CrossValidator,不会保存每一个参数对应的评估结果,而只是把最好的模型参数保存下来了
# 如果要绘制迭代次数与RMSE的关系图,可以采用for 循环在不同迭代次数下建模并评估。这里不再继续
其实每次只优化一个参数也是一种策略,当然后面还要配合其他操作,这里就不进一步探索了。
下面是原作者对于该数据集运行线性回归和决策树回归在不同参数下的实验结果,可供参考
对数变换前后cnt的分布图
绘制迭代次数与RMSE的关系图:
评估step对模型的影响(ml.regression.LinearRegression没有step这个参数):
不同正则化系数对模型的影响
先看L2正则化系数对模型的影响
再看L1正则化系数对模型的影响
截距对模型的影响
决策树
树的不同最大深度对性能影响:
最大划分数(每个节点分支时最大bin数)对模型的影响