可视化-python

<title>readme_energy_viz</title>




<body> <div tabindex="-1" id="notebook" class="border-box-sizing"> <div class="container" id="notebook-container">

模型构建(能耗可视化)

In [1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline

In [2]:
from tqdm import tqdm_notebook as tqdm

In [3]:
from pathlib import Path

数据读入

In [4]:
data_dir = '/Users/douby/dataset/ytkc'

In [5]:
data_files = list((Path(data_dir) / 'feature_agg_half').glob('./*.csv'))
data_files

Out[5]:

[PosixPath('/Users/douby/dataset/ytkc/feature_agg_half/LZYTFTC25J1054033.csv'),
 PosixPath('/Users/douby/dataset/ytkc/feature_agg_half/LZYTATE64G1080906.csv'),
 PosixPath('/Users/douby/dataset/ytkc/feature_agg_half/LZYTFTC23J1054032.csv'),
 PosixPath('/Users/douby/dataset/ytkc/feature_agg_half/LZYTATE63H1023601.csv'),
 PosixPath('/Users/douby/dataset/ytkc/feature_agg_half/LZYTATE66G1080907.csv'),
 PosixPath('/Users/douby/dataset/ytkc/feature_agg_half/LZYTFTC21J1054031.csv')]
In [6]:
[print(pd.read_csv(df).shape) for df in data_files]

(248, 13)
(10, 13)
(269, 13)
(375, 13)
(16, 13)
(90, 13)

Out[6]:

[None, None, None, None, None, None]
In [7]:
# data_dfs = [pd.read_csv(data_file) for data_file in [data_files[0], data_files[1], data_files[2], data_files[3], data_files[4], data_files[5]]]
data_dfs = [pd.read_csv(data_file) for data_file in [data_files[0], data_files[1], data_files[2], data_files[4], data_files[5]]]
# data_dfs = [pd.read_csv(data_file) for data_file in [data_files[3]]]
data_dfs[0].head()

Out[7]:

distance avg_speed acs acs_idle idle_speed smooth_speed without_gear with_gear over_speed economical_speed sharp_turn acceleration deceleration
0 0.339039 -12.078710 0.259306 0.314533 0.165292 -0.239653 0.157602 -0.107825 0.0 0.000000 -0.000054 0.000166 -0.000122
1 0.614563 -2.027863 0.000000 0.000000 0.471228 -0.561469 0.026788 -0.104122 0.0 0.000000 0.001136 -0.000065 -0.000458
2 0.238495 -23.937845 0.000000 0.000000 -0.342079 -0.582975 0.000416 0.000339 0.0 0.127921 -0.000127 -0.000240 0.000206
3 -0.228120 2.818354 0.029315 0.071177 0.103766 -0.062362 -0.003305 0.000948 0.0 -0.003327 0.000078 0.000145 0.000126
4 -0.053401 32.830485 0.070601 0.048295 0.122727 -0.691631 0.114690 0.115410 0.0 -0.029081 -0.000064 0.000142 0.000004
In [8]:
# data_df = data_dfs[0].copy()
data_df = pd.concat(data_dfs).reset_index(drop=True)
data_df.head()

Out[8]:

distance avg_speed acs acs_idle idle_speed smooth_speed without_gear with_gear over_speed economical_speed sharp_turn acceleration deceleration
0 0.339039 -12.078710 0.259306 0.314533 0.165292 -0.239653 0.157602 -0.107825 0.0 0.000000 -0.000054 0.000166 -0.000122
1 0.614563 -2.027863 0.000000 0.000000 0.471228 -0.561469 0.026788 -0.104122 0.0 0.000000 0.001136 -0.000065 -0.000458
2 0.238495 -23.937845 0.000000 0.000000 -0.342079 -0.582975 0.000416 0.000339 0.0 0.127921 -0.000127 -0.000240 0.000206
3 -0.228120 2.818354 0.029315 0.071177 0.103766 -0.062362 -0.003305 0.000948 0.0 -0.003327 0.000078 0.000145 0.000126
4 -0.053401 32.830485 0.070601 0.048295 0.122727 -0.691631 0.114690 0.115410 0.0 -0.029081 -0.000064 0.000142 0.000004
In [9]:
data_df.describe()

Out[9]:

distance avg_speed acs acs_idle idle_speed smooth_speed without_gear with_gear over_speed economical_speed sharp_turn acceleration deceleration
count 633.000000 633.000000 633.000000 633.000000 633.000000 633.000000 633.000000 633.000000 633.000000 633.000000 633.000000 633.000000 633.000000
mean 0.229736 -6.440642 0.042807 -0.046857 -0.060265 -0.179363 -0.003372 0.006495 0.000290 0.012537 -0.000126 -0.000056 0.000020
std 0.489846 28.344863 0.196066 0.128160 0.158991 0.502956 0.063343 0.165652 0.003622 0.210383 0.000283 0.000377 0.000121
min -0.426985 -53.604094 -0.994052 -0.660708 -0.529819 -3.088889 -0.176723 -2.742593 -0.015642 -0.789326 -0.004440 -0.001327 -0.001001
25% -0.036451 -34.058430 -0.045746 -0.121145 -0.165821 -0.565963 -0.042504 -0.068378 0.000000 -0.123138 -0.000224 -0.000312 -0.000039
50% 0.148542 -3.626165 0.000000 -0.017720 -0.039279 -0.238453 0.000000 0.005794 0.000000 0.008953 -0.000072 -0.000044 0.000018
75% 0.396871 20.102755 0.105704 0.016907 0.042837 0.227981 0.034733 0.098355 0.000000 0.169764 0.000008 0.000192 0.000086
max 8.932278 44.573739 1.556257 0.387177 0.471228 0.834543 0.467464 0.334345 0.047686 0.621732 0.001136 0.001022 0.000394

特征预处理

In [10]:
from plotly.offline import init_notebook_mode, iplot
from plotly import graph_objs as go
import plotly.express as px
init_notebook_mode(connected=True)

In [11]:
col_min_maxs = []

y(每升油可行驶距离)的分布

In [12]:
iplot(px.histogram(data_df, x='distance', histnorm='probability'))



In [13]:
col_min_maxs.append([-0.4, 1.2])

平均时速与85km/h之差的分布

In [14]:
iplot(px.histogram(data_df, x='avg_speed', histnorm='probability'))



In [15]:
col_min_maxs.append([-70, 60])

空调开启

In [16]:
iplot(px.histogram(data_df, x='acs', histnorm='probability'))



In [17]:
col_min_maxs.append([-0.2, 0.3])

怠速(开启空调)

In [18]:
iplot(px.histogram(data_df, x='acs_idle', histnorm='probability'))



In [19]:
col_min_maxs.append([-0.3, 0.3])

怠速(未开启空调)

In [20]:
iplot(px.histogram(data_df, x='idle_speed', histnorm='probability'))



In [21]:
col_min_maxs.append([-0.7, 0.4])

平稳驾驶

In [22]:
iplot(px.histogram(data_df, x='smooth_speed', histnorm='probability'))



In [23]:
col_min_maxs.append([-1, 1])

空档滑行

In [24]:
iplot(px.histogram(data_df, x='without_gear', histnorm='probability'))



In [25]:
col_min_maxs.append([-0.06, 0.06])

带档滑行

In [26]:
iplot(px.histogram(data_df, x='with_gear', histnorm='probability'))



In [27]:
col_min_maxs.append([-0.02, 0.05])

In [28]:
data_df.describe()

Out[28]:

distance avg_speed acs acs_idle idle_speed smooth_speed without_gear with_gear over_speed economical_speed sharp_turn acceleration deceleration
count 633.000000 633.000000 633.000000 633.000000 633.000000 633.000000 633.000000 633.000000 633.000000 633.000000 633.000000 633.000000 633.000000
mean 0.229736 -6.440642 0.042807 -0.046857 -0.060265 -0.179363 -0.003372 0.006495 0.000290 0.012537 -0.000126 -0.000056 0.000020
std 0.489846 28.344863 0.196066 0.128160 0.158991 0.502956 0.063343 0.165652 0.003622 0.210383 0.000283 0.000377 0.000121
min -0.426985 -53.604094 -0.994052 -0.660708 -0.529819 -3.088889 -0.176723 -2.742593 -0.015642 -0.789326 -0.004440 -0.001327 -0.001001
25% -0.036451 -34.058430 -0.045746 -0.121145 -0.165821 -0.565963 -0.042504 -0.068378 0.000000 -0.123138 -0.000224 -0.000312 -0.000039
50% 0.148542 -3.626165 0.000000 -0.017720 -0.039279 -0.238453 0.000000 0.005794 0.000000 0.008953 -0.000072 -0.000044 0.000018
75% 0.396871 20.102755 0.105704 0.016907 0.042837 0.227981 0.034733 0.098355 0.000000 0.169764 0.000008 0.000192 0.000086
max 8.932278 44.573739 1.556257 0.387177 0.471228 0.834543 0.467464 0.334345 0.047686 0.621732 0.001136 0.001022 0.000394

超速

In [29]:
iplot(px.histogram(data_df[data_df['over_speed'].abs() < 0.0005], x='over_speed', histnorm='probability'))



In [30]:
col_min_maxs.append(None)

经济速度

In [31]:
iplot(px.histogram(data_df, x='economical_speed', histnorm='probability'))



In [32]:
col_min_maxs.append([-0.35,  0.4])

急转弯

In [33]:
iplot(px.histogram(data_df, x='sharp_turn', histnorm='probability'))



In [34]:
col_min_maxs.append([-0.0006,  0.00025])

急加速

In [35]:
iplot(px.histogram(data_df, x='acceleration', histnorm='probability'))



急减速

In [36]:
iplot(px.histogram(data_df, x='deceleration', histnorm='probability'))



In [37]:
col_min_maxs.append(None)

In [38]:
print(col_min_maxs)

[[-0.4, 1.2], [-70, 60], [-0.2, 0.3], [-0.3, 0.3], [-0.7, 0.4], [-1, 1], [-0.06, 0.06], [-0.02, 0.05], None, [-0.35, 0.4], [-0.0006, 0.00025], None]

相关系数

In [39]:
from scipy.stats import pearsonr
for idx, col in enumerate(data_df.columns[1:]):
    print('{:>20s}: {} {}'.format(col, idx + 1, pearsonr(data_df.loc[:, 'distance'], data_df.loc[:, col])))

           avg_speed: 1 (-0.4806056935158062, 6.771838194917686e-38)
                 acs: 2 (0.025909353896178537, 0.5152485744583909)
            acs_idle: 3 (-0.3700028295037353, 5.705255667186981e-22)
          idle_speed: 4 (-0.3228488553373969, 8.032180151372731e-17)
        smooth_speed: 5 (0.2785124836395245, 9.67637030815978e-13)
        without_gear: 6 (-0.2567446649900016, 5.489270993302441e-11)
           with_gear: 7 (-0.2168626382253659, 3.566268286835245e-08)
          over_speed: 8 (0.046565596512352606, 0.24204694520483655)
    economical_speed: 9 (0.3582354174754291, 1.32959542617395e-20)
          sharp_turn: 10 (-0.49814240667459775, 5.278312542744376e-41)
        acceleration: 11 (-0.49021488077200454, 1.4122058291001258e-39)
        deceleration: 12 (-0.07419675772517359, 0.06209243092334431)
In [40]:
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import StandardScaler

In [41]:
data_x = data_df.copy()
data_x.pop('over_speed')
data_x.pop('deceleration')
data_x.head()

Out[41]:

distance avg_speed acs acs_idle idle_speed smooth_speed without_gear with_gear economical_speed sharp_turn acceleration
0 0.339039 -12.078710 0.259306 0.314533 0.165292 -0.239653 0.157602 -0.107825 0.000000 -0.000054 0.000166
1 0.614563 -2.027863 0.000000 0.000000 0.471228 -0.561469 0.026788 -0.104122 0.000000 0.001136 -0.000065
2 0.238495 -23.937845 0.000000 0.000000 -0.342079 -0.582975 0.000416 0.000339 0.127921 -0.000127 -0.000240
3 -0.228120 2.818354 0.029315 0.071177 0.103766 -0.062362 -0.003305 0.000948 -0.003327 0.000078 0.000145
4 -0.053401 32.830485 0.070601 0.048295 0.122727 -0.691631 0.114690 0.115410 -0.029081 -0.000064 0.000142
In [42]:
data_x.shape

Out[42]:

(633, 11)
In [43]:
data_x.describe()

Out[43]:

distance avg_speed acs acs_idle idle_speed smooth_speed without_gear with_gear economical_speed sharp_turn acceleration
count 633.000000 633.000000 633.000000 633.000000 633.000000 633.000000 633.000000 633.000000 633.000000 633.000000 633.000000
mean 0.229736 -6.440642 0.042807 -0.046857 -0.060265 -0.179363 -0.003372 0.006495 0.012537 -0.000126 -0.000056
std 0.489846 28.344863 0.196066 0.128160 0.158991 0.502956 0.063343 0.165652 0.210383 0.000283 0.000377
min -0.426985 -53.604094 -0.994052 -0.660708 -0.529819 -3.088889 -0.176723 -2.742593 -0.789326 -0.004440 -0.001327
25% -0.036451 -34.058430 -0.045746 -0.121145 -0.165821 -0.565963 -0.042504 -0.068378 -0.123138 -0.000224 -0.000312
50% 0.148542 -3.626165 0.000000 -0.017720 -0.039279 -0.238453 0.000000 0.005794 0.008953 -0.000072 -0.000044
75% 0.396871 20.102755 0.105704 0.016907 0.042837 0.227981 0.034733 0.098355 0.169764 0.000008 0.000192
max 8.932278 44.573739 1.556257 0.387177 0.471228 0.834543 0.467464 0.334345 0.621732 0.001136 0.001022
In [44]:
min_maxs2 = {
    'avg_speed': [data_x['avg_speed'].quantile(0.01, interpolation='lower'), data_x['avg_speed'].quantile(0.99, interpolation='lower')],
    'idle_speed': [data_x['idle_speed'].quantile(0.01, interpolation='lower'), data_x['idle_speed'].quantile(0.99, interpolation='lower')],
    'smooth_speed': [data_x['smooth_speed'].quantile(0.01, interpolation='lower'), data_x['smooth_speed'].quantile(0.99, interpolation='lower')],
    'without_gear': [data_x['without_gear'].quantile(0.01, interpolation='lower'), data_x['without_gear'].quantile(0.99, interpolation='lower')],
    'with_gear': [data_x['with_gear'].quantile(0.01, interpolation='lower'), data_x['with_gear'].quantile(0.99, interpolation='lower')],
}

In [45]:
min_maxs = {}
for col in data_x.columns:
    if col == 'distance':
        continue
    min_maxs[col] = [data_x[col].quantile(0.01, interpolation='lower'), data_x[col].quantile(0.99, interpolation='lower')]

In [46]:
for key, value in min_maxs.items():
    data_x = data_x[(data_x[key] >= value[0]) & (data_x[key] <= value[1])].reset_index(drop=True)
data_x.head()

Out[46]:

distance avg_speed acs acs_idle idle_speed smooth_speed without_gear with_gear economical_speed sharp_turn acceleration
0 0.238495 -23.937845 0.000000 0.000000 -0.342079 -0.582975 0.000416 0.000339 0.127921 -0.000127 -0.000240
1 -0.228120 2.818354 0.029315 0.071177 0.103766 -0.062362 -0.003305 0.000948 -0.003327 0.000078 0.000145
2 -0.053401 32.830485 0.070601 0.048295 0.122727 -0.691631 0.114690 0.115410 -0.029081 -0.000064 0.000142
3 0.138776 -29.274609 0.000000 0.000000 -0.018761 0.354382 -0.066567 -0.103120 0.114558 -0.000033 -0.000140
4 -0.068668 27.737001 0.000000 0.000000 -0.056566 -0.507694 0.047777 0.081056 -0.358102 -0.000117 0.000358
In [47]:
min_maxs = min_maxs2

In [48]:
min_maxs

Out[48]:

{'avg_speed': [-51.621932734195525, 41.412472523975026],
 'idle_speed': [-0.4410424367192252, 0.28424100553722353],
 'smooth_speed': [-1.4261626899586215, 0.6907720409622349],
 'without_gear': [-0.1444684040642318, 0.1389533952208047],
 'with_gear': [-0.20398162286404914, 0.23038489260487244]}
In [49]:
data_x.shape

Out[49]:

(528, 11)

数据切割

In [50]:
data_x = data_x[data_x['distance'].abs() < 0.5].reset_index(drop=True)

In [51]:
data_x.shape

Out[51]:

(453, 11)
In [52]:
iplot(px.histogram(data_x, x='acs_idle', histnorm='probability'))



In [54]:
min_maxs

Out[54]:

{'avg_speed': [-51.621932734195525, 41.412472523975026],
 'idle_speed': [-0.4410424367192252, 0.28424100553722353],
 'smooth_speed': [-1.4261626899586215, 0.6907720409622349],
 'without_gear': [-0.1444684040642318, 0.1389533952208047],
 'with_gear': [-0.20398162286404914, 0.23038489260487244]}
In [56]:
stds = [col for col in data_x.columns if col not in list(min_maxs) + ['distance']]
stds

Out[56]:

['acs', 'acs_idle', 'economical_speed', 'sharp_turn', 'acceleration']
In [57]:
data_x.describe()

Out[57]:

distance avg_speed acs acs_idle idle_speed smooth_speed without_gear with_gear economical_speed sharp_turn acceleration
count 453.000000 453.000000 453.000000 453.000000 453.000000 453.000000 453.000000 453.000000 453.000000 453.000000 453.000000
mean 0.104706 -0.850899 0.039484 -0.030939 -0.046611 -0.255106 0.001487 0.035037 -0.020283 -0.000079 -0.000006
std 0.203774 26.753502 0.155878 0.105407 0.144603 0.404190 0.054409 0.094853 0.190077 0.000152 0.000334
min -0.426985 -51.520323 -0.335255 -0.330399 -0.436964 -1.406471 -0.138302 -0.203982 -0.514268 -0.000695 -0.000940
25% -0.060484 -26.207044 -0.044206 -0.095439 -0.145260 -0.574704 -0.033859 -0.036968 -0.152447 -0.000182 -0.000219
50% 0.096973 3.548778 0.001166 -0.005849 -0.019430 -0.350080 0.001730 0.042694 -0.033392 -0.000047 0.000000
75% 0.269420 22.896486 0.103145 0.025409 0.053965 0.048863 0.038242 0.115736 0.129223 0.000030 0.000216
max 0.498554 41.412473 0.676811 0.286205 0.284241 0.679493 0.138953 0.230385 0.428343 0.000245 0.000767
In [58]:
minmax_scaler = MinMaxScaler()
std_scaler = StandardScaler()

In [59]:
data_x['acs'].std() ** 2

Out[59]:

0.024297971088833834
In [60]:
x1 = data_x[min_maxs.keys()].values
x2 = data_x[stds].values

In [61]:
x1 = minmax_scaler.fit_transform(x1)
x2 = std_scaler.fit_transform(x2)

In [62]:
minmax_scaler.data_min_

Out[62]:

array([-51.52032262,  -0.43696419,  -1.4064708 ,  -0.13830228,
        -0.20398162])
In [63]:
minmax_scaler.data_max_

Out[63]:

array([41.41247252,  0.28424101,  0.67949319,  0.1389534 ,  0.23038489])
In [64]:
std_scaler.var_

Out[64]:

array([2.42443332e-02, 1.10860194e-02, 3.60496195e-02, 2.29959783e-08,
       1.11296355e-07])
In [65]:
std_scaler.mean_

Out[65]:

array([ 3.94835887e-02, -3.09393541e-02, -2.02834151e-02, -7.88421458e-05,
       -6.35169372e-06])
In [66]:
# std_scaler.var_

In [67]:
# print(x1.shape, x2.shape)

In [68]:
x = np.concatenate([x1, x2], axis=1)
# x = x2

In [69]:
# x = (data_x - data_x.mean()).iloc[:, 1:].values

In [70]:
after_df = pd.DataFrame(x, columns=list(min_maxs.keys()) + stds)
print(after_df.shape, data_x.shape)
after_df['distance'] = data_x['distance']
for col in stds:
    after_df = after_df[after_df[col].abs() <= 3].reset_index(drop=True)
after_df.describe()

(453, 10) (453, 11)

Out[70]:

avg_speed idle_speed smooth_speed without_gear with_gear acs acs_idle economical_speed sharp_turn acceleration distance
count 440.000000 440.000000 440.000000 440.000000 440.000000 440.000000 440.000000 440.000000 440.000000 440.000000 440.000000
mean 0.544840 0.540288 0.551291 0.505293 0.550379 -0.072003 0.014984 -0.003437 0.016661 -0.002541 0.102004
std 0.287738 0.200288 0.193503 0.195712 0.219310 0.876400 0.972739 0.999704 0.961396 0.996219 0.202384
min 0.000000 0.000000 0.000000 0.000000 0.000000 -2.406705 -2.844141 -2.601735 -2.753512 -2.797615 -0.426985
25% 0.275532 0.402921 0.399683 0.378846 0.374730 -0.547633 -0.577304 -0.714329 -0.656314 -0.636992 -0.058288
50% 0.590539 0.574636 0.504421 0.506732 0.567631 -0.253578 0.243660 -0.069439 0.206128 0.019039 0.096232
75% 0.799941 0.680800 0.698807 0.636874 0.737920 0.345690 0.537584 0.782284 0.718773 0.656972 0.253238
max 1.000000 1.000000 1.000000 1.000000 1.000000 2.891058 2.827787 2.362839 2.138621 2.319161 0.498554
In [71]:
after_df.shape

Out[71]:

(440, 11)
In [72]:
for col in stds:
    after_df[col] = after_df[col] / 6 + 0.5

In [73]:
from sklearn.linear_model import LogisticRegression
from sklearn import linear_model

In [74]:
iplot(px.histogram(after_df, x='distance', histnorm='probability'))



In [75]:
X = after_df.iloc[:, :-1]
y = after_df.loc[:, 'distance'] - after_df.loc[:, 'distance'].mean()
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=53)

