[英]How print predictions with tensorflow
我構建了一個簡單的預測 model (我必須預測可以是 0 或 1 的“目標”),我希望以如下格式打印並保存到文件數據:
id_of_prediction, prediction
0, 1
1, 0
2, 1
3, 1
...
你知道怎么做嗎? 現在打印的值為:
<generator object Estimator.predict at 0x7f718d5621a8>
我的代碼:
def dl_model(deep_df): # deep_df is a dataframe
deep_feat = deep_df.drop(columns=["target"], axis=1)
deep_label = deep_df["target"]
categorical_columns = [col for col in deep_feat.columns if len(deep_feat[col].unique())==2 or deep_feat[col].dtype=='O']
continuous_columns = [col for col in deep_feat.columns if len(deep_feat[col].unique()) > 2 and (
deep_feat[col].dtype == 'int64' or deep_feat[col].dtype == 'float64')]
X_T, X_t, y_T, y_t = train_test_split(deep_feat, deep_label, test_size=0.3)
cols_to_scale = continuous_columns[:]
cols_to_scale.remove("age")
scaler = StandardScaler()
X_T.loc[:, cols_to_scale] = scaler.fit_transform(X_T.loc[:, cols_to_scale])
X_t.loc[:, cols_to_scale] = scaler.fit_transform(X_t.loc[:, cols_to_scale])
categorical_object_feat_cols = [tf.feature_column.embedding_column(
tf.feature_column.categorical_column_with_hash_bucket(key=col, hash_bucket_size=1000),
dimension=len(deep_df[col].unique())) for col in categorical_columns if deep_df[col].dtype == 'O']
categorical_integer_feat_cols = [
tf.feature_column.embedding_column(tf.feature_column.categorical_column_with_identity(key=col, num_buckets=2),
dimension=len(deep_df[col].unique())) for col in categorical_columns if deep_df[col].dtype == 'int64']
continuous_feat_cols = [tf.feature_column.numeric_column(key=col) for col in continuous_columns if col != "age"]
age_bucket = tf.feature_column.bucketized_column(tf.feature_column.numeric_column(key="age"),
boundaries=[20, 30, 40, 50, 60, 70, 80, 90])
feat_cols = categorical_object_feat_cols + \
categorical_integer_feat_cols + \
continuous_feat_cols + \
[age_bucket]
input_fun = tf.compat.v1.estimator.inputs.pandas_input_fn(X_T, y_T, batch_size=50, num_epochs=1000, shuffle=True)
pred_input_fun = tf.compat.v1.estimator.inputs.pandas_input_fn(X_t, batch_size=50, shuffle=False)
DNN_model = tf.estimator.DNNClassifier(hidden_units=[10, 10, 10], feature_columns=feat_cols, n_classes=2)
DNN_model.train(input_fn=input_fun, steps=5000)
predictions = DNN_model.predict(pred_input_fun)
res_pred = list(predictions)
y_pred = []
for i in range(len(res_pred)):
y_pred.append(res_pred[i]["class_ids"][0])
rep = classification_report(y_t, y_pred)
print(rep)
new_data = pd.read_csv("test.csv")
predictions = DNN_model.predict(new_data) # my predictions
print(predictions)
您擁有的 object 是一台發電機。 它的目的是迭代返回數據,您可以在此處閱讀更多信息: https://wiki.python.org/moin/Generators
要展開生成器,您可以將其傳遞給list
function。 它獲取生成器的每個“元素”,並將其一個一個地放入新列表中。
所以你可以做的是展開生成器,預覽它的內容並轉儲到 CSV 或類似的格式。
您可以通過以下方式打印預測,
print(next(predictions))
或者
while(True):
try:
print(next(predictions))
except:
break
在 python 中使用next()
方法來迭代生成器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.