如何使用TensorFlow和Estimators探索泰坦尼克號資料集?


可以使用TensorFlow的estimator,透過`head`方法、`describe`方法和`shape`方法來探索泰坦尼克號資料集。`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`方法給出資料集的維度。

更新於:2021年2月22日

83 次瀏覽

啟動你的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.