<jupyter_start><jupyter_text>基础回归模型：预测汽车燃油效率Auto MPG<jupyter_code>import sys
import os
import pathlib

import matplotlib as mpl
import matplotlib.pyplot as plt
import tensorflow as tf 
from tensorflow import keras
import pandas as pd
import numpy as np
import seaborn as sns


%matplotlib inline

print("python version: ", sys.version_info)
for module in np, mpl, tf, keras, pd, sns:
    print(module.__name__, "version: ", module.__version__)
    
seed = 10383<jupyter_output>python version:  sys.version_info(major=3, minor=7, micro=2, releaselevel='final', serial=0)
numpy version:  1.16.0
matplotlib version:  3.0.3
tensorflow version:  2.3.0
tensorflow.keras version:  2.4.0
pandas version:  0.24.2
seaborn version:  0.11.0<jupyter_text>1. Auto MPG 数据集  1.1 获取数据<jupyter_code>dataset_path = keras.utils.get_file("auto-mpg.data", "http://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data")
dataset_path
column_names = [
    "MPG",
    "Cylinders",
    "Displacement",
    "Horsepower",
    "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()<jupyter_output><empty_output><jupyter_text>4.2 数据清洗 数据集中包括一些未知值。<jupyter_code>dataset.isna().sum()<jupyter_output><empty_output><jupyter_text>为了保证这个初始示例的简单性，删除这些行。<jupyter_code>dataset = dataset.dropna()<jupyter_output><empty_output><jupyter_text>"Origin" 列实际上代表分类，而不仅仅是一个数字。所以把它转换为独热码 （one-hot）:<jupyter_code>origin = dataset.pop("Origin")
dataset["USA"] = (origin == 1) * 1.0
dataset["Europe"] = (origin == 2) * 1.0
dataset["Japan"] = (origin == 3) * 1.0  
dataset.tail()<jupyter_output><empty_output><jupyter_text>1.3 拆分数据集<jupyter_code>train_dataset = dataset.sample(frac=0.8, random_state=seed)
test_dataset = dataset.drop(train_dataset.index)<jupyter_output><empty_output><jupyter_text>1.4 数据检查 快速查看训练集中几对列的联合分布。<jupyter_code>sns.pairplot(train_dataset[["MPG", "Cylinders", "Displacement", "Weight"]], diag_kind="kde")<jupyter_output><empty_output><jupyter_text>也可以查看总体的数据统计:<jupyter_code>train_stats = train_dataset.describe()
train_stats.pop("MPG")
train_stats = train_stats.transpose()
train_stats<jupyter_output><empty_output><jupyter_text>1.5 从数据集中分离标签<jupyter_code>train_labels = train_dataset.pop("MPG")
test_labels = test_dataset.pop("MPG")<jupyter_output><empty_output><jupyter_text>1.6 数据归一化<jupyter_code>def norm(x):
    return (x - train_stats["mean"]) / train_stats["std"]

normed_train_data = norm(train_dataset)
normed_test_data = norm(test_dataset)<jupyter_output><empty_output><jupyter_text>2. 模型  2.1 构建模型<jupyter_code>def build_model():
    model = keras.Sequential([
        keras.layers.Dense(64, activation="relu", input_shape=[len(train_dataset.keys())]),
        keras.layers.Dense(64, activation="relu"),
        keras.layers.Dense(1)
    ])
    optimizer = keras.optimizers.RMSprop(0.001)
    model.compile(loss="mse", optimizer=optimizer, metrics=["mae", "mse"])
    return model
model = build_model()<jupyter_output><empty_output><jupyter_text>2.2 检查模型<jupyter_code>model.summary()
example_batch = normed_train_data[:10]
example_result = model.predict(example_batch)
example_result<jupyter_output><empty_output><jupyter_text>2.3 训练模型 对模型进行1000个周期的训练，并在 history 对象中记录训练和验证的准确性。<jupyter_code>class PrintDot(keras.callbacks.Callback):
    def on_epoch_end(self, epoch, logs):
        if epoch % 100 == 0:
            print("")
        print(".", end="")


EPOCHS = 1000

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()
def plot_history(history):
    hist = pd.DataFrame(history.history)
    hist["epoch"] = history.epoch

    plt.figure()
    plt.xlabel("Epoch")
    plt.ylabel("Mean Abs Error [MPG]")
    plt.plot(hist["epoch"], hist["mae"], label="Train 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 [$MPG^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)<jupyter_output><empty_output><jupyter_text>2.4 early stopping<jupyter_code>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))<jupyter_output>3/3 - 0s - loss: 9.5104 - mae: 2.3102 - mse: 9.5104
Testing set Mean Abs Error:  2.31 MPG<jupyter_text>2.5 预测<jupyter_code>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])<jupyter_output><empty_output><jupyter_text>查看误差分布<jupyter_code>error = test_predictions - test_labels
plt.hist(error, bins=25)
plt.xlabel("Prediction Error [MPG]")
_ = plt.ylabel("Count")<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup