可视化-python
<title>readme_energy_viz</title>
</div>
</div>
</div>
</body>
模型构建(能耗可视化)¶
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]:
In [6]:
[print(pd.read_csv(df).shape) for df in data_files]
Out[6]:
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]:
In [8]:
# data_df = data_dfs[0].copy()
data_df = pd.concat(data_dfs).reset_index(drop=True)
data_df.head()
Out[8]:
In [9]:
data_df.describe()
Out[9]:
特征预处理¶
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]:
超速¶
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)
相关系数¶
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])))
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]:
In [42]:
data_x.shape
Out[42]:
In [43]:
data_x.describe()
Out[43]:
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]:
In [47]:
min_maxs = min_maxs2
In [48]:
min_maxs
Out[48]:
In [49]:
data_x.shape
Out[49]:
数据切割¶
In [50]:
data_x = data_x[data_x['distance'].abs() < 0.5].reset_index(drop=True)
In [51]:
data_x.shape
Out[51]:
In [52]:
iplot(px.histogram(data_x, x='acs_idle', histnorm='probability'))
In [54]:
min_maxs
Out[54]:
In [56]:
stds = [col for col in data_x.columns if col not in list(min_maxs) + ['distance']]
stds
Out[56]:
In [57]:
data_x.describe()
Out[57]:
In [58]:
minmax_scaler = MinMaxScaler()
std_scaler = StandardScaler()
In [59]:
data_x['acs'].std() ** 2
Out[59]:
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]:
In [63]:
minmax_scaler.data_max_
Out[63]:
In [64]:
std_scaler.var_
Out[64]:
In [65]:
std_scaler.mean_
Out[65]:
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()
Out[70]:
In [71]:
after_df.shape
Out[71]:
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]:
In [78]:
after_df.head()
Out[78]:
In [79]:
reg.coef_
Out[79]:
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])))
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]:
In [83]:
r2_score(y_test, reg.predict(X_test))
Out[83]:
In [84]:
after_df.head()
Out[84]:
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]:
In [87]:
reg.fit(X_train, y_train)
reg.coef_
Out[87]:
In [88]:
mean_absolute_error(y_test, reg.predict(X_test))
Out[88]:
In [89]:
r2_score(y_test, reg.predict(X_test))
Out[89]:

浙公网安备 33010602011771号