In [76]:
reg = linear_model.LinearRegression()

In [77]:
reg.fit(X_train, y_train)

Out[77]:

LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, normalize=False)
In [78]:
after_df.head()

Out[78]:

avg_speed idle_speed smooth_speed without_gear with_gear acs acs_idle economical_speed sharp_turn acceleration distance
0 0.296800 0.131564 0.394779 0.500326 0.470388 0.457737 0.548975 0.630095 0.446732 0.383276 0.238495
1 0.584709 0.749760 0.644359 0.486905 0.471790 0.489116 0.661643 0.514885 0.672521 0.575834 -0.228120
2 0.907654 0.776050 0.342690 0.912486 0.735304 0.533308 0.625423 0.492278 0.516407 0.574239 -0.053401
3 0.239374 0.579867 0.844144 0.258733 0.232203 0.457737 0.548975 0.618365 0.550169 0.433052 0.138776
4 0.852846 0.527447 0.430869 0.671148 0.656214 0.457737 0.548975 0.203461 0.457666 0.681878 -0.068668
In [79]:
reg.coef_

Out[79]:

array([-0.28016056, -0.13911922, -0.1505462 , -0.02798608,  0.00107875,
        0.06146868, -0.3391931 , -0.21884858, -0.20782035, -0.32308545])
