使用 Estimators 進行訓練前的準備
在這個練習中,我們會回到第 2 章使用過的 King County 房屋交易資料集。我們將再次開發並訓練一個機器學習模型來預測房價;但這次會改用 estimator API。
我們不會一次做完所有步驟,而是把流程拆成幾個部分。先定義特徵欄位並載入資料。下一個練習會定義並訓練一個現成的 estimator。請注意,feature_column 已從 tensorflow 為你匯入。此外,numpy 也已以 np 名稱匯入,且 Kings County 房屋資料集已作為 pandas 的 DataFrame:housing 提供。
本練習屬於課程
Python 的 TensorFlow 入門
練習說明
- 完成
bedrooms的特徵欄位,並為bathrooms新增另一個數值型特徵欄位。請使用bedrooms與bathrooms作為鍵。 - 依照定義的順序建立特徵欄位清單
feature_list。 - 將
labels設為housing中的price欄位。 - 完成
features字典中bedrooms的條目,並新增bathrooms的條目。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Define feature columns for bedrooms and bathrooms
bedrooms = feature_column.numeric_column("____")
bathrooms = ____
# Define the list of feature columns
feature_list = [____, ____]
def input_fn():
# Define the labels
labels = np.array(____)
# Define the features
features = {'bedrooms':np.array(housing['____']),
'bathrooms':____}
return features, labels