How to write such `y` into a `.csv` file (one column is `ID` and one column is our predict labeled `y`)?

Viewed 52

I have a label prediction of the testing data set y. It is an array of 0 and 1. I also transfer it into a 20000 by 1 array as a label.

y_pred = classifier.predict(X_test)
y_pred = (y_pred > 0.5)
y_pred=y_pred.astype(int)
y_pred1=np.reshape(y_pred, (20000,1))
print(y_pred1)
print(y_pred1.shape)
[[1 1 1 ... 0 0 0]]
(1, 20000)

I want to write such y into a .csv file (one column is ID and one column is our predict labeled y). I saw the following code:

result = pd.DataFrame({'Id':test['Id'].values, 'Default':y_pred.astype(np.int32)})
result.to_csv("1.csv", index=False)
result.head(10)

But there is an error report: ValueError: Data must be 1-dimensional

1 Answers

Try the following:

result = pd.DataFrame({'Id':test['Id'].values, 'Default':y_pred.astype(np.int32)[0]})

The values are probably inside a one element list.

Related