In [80]:
from scipy.stats import pearsonr
for idx, col in enumerate(data_df.columns[1:]):
    print('{:>20s}: {} {}'.format(col, idx + 1, pearsonr(data_df.loc[:, 'distance'], data_df.loc[:, col])))

           avg_speed: 1 (-0.4806056935158062, 6.771838194917686e-38)
                 acs: 2 (0.025909353896178537, 0.5152485744583909)
            acs_idle: 3 (-0.3700028295037353, 5.705255667186981e-22)
          idle_speed: 4 (-0.3228488553373969, 8.032180151372731e-17)
        smooth_speed: 5 (0.2785124836395245, 9.67637030815978e-13)
        without_gear: 6 (-0.2567446649900016, 5.489270993302441e-11)
           with_gear: 7 (-0.2168626382253659, 3.566268286835245e-08)
          over_speed: 8 (0.046565596512352606, 0.24204694520483655)
    economical_speed: 9 (0.3582354174754291, 1.32959542617395e-20)
          sharp_turn: 10 (-0.49814240667459775, 5.278312542744376e-41)
        acceleration: 11 (-0.49021488077200454, 1.4122058291001258e-39)
        deceleration: 12 (-0.07419675772517359, 0.06209243092334431)
In [81]:
from sklearn.metrics import mean_squared_error, r2_score, mean_absolute_error

