我建立了一个简单的预测模型(我必须预测“目标”,它可以是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)
您拥有的对象是一个生成器。它的目的是迭代地返回数据,您可以在此处了解更多信息:https://wiki.python.org/moin/Generators
要展开生成器,可以将其传递给list
功能。它使用生成器的每个“元素”,并将其逐个放入新列表。
因此,您可以做的是展开生成器,预览其内容并将其转储为CSV或类似格式。
您可以通过以下方式打印预测:
print(next(predictions))
OR
while(True):
try:
print(next(predictions))
except:
break
next()
方法在python中用于迭代生成器。