如何使用 Estimators 在 TensorFlow 中進行特徵工程?


可以使用 Estimators 在 TensorFlow 中進行特徵工程,方法是首先定義列,然後迭代分類列。獲取特徵的唯一名稱,並將其附加到空列表中。

閱讀更多: 什麼是 TensorFlow?Keras 如何與 TensorFlow 協同工作來建立神經網路?

我們將使用 Keras Sequential API,它有助於構建一個順序模型,該模型用於處理簡單的層堆疊,其中每一層只有一個輸入張量和一個輸出張量。

包含至少一層卷積層的神經網路稱為卷積神經網路。我們可以使用卷積神經網路來構建學習模型。

我們使用 Google Colaboratory 來執行以下程式碼。Google Colab 或 Colaboratory 幫助在瀏覽器上執行 Python 程式碼,無需任何配置,並且可以免費訪問 GPU(圖形處理單元)。Colaboratory 基於 Jupyter Notebook 構建。

Estimator 是 TensorFlow 中對完整模型的高階表示。它旨在易於擴充套件和非同步訓練。Estimator 使用特徵列來描述模型如何解釋原始輸入特徵。Estimator 期望一個數值輸入向量,特徵列將有助於描述模型應該如何轉換資料集中的每個特徵。

選擇和使用正確的特徵列集對於學習有效的模型至關重要。特徵列可以是原始特徵字典中的原始輸入之一,也可以是使用針對一個或多個基本列定義的轉換建立的新列。

線性估計器同時使用數值和分類特徵。特徵列適用於所有 TensorFlow 估計器。他們的目標是定義用於建模的特徵。他們還具有特徵工程功能,例如獨熱編碼、歸一化和分箱。

示例

print("Feature engineering")
CATEGORICAL_COLUMNS = ['sex', 'n_siblings_spouses', 'parch', 'class', 'deck', 'embark_town', 'alone']
NUMERIC_COLUMNS = ['age', 'fare']
feature_columns = []
print("Iterating through categorical columns")
for feature_name in CATEGORICAL_COLUMNS:
vocabulary = dftrain[feature_name].unique()
feature_columns.append(tf.feature_column.categorical_column_with_vocabulary_list(feature_name, vocabulary))
print("Iterating through numeric columns")
for feature_name in NUMERIC_COLUMNS:
feature_columns.append(tf.feature_column.numeric_column(feature_name, dtype=tf.float32))

程式碼來源 −https://www.tensorflow.org/tutorials/estimator/linear

輸出

Feature engineering
Iterating through categorical columns
Iterating through numeric columns

解釋

  • 此處執行特徵工程。
  • 迭代列,並將它們新增到列表中。

更新於:2021年2月22日

瀏覽量:138

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.