In [82]:
mean_absolute_error(y_test, reg.predict(X_test))

Out[82]:

0.09102681542495218
In [83]:
r2_score(y_test, reg.predict(X_test))

Out[83]:

0.6527850673214266
In [84]:
after_df.head()

Out[84]:

avg_speed idle_speed smooth_speed without_gear with_gear acs acs_idle economical_speed sharp_turn acceleration distance
0 0.296800 0.131564 0.394779 0.500326 0.470388 0.457737 0.548975 0.630095 0.446732 0.383276 0.238495
1 0.584709 0.749760 0.644359 0.486905 0.471790 0.489116 0.661643 0.514885 0.672521 0.575834 -0.228120
2 0.907654 0.776050 0.342690 0.912486 0.735304 0.533308 0.625423 0.492278 0.516407 0.574239 -0.053401
3 0.239374 0.579867 0.844144 0.258733 0.232203 0.457737 0.548975 0.618365 0.550169 0.433052 0.138776
4 0.852846 0.527447 0.430869 0.671148 0.656214 0.457737 0.548975 0.203461 0.457666 0.681878 -0.068668
In [85]:
X = after_df.iloc[:, [0, 1, 2, 3, 5, 6, 7, 8, 9]]
y = after_df.loc[:, 'distance']
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=53)

In [86]:
X.columns

Out[86]:

Index(['avg_speed', 'idle_speed', 'smooth_speed', 'without_gear', 'acs',
       'acs_idle', 'economical_speed', 'sharp_turn', 'acceleration'],
      dtype='object')
In [87]:
reg.fit(X_train, y_train)
reg.coef_

Out[87]:

array([-0.27961133, -0.13924411, -0.15054304, -0.02811706,  0.06150276,
       -0.33922744, -0.2191993 , -0.20771092, -0.32300866])
In [88]:
mean_absolute_error(y_test, reg.predict(X_test))

Out[88]:

0.09103503726488799
In [89]:
r2_score(y_test, reg.predict(X_test))

Out[89]:

0.6526890643994555
</div> </div> </div> </body>
posted @ 2020-02-21 15:58  亦神  阅读(189)  评论(0)    收藏  举报