user11979210
user11979210

Reputation:

How print predictions with tensorflow

I build a simple predictive model (I have to predict 'target' which can be 0 or 1) and I wish to print and save to file data in format like:

id_of_prediction, prediction
0, 1
1, 0
2, 1
3, 1
... 

Do you know how to do it? Right now the printed value is:

<generator object Estimator.predict at 0x7f718d5621a8>

My code:

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)

Upvotes: 0

Views: 512

Answers (2)

Swapnil Masurekar
Swapnil Masurekar

Reputation: 488

You can print predictions by,

print(next(predictions))

OR

while(True):
    try:
        print(next(predictions))
    except:
        break

next() method is used in python in order to iterate a generator.

Upvotes: 0

Yuras
Yuras

Reputation: 482

The object you have is a generator. It's purpose is to iteratively return data, you can read more about this here: https://wiki.python.org/moin/Generators

To unroll a generator you can pass it to the list function. It takes every "element" of the generator and puts it to the new list, one by one.

So what you could do is to unroll the generator, preview its content and dump to CSV or similar format.

Upvotes: 1

Related Questions