导包
import functools
import numpy as np
import tensorflow as tf
import tensorflow_datasets as tfds
import os
导入数据
cwd = os.getcwd()
train_path = cwd + os.sep + 'dataset/train.csv'
test_path = cwd + os.sep + 'dataset/eval.csv'
train_path
'D:\\codes\\jupyterlab\\test\\dataset/train.csv'
# 让 numpy 数据更易读。
np.set_printoptions(precision=3, suppress=True)
读取csv文件构建dataset
CSV_COLUMNS = ['survived', 'sex', 'age',
'n_siblings_spouses', 'parch', 'fare',
'class', 'deck', 'embark_town', 'alone']
LABEL_COLUMN = CSV_COLUMNS[0]
def get_dataset(file_path):
dataset = tf.data.experimental.make_csv_dataset(
file_path,
batch_size=6, # 为了示例更容易展示,手动设置较小的值
label_name=LABEL_COLUMN,
na_value="?",
num_epochs=1,
ignore_errors=True)
return dataset
raw_train_data = get_dataset(train_path)
raw_test_data = get_dataset(test_path)
获取标签和features张量
examples, labels = next(iter(raw_train_data)) # 第一个批次
print("EXAMPLES: \n", examples, "\n")
print("LABELS: \n", labels)
EXAMPLES:
OrderedDict([('sex', <tf.Tensor: shape=(6,), dtype=string, numpy=
array([b'male', b'female', b'female', b'female', b'male', b'female'],
dtype=object)>), ('age', <tf.Tensor: shape=(6,), dtype=float32, numpy=array([34., 18., 16., 52., 22., 28.], dtype=float32)>), ('n_siblings_spouses', <tf.Tensor: shape=(6,), dtype=int32, numpy=array([1, 0, 0, 1, 0, 1])>), ('parch', <tf.Tensor: shape=(6,), dtype=int32, numpy=array([0, 1, 0, 0, 0, 0])>), ('fare', <tf.Tensor: shape=(6,), dtype=float32, numpy=array([26. , 9.35 , 7.733, 78.267, 7.25 , 16.1 ], dtype=float32)>), ('class', <tf.Tensor: shape=(6,), dtype=string, numpy=
array([b'Second', b'Third', b'Third', b'First', b'Third', b'Third'],
dtype=object)>), ('deck', <tf.Tensor: shape=(6,), dtype=string, numpy=
array([b'unknown', b'unknown', b'unknown', b'D', b'unknown', b'unknown'],
dtype=object)>), ('embark_town', <tf.Tensor: shape=(6,), dtype=string, numpy=
array([b'Southampton', b'Southampton', b'Queenstown', b'Cherbourg',
b'Southampton', b'Southampton'], dtype=object)>), ('alone', <tf.Tensor: shape=(6,), dtype=string, numpy=array([b'n', b'n', b'y', b'n', b'y', b'n'], dtype=object)>)])
LABELS:
tf.Tensor([0 1 1 1 0 1], shape=(6,), dtype=int32)
字典转二维张量
类别型数据处理
CATEGORIES = {
'sex': ['male', 'female'],
'class' : ['First', 'Second', 'Third'],
'deck' : ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'],
'embark_town' : ['Cherbourg', 'Southhampton', 'Queenstown'],
'alone' : ['y', 'n']
}
categorical_columns = []
for feature, vocab in CATEGORIES.items():
cat_col = tf.feature_column.categorical_column_with_vocabulary_list(
key=feature, vocabulary_list=vocab)
categorical_columns.append(tf.feature_column.indicator_column(cat_col))
categorical_columns
[IndicatorColumn(categorical_column=VocabularyListCategoricalColumn(key='sex', vocabulary_list=('male', 'female'), dtype=tf.string, default_value=-1, num_oov_buckets=0)),
IndicatorColumn(categorical_column=VocabularyListCategoricalColumn(key='class', vocabulary_list=('First', 'Second', 'Third'), dtype=tf.string, default_value=-1, num_oov_buckets=0)),
IndicatorColumn(categorical_column=VocabularyListCategoricalColumn(key='deck', vocabulary_list=('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'), dtype=tf.string, default_value=-1, num_oov_buckets=0)),
IndicatorColumn(categorical_column=VocabularyListCategoricalColumn(key='embark_town', vocabulary_list=('Cherbourg', 'Southhampton', 'Queenstown'), dtype=tf.string, default_value=-1, num_oov_buckets=0)),
IndicatorColumn(categorical_column=VocabularyListCategoricalColumn(key='alone', vocabulary_list=('y', 'n'), dtype=tf.string, default_value=-1, num_oov_buckets=0))]
连续型数据处理
def process_continuous_data(mean, data):
# 标准化数据
data = tf.cast(data, tf.float32) * 1/(2*mean)
return tf.reshape(data, [-1, 1])
MEANS = {
'age' : 29.631308,
'n_siblings_spouses' : 0.545455,
'parch' : 0.379585,
'fare' : 34.385399
}
numerical_columns = []
for feature in MEANS.keys():
num_col = tf.feature_column.numeric_column(feature,
normalizer_fn=functools
.partial(process_continuous_data, MEANS[feature]))
numerical_columns.append(num_col)
numerical_columns
[NumericColumn(key='age', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=functools.partial(<function process_continuous_data at 0x0000028CAAA00160>, 29.631308)),
NumericColumn(key='n_siblings_spouses', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=functools.partial(<function process_continuous_data at 0x0000028CAAA00160>, 0.545455)),
NumericColumn(key='parch', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=functools.partial(<function process_continuous_data at 0x0000028CAAA00160>, 0.379585)),
NumericColumn(key='fare', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=functools.partial(<function process_continuous_data at 0x0000028CAAA00160>, 34.385399))]
创建预处理层
preprocessing_layer = tf.keras.layers.DenseFeatures(categorical_columns+numerical_columns)
categorical_columns+numerical_columns
[IndicatorColumn(categorical_column=VocabularyListCategoricalColumn(key='sex', vocabulary_list=('male', 'female'), dtype=tf.string, default_value=-1, num_oov_buckets=0)),
IndicatorColumn(categorical_column=VocabularyListCategoricalColumn(key='class', vocabulary_list=('First', 'Second', 'Third'), dtype=tf.string, default_value=-1, num_oov_buckets=0)),
IndicatorColumn(categorical_column=VocabularyListCategoricalColumn(key='deck', vocabulary_list=('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'), dtype=tf.string, default_value=-1, num_oov_buckets=0)),
IndicatorColumn(categorical_column=VocabularyListCategoricalColumn(key='embark_town', vocabulary_list=('Cherbourg', 'Southhampton', 'Queenstown'), dtype=tf.string, default_value=-1, num_oov_buckets=0)),
IndicatorColumn(categorical_column=VocabularyListCategoricalColumn(key='alone', vocabulary_list=('y', 'n'), dtype=tf.string, default_value=-1, num_oov_buckets=0)),
NumericColumn(key='age', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=functools.partial(<function process_continuous_data at 0x0000028CAAA00160>, 29.631308)),
NumericColumn(key='n_siblings_spouses', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=functools.partial(<function process_continuous_data at 0x0000028CAAA00160>, 0.545455)),
NumericColumn(key='parch', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=functools.partial(<function process_continuous_data at 0x0000028CAAA00160>, 0.379585)),
NumericColumn(key='fare', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=functools.partial(<function process_continuous_data at 0x0000028CAAA00160>, 34.385399))]
创建模型
model = tf.keras.Sequential([
preprocessing_layer,
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid'),
])
model.compile(
loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
模型训练
train_data = raw_train_data.shuffle(500)
test_data = raw_test_data
list(train_data.take(1).as_numpy_iterator())
[(OrderedDict([('sex',
array([b'female', b'female', b'male', b'female', b'male', b'male'],
dtype=object)),
('age', array([26., 60., 23., 28., 21., 30.], dtype=float32)),
('n_siblings_spouses', array([0, 1, 0, 0, 0, 0])),
('parch', array([0, 0, 0, 0, 0, 0])),
('fare',
array([ 7.925, 75.25 , 15.046, 8.05 , 7.733, 10.5 ], dtype=float32)),
('class',
array([b'Third', b'First', b'Second', b'Third', b'Third', b'Second'],
dtype=object)),
('deck',
array([b'unknown', b'D', b'unknown', b'unknown', b'unknown', b'unknown'],
dtype=object)),
('embark_town',
array([b'Southampton', b'Cherbourg', b'Cherbourg', b'Southampton',
b'Queenstown', b'Southampton'], dtype=object)),
('alone',
array([b'y', b'n', b'y', b'y', b'y', b'y'], dtype=object))]),
array([1, 1, 0, 0, 0, 0])),
model.fit(train_data, epochs=20)
Epoch 1/20
WARNING:tensorflow:Layers in a Sequential model should only have a single input tensor. Received: inputs=OrderedDict([('sex', <tf.Tensor 'IteratorGetNext:8' shape=(None,) dtype=string>), ('age', <tf.Tensor 'IteratorGetNext:0' shape=(None,) dtype=float32>), ('n_siblings_spouses', <tf.Tensor 'IteratorGetNext:6' shape=(None,) dtype=int32>), ('parch', <tf.Tensor 'IteratorGetNext:7' shape=(None,) dtype=int32>), ('fare', <tf.Tensor 'IteratorGetNext:5' shape=(None,) dtype=float32>), ('class', <tf.Tensor 'IteratorGetNext:2' shape=(None,) dtype=string>), ('deck', <tf.Tensor 'IteratorGetNext:3' shape=(None,) dtype=string>), ('embark_town', <tf.Tensor 'IteratorGetNext:4' shape=(None,) dtype=string>), ('alone', <tf.Tensor 'IteratorGetNext:1' shape=(None,) dtype=string>)]). Consider rewriting this model with the Functional API.
WARNING:tensorflow:Layers in a Sequential model should only have a single input tensor. Received: inputs=OrderedDict([('sex', <tf.Tensor 'IteratorGetNext:8' shape=(None,) dtype=string>), ('age', <tf.Tensor 'IteratorGetNext:0' shape=(None,) dtype=float32>), ('n_siblings_spouses', <tf.Tensor 'IteratorGetNext:6' shape=(None,) dtype=int32>), ('parch', <tf.Tensor 'IteratorGetNext:7' shape=(None,) dtype=int32>), ('fare', <tf.Tensor 'IteratorGetNext:5' shape=(None,) dtype=float32>), ('class', <tf.Tensor 'IteratorGetNext:2' shape=(None,) dtype=string>), ('deck', <tf.Tensor 'IteratorGetNext:3' shape=(None,) dtype=string>), ('embark_town', <tf.Tensor 'IteratorGetNext:4' shape=(None,) dtype=string>), ('alone', <tf.Tensor 'IteratorGetNext:1' shape=(None,) dtype=string>)]). Consider rewriting this model with the Functional API.
105/105 [==============================] - 1s 2ms/step - loss: 0.5012 - accuracy: 0.7783
Epoch 2/20
105/105 [==============================] - 0s 806us/step - loss: 0.4281 - accuracy: 0.8214
Epoch 3/20
105/105 [==============================] - 0s 801us/step - loss: 0.4229 - accuracy: 0.8070
Epoch 4/20
105/105 [==============================] - 0s 798us/step - loss: 0.4061 - accuracy: 0.8118
Epoch 5/20
105/105 [==============================] - 0s 795us/step - loss: 0.3954 - accuracy: 0.8246
Epoch 6/20
105/105 [==============================] - 0s 789us/step - loss: 0.3923 - accuracy: 0.8262
Epoch 7/20
105/105 [==============================] - 0s 797us/step - loss: 0.3910 - accuracy: 0.8246
Epoch 8/20
105/105 [==============================] - 0s 785us/step - loss: 0.3794 - accuracy: 0.8341
Epoch 9/20
105/105 [==============================] - 0s 786us/step - loss: 0.3752 - accuracy: 0.8437
Epoch 10/20
105/105 [==============================] - 0s 815us/step - loss: 0.3742 - accuracy: 0.8405
Epoch 11/20
105/105 [==============================] - 0s 815us/step - loss: 0.3691 - accuracy: 0.8341
Epoch 12/20
105/105 [==============================] - 0s 824us/step - loss: 0.3640 - accuracy: 0.8453
Epoch 13/20
105/105 [==============================] - 0s 819us/step - loss: 0.3567 - accuracy: 0.8389
Epoch 14/20
105/105 [==============================] - 0s 1ms/step - loss: 0.3531 - accuracy: 0.8485
Epoch 15/20
105/105 [==============================] - 0s 789us/step - loss: 0.3480 - accuracy: 0.8533
Epoch 16/20
105/105 [==============================] - 0s 796us/step - loss: 0.3478 - accuracy: 0.8437
Epoch 17/20
105/105 [==============================] - 0s 796us/step - loss: 0.3490 - accuracy: 0.8517
Epoch 18/20
105/105 [==============================] - 0s 786us/step - loss: 0.3443 - accuracy: 0.8469
Epoch 19/20
105/105 [==============================] - 0s 796us/step - loss: 0.3393 - accuracy: 0.8453
Epoch 20/20
105/105 [==============================] - 0s 805us/step - loss: 0.3374 - accuracy: 0.8437
<keras.callbacks.History at 0x28cabc9b520>
模型验证
test_loss, test_accuracy = model.evaluate(test_data)
print('\n\nTest Loss {}, Test Accuracy {}'.format(test_loss, test_accuracy))
WARNING:tensorflow:Layers in a Sequential model should only have a single input tensor. Received: inputs=OrderedDict([('sex', <tf.Tensor 'IteratorGetNext:8' shape=(None,) dtype=string>), ('age', <tf.Tensor 'IteratorGetNext:0' shape=(None,) dtype=float32>), ('n_siblings_spouses', <tf.Tensor 'IteratorGetNext:6' shape=(None,) dtype=int32>), ('parch', <tf.Tensor 'IteratorGetNext:7' shape=(None,) dtype=int32>), ('fare', <tf.Tensor 'IteratorGetNext:5' shape=(None,) dtype=float32>), ('class', <tf.Tensor 'IteratorGetNext:2' shape=(None,) dtype=string>), ('deck', <tf.Tensor 'IteratorGetNext:3' shape=(None,) dtype=string>), ('embark_town', <tf.Tensor 'IteratorGetNext:4' shape=(None,) dtype=string>), ('alone', <tf.Tensor 'IteratorGetNext:1' shape=(None,) dtype=string>)]). Consider rewriting this model with the Functional API.
44/44 [==============================] - 0s 897us/step - loss: 0.4517 - accuracy: 0.8220
Test Loss 0.45173394680023193, Test Accuracy 0.8219696879386902
模型预测
predictions = model.predict(test_data)
# 显示部分结果
for prediction, survived in zip(predictions[:10], list(test_data)[0][1][:10]):
print("Predicted survival: {:.2%}".format(prediction[0]),
" | Actual outcome: ",
("SURVIVED" if bool(survived) else "DIED"))
WARNING:tensorflow:Layers in a Sequential model should only have a single input tensor. Received: inputs=OrderedDict([('sex', <tf.Tensor 'IteratorGetNext:8' shape=(None,) dtype=string>), ('age', <tf.Tensor 'IteratorGetNext:0' shape=(None,) dtype=float32>), ('n_siblings_spouses', <tf.Tensor 'IteratorGetNext:6' shape=(None,) dtype=int32>), ('parch', <tf.Tensor 'IteratorGetNext:7' shape=(None,) dtype=int32>), ('fare', <tf.Tensor 'IteratorGetNext:5' shape=(None,) dtype=float32>), ('class', <tf.Tensor 'IteratorGetNext:2' shape=(None,) dtype=string>), ('deck', <tf.Tensor 'IteratorGetNext:3' shape=(None,) dtype=string>), ('embark_town', <tf.Tensor 'IteratorGetNext:4' shape=(None,) dtype=string>), ('alone', <tf.Tensor 'IteratorGetNext:1' shape=(None,) dtype=string>)]). Consider rewriting this model with the Functional API.
Predicted survival: 57.07% | Actual outcome: SURVIVED
Predicted survival: 83.36% | Actual outcome: DIED
Predicted survival: 8.28% | Actual outcome: DIED
Predicted survival: 8.79% | Actual outcome: SURVIVED
Predicted survival: 57.22% | Actual outcome: DIED
Predicted survival: 8.16% | Actual outcome: SURVIVED