当前位置:网站首页>【无标题】
【无标题】
2022-08-10 04:22:00 【kyccom】
使用回归 (regression)进行预测
1.导入模块
!pip install seaborn
import pathlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
tf.__version__
2.下载数据集
dataset_path = keras.utils.get_file('auto-img.data', 'http://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data')
dataset_path
运行后输出:
‘C:\Users\Administrator\.keras\datasets\auto-img.data’
查看一下数据集中的内容:
column_names = ['MPG','Cylinders','Displacement','Horsepowner','Weight','Acceleration', 'Model Year','Origin']
raw_dataset = pd.read_csv(dataset_path, names=column_names,
na_values='?', comment='\t',
sep=' ',skipinitialspace=True)
dataset = raw_dataset.copy()
dataset.tail()
3.数据清洗
查看一下有没有空值:
dataset.isna().sum()
输出结果:
MPG 0
Cylinders 0
Displacement 0
Horsepowner 6
Weight 0
Acceleration 0
Model Year 0
Origin 0
dtype: int64
不管有没有空值 ,去除一下:
dataset = dataset.dropna()
用pop 命令, 把origin列做拿出来,后续作为为独热编码:
origin = dataset.pop('Origin')
给表增加列:
dataset['USA'] = (origin == 1)*1.0
dataset['Europe'] = (origin == 2)*1.0
dataset['Japan'] = (origin == 3)*1.0
dataset.tail()
在这里插入图片描述
将数据集拆分为验证集与训练集
train_dataset = dataset.sample(frac=0.8, random_state=0)
test_dataset = dataset.drop(train_dataset.index)
用seaborn 查看一下情况
sns.pairplot(train_dataset[['MPG', 'Cylinders', 'Displacement', 'Weight']], diag_kind='kde')
看一下透视统计:
train_stats = train_dataset.describe()
train_stats.pop('MPG')
train_stats = train_stats.transpose()
train_stats
把标签分离出来
train_labels = train_dataset.pop('MPG')
test_labels = test_dataset.pop('MPG')
数据归一化(每个值减去均值后再除以标准差)
def norm(x):
return (x - train_stats['mean'])/train_stats['std']
normed_train_data = norm(train_dataset)
normed_test_data = norm(test_dataset)
4.构建模型
def build_model():
model = keras.Sequential([
layers.Dense(64, activation='relu', input_shape=[len(train_dataset.keys())]),
layers.Dense(64, activation='relu'),
layers.Dense(1)
])
optimizer = tf.keras.optimizers.RMSprop(0.001)
model.compile(loss='mse',
optimizer=optimizer,
metrics=['mae', 'mse'])
return model
model = build_model()
model.summary()
Model: “sequential_2”
Layer (type) Output Shape Param #
dense_3 (Dense) (None, 64) 640
dense_4 (Dense) (None, 64) 4160
dense_5 (Dense) (None, 1) 65
=================================================================
Total params: 4,865
Trainable params: 4,865
Non-trainable params: 0
测试一下预测:
example_batch = normed_train_data[:10]
example_result = model.predict(example_batch)
example_result
1/1 [==============================] - 0s 311ms/step
array([[ 0.1861527 ],
[-0.03714132],
[ 0.41865367],
[ 0.08125568],
[ 0.35935453],
[ 0.14658093],
[ 0.42246234],
[ 0.07202747],
[ 0.08832908],
[ 0.41105857]], dtype=float32)
5. 开始训练
class PrintDot(keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs):
if epoch%100 ==0:print('')
print('.', end='')
EPOCHS = 10000
history = model.fit(
normed_train_data, train_labels,
epochs=EPOCHS, validation_split=0.2, verbose=0,
callbacks=[PrintDot()])
来看一下模型中的参数
hist= pd.DataFrame(history.history)
hist['epoch'] = history.epoch
hist.tail()
6.图形化显示
def plot_history(history):
hist = pd.DataFrame(history.history)
hist['epoch'] = history.epoch
plt.figure()
plt.xlabel('Epoch')
plt.ylabel('Mean Abs Error [MGP]')
plt.plot(hist['epoch'], hist['mae'], label='Val Error')
plt.plot(hist['epoch'], hist['val_mae'], label='Val Error')
plt.ylim([0,5])
plt.legend()
plt.figure()
plt.xlabel('Epoch')
plt.ylabel('Mean Square Error [$MGP^2$]')
plt.plot(hist['epoch'], hist['mse'], label='Train Error')
plt.plot(hist['epoch'], hist['val_mse'], label='Val Error')
plt.ylim([0,20])
plt.legend()
plt.show()
运行一下:
plot_history(history)
再优化一下
model = build_model()
early_stop = keras.callbacks.EarlyStopping(monitor='val_loss', patience=10)
history = model.fit(normed_train_data, train_labels, epochs=EPOCHS,
validation_split=0.2, verbose=0, callbacks= [early_stop, PrintDot()])
plot_history(history)
loss, mae, mse = model.evaluate(normed_test_data, test_labels, verbose=2)
print("Testing set Mean Abs Error: {:5.2f} MPG".format(mae))
3/3 - 0s - loss: 6.5743 - mae: 1.8934 - mse: 6.5743 - 37ms/epoch - 12ms/step
Testing set Mean Abs Error: 1.89 MPG
用测数据预测一下MPG
test_predictions = model.predict(normed_test_data).flatten()
plt.scatter(test_labels, test_predictions)
plt.xlabel('True Values [MPG]')
plt.ylabel('Predictions [MPG]')
plt.axis('equal')
plt.axis('square')
plt.xlim([0,plt.xlim()[1]])
plt.ylim([0,plt.ylim()[1]])
_ = plt.plot([-100, 100], [-100, 100])
这看起来我们的模型预测得相当好。我们来看下误差分布。
error = test_predictions - test_labels
plt.hist(error, bins = 25)
plt.xlabel("Prediction Error [MPG]")
_ = plt.ylabel("Count")
边栏推荐
猜你喜欢
随机推荐
22牛客多校3 A.Ancestor(LCA + 枚举)
解决“#231-D declaration is not visible outside of function”告警方法
JS gets the year, month, day, time, etc. of the simple current time
虚假新闻检测论文阅读(七):A temporal ensembling based semi-supervised ConvNet for the detection of fake news
干货 | 查资料利器:线上图书馆
【OpenCV图像处理4】算术与位运算
RK3568处理器体验小记
音乐现场的未来将被NFT门票主宰?
GP如何进行数据比对?
结构体的内存对齐问题
自定义训练,使用Generator dataset迭代数据报错
@Autowired注解 --required a single bean, but 2 were found出现的原因以及解决方法
webrtc学习--websocket服务器(二) (web端播放h264)
华为交换机配置日志推送
如何成为一名合格的 DBA?看看“老油条”们怎么说
JVM内存模型
最牛最全的 Postman 实现 API 自动化测试教程
打开VsCode经常弹出:尝试在目标目录创建文件时发生一个错误:拒绝访问:重试 跳过这个文件(不推荐),关闭安装程序
MindSpore官方RandomChoiceWithMask算子用例报错
安芯电子IPO过会:年营收4亿 汪良恩兄弟持股61.6%