如何使用 TensorFlow 和 Estimator 透過提升樹顯示資料的樣本?


可以使用 TensorFlow 中的提升樹,透過 'head' 方法、'describe' 方法和 'shape' 方法來顯示泰坦尼克號資料集的樣本。'head' 方法顯示資料集的前幾行,'describe' 方法顯示有關資料集的資訊,例如列名、型別、均值、方差、標準差等等。'shape' 方法顯示資料的維度。

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

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

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

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

我們將瞭解如何使用決策樹和 tf.estimator API 訓練梯度提升模型。

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

提升樹模型被認為是迴歸和分類中最流行和有效的機器學習方法。這是一種整合技術,它結合了來自許多(10 個、100 個或 1000 個)樹模型的預測。

示例

print("Some sample of the data")
print(dftrain.head())
print("Metadata about the dataset")
print(dftrain.describe())
print("Dimensions of the data")
print(dftrain.shape[0], dfeval.shape[0])

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

輸出

Some sample of the data
   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]
Metadata about the dataset
        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
Dimensions of the data
627 264

解釋

  • 資料集包含訓練集和評估集。
  • dftrain 和 y_train 是訓練集。
  • 模型使用它來學習特徵和模式。
  • 使用評估集 dfeval 和 y_eval 測試模型。
  • 獲得並顯示某些資料的彙總統計資訊。

更新於: 2021年2月25日

73 次瀏覽

啟動你的職業生涯

透過完成課程獲得認證

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