Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do 'numpy.ndarray' object do not 'numpy.ndarray' object?

When you call DataFrame.to_numpy(), pandas will find the NumPy dtype that can hold all of the dtypes in the DataFrame. But how to perform the reverse operation?

I have an 'numpy.ndarray' object 'pred'. It looks like this:

[[0.00599913 0.00506044 0.00508315 ... 0.00540191 0.00542058 0.00542058]]

I am trying to do like this:

 pred = np.uint8(pred)
 print("Model predict:\n", pred.T)

But I get:

[[0 0 0 ... 0 0 0]]

Why, after the conversion, I do not get something like this:

0 0 0 0 0 0 ... 0 0 0 0 0 0

And how to write the pred to a file?

pred.to_csv('pred.csv', header=None, index=False)
pred = pd.read_csv('pred.csv', sep=',', header=None)

Gives an error message:

AttributeError                            Traceback (most recent call last)
<ipython-input-68-b223b39b5db1> in <module>()
----> 1 pred.to_csv('pred.csv', header=None, index=False)
      2 pred = pd.read_csv('pred.csv', sep=',', header=None)
AttributeError: 'numpy.ndarray' object has no attribute 'to_csv'

Please help me figure this out.

like image 230
Snowface Avatar asked Sep 18 '25 07:09

Snowface


1 Answers

You can solve the issue with one line of code to convert ndarray to pandas df and then to csv file.

pd.DataFrame(X_train_res).to_csv("x_train_smote_oversample.csv")
like image 121
Elvin Aghammadzada Avatar answered Sep 20 '25 23:09

Elvin Aghammadzada