如何将 Tensorflow 与 Estimators 结合使用来探索 titanic 数据?

tensorflowserver side programmingprogramming更新于 2025/5/21 10:52:17

可以使用 Tensorflow 中的 estimator 通过使用‘head’方法、‘describe’方法和‘shape’方法来探索 titanic 数据集。head 方法给出数据集的前几行,而 describe 方法给出数据集的相关信息,例如列名、类型、平均值、方差、标准差等。 shape 方法给出了数据的维度。

阅读更多: 什么是 TensorFlow,以及 Keras 如何与 TensorFlow 配合使用来创建神经网络?

我们将使用 Keras Sequential API,它有助于构建用于处理普通层堆栈的顺序模型,其中每个层都有一个输入张量和一个输出张量。

包含至少一个层的神经网络称为卷积层。我们可以使用卷积神经网络构建学习模型。

TensorFlow Text 包含可与 TensorFlow 2.0 一起使用的文本相关类和操作的集合。TensorFlow Text 可用于预处理序列建模。

我们正在使用 Google Colaboratory 运行以下代码。 Google Colab 或 Colaboratory 可帮助在浏览器上运行 Python 代码,无需配置,并可免费访问 GPU(图形处理单元)。Colaboratory 是在 Jupyter Notebook 之上构建的。

Estimator 是 TensorFlow 对完整模型的高级表示。它旨在轻松扩展和异步训练。

我们将使用 tf.estimator API 训练逻辑回归模型。该模型用作其他算法的基线。我们使用泰坦尼克号数据集,目标是根据性别、年龄、等级等特征预测乘客的生存情况。

示例

print("Sample data is being displayed")
print(dftrain.head())
print("The metadata about data is being displayed")
print(dftrain.describe())
print("The dimensions of the data is being displayed")
print(dftrain.shape[0], dfeval.shape[0])

代码来源 −https://www.tensorflow.org/tutorials/estimator/linear

输出

Sample data is being displayed
   sex    age  n_siblings_spouses parch ... class deck embark_town alone
0  male   22.0  1 0   ... Third unknown Southampton      n
1  female 38.0  1 0   ... First C Cherbourg              n
2  female 26.0  0 0   ... Third unknown Southampton      y
3  female 35.0  1 0   ... First C Southampton            n
4  male   28.0  0 0   .. Third unknown Queenstown        y
[5 rows x 9 columns]
The metadata about data is being displayed
        age    n_siblings_spouses  parch       fare
count  627.000000  627.000000   627.000000   627.000000
mean   29.631308     0.545455     0.379585    34.385399
std    12.511818     1.151090     0.792999    54.597730
min     0.750000     0.000000     0.000000     0.000000
25%    23.000000     0.000000     0.000000     7.895800
50%    28.000000     0.000000     0.000000    15.045800
75%    35.000000     1.000000     0.000000    31.387500
max    80.000000     8.000000     5.000000   512.329200
The dimensions of the data is being displayed
627 264

解释

  • 泰坦尼克号数据集的样本数据显示在控制台上。

  • describe 方法用于提供有关数据集的元数据。

  • shape 方法提供数据集的维度。


相关文章