如何使用 TensorFlow 和 Estimators 來檢查 Python 中的泰坦尼克號資料集?
可以透過迭代特徵並將特徵轉換為列表,並在控制檯上顯示它們,使用 TensorFlow 和 Estimators 檢查泰坦尼克號資料集。
閱讀更多: 什麼是 TensorFlow 以及 Keras 如何與 TensorFlow 協作建立神經網路?
我們將使用 Keras Sequential API,它有助於構建一個順序模型,該模型用於處理簡單的層堆疊,其中每一層只有一個輸入張量和一個輸出張量。
包含至少一層的神經網路稱為卷積層。我們可以使用卷積神經網路來構建學習模型。
我們正在使用 Google Colaboratory 來執行以下程式碼。Google Colab 或 Colaboratory 幫助在瀏覽器上執行 Python 程式碼,無需任何配置,並且可以免費訪問 GPU(圖形處理單元)。Colaboratory 建立在 Jupyter Notebook 的基礎之上。
Estimator 是 TensorFlow 對完整模型的高階表示。它旨在實現輕鬆擴充套件和非同步訓練。
我們將使用 tf.estimator API 訓練一個邏輯迴歸模型。該模型用作其他演算法的基線。我們使用泰坦尼克號資料集,目標是根據性別、年齡、等級等特徵預測乘客的生存情況。
Estimator 使用特徵列來描述模型如何解釋原始輸入特徵。Estimator 期望一個數值輸入向量,特徵列將有助於描述模型如何轉換資料集中每個特徵。選擇和使用正確的特徵列集對於學習有效的模型至關重要。
示例
print("The dataset is being inspected")
ds = make_input_fn(dftrain, y_train, batch_size=10)()
for feature_batch, label_batch in ds.take(1):
print('Some feature keys are:', list(feature_batch.keys()))
print()
print('A batch of class:', feature_batch['class'].numpy())
print()
print('A batch of Labels:', label_batch.numpy())程式碼來源 −https://www.tensorflow.org/tutorials/estimator/linear
輸出
The dataset is being inspected Some feature keys are: ['sex', 'age', 'n_siblings_spouses', 'parch', 'fare', 'class', 'deck', 'embark_town', 'alone'] A batch of class: [b'First' b'First' b'First' b'Third' b'Third' b'Third' b'First' b'Third' b'Second' b'Third'] A batch of Labels: [0 1 1 0 0 0 1 0 0 0]
解釋
- 資料集被檢查。
- 特徵鍵、標籤和類別在控制檯上顯示。
- 這是透過迭代資料集的一批來完成的。
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP