如何使用 Tensorflow 和 Estimators 来预测 titanic 数据集的输出?
tensorflowserver side programmingprogramming更新于 2025/5/20 2:07:17
可以使用 Tensorflow 和 Estimators 来预测 titanic 数据集的输出,方法是使用之前创建的估算器"BoostedTreesClassifier"并调用"predict"方法。
阅读更多: 什么是 TensorFlow,以及 Keras 如何与 TensorFlow 配合使用来创建神经网络?
我们将使用 Keras Sequential API,它有助于构建用于处理普通层堆栈的顺序模型,其中每个层都有一个输入张量和一个输出张量。
包含至少一个层的神经网络称为卷积层。我们可以使用卷积神经网络构建学习模型。
我们使用 Google Colaboratory 运行以下代码。Google Colab 或 Colaboratory 有助于在浏览器上运行 Python 代码,无需配置,可以免费访问 GPU(图形处理单元)。Colaboratory 建立在 Jupyter Notebook 之上。
Estimator 是 TensorFlow 对完整模型的高级表示。它旨在轻松扩展和异步训练。 我们将使用 tf.estimator API 训练逻辑回归模型。该模型用作其他算法的基线。我们使用泰坦尼克号数据集,目的是根据性别、年龄、舱位等特征预测乘客生存情况。
示例
print("The predictions are being made")
pred_dicts = list(est.predict(eval_input_fn))
probs = pd.Series([pred['probabilities'][1] for pred in pred_dicts])
probs.plot(kind='hist', bins=20, title='predicted probabilities')
plt.show()
输出
The predictions are being made INFO:tensorflow:Calling model_fn. INFO:tensorflow:Done calling model_fn. INFO:tensorflow:Graph was finalized. INFO:tensorflow:Restoring parameters from /tmp/tmpyls8bhku/model.ckpt-100 INFO:tensorflow:Running local_init_op. INFO:tensorflow:Done running local_init_op.

解释
- 模型训练完成后,即可进行预测。
- 对乘客进行此操作,以查看他们是否能幸存。
- 乘客属于评估数据集。
- TensorFlow 模型经过优化,可对批量或一次性收集的示例进行预测。
- 预测概率在控制台上可视化。
相关文章
打印
下一节:如何使用 Tensorflow 和 Estimator 查找 titanic 数据集上的 ROC 曲线? ❯❮ 上一节:如何将 Tensorflow 和 Estimator 与 Boosted 树一起使用来训练和评估模型?

