简体   繁体   中英

Appending 2-D numpy array to txt file

My program prints a variable predictions when executed, which is a 2-D numpy array. I am attempting to write the output to a text file. I ensured the shape is 2D by using predictions.shape and checked the type to be <class numpy.ndarray> by using type(predictions)

The error I am receiving is:

Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    predictor()
  File "/Users/owner/Desktop/algo/predict.py", line 146, in predictor
    predictions.savetxt("predictions.txt", "a", delimiter=',', fmt='%d',  header='', footer='')
AttributeError: 'numpy.ndarray' object has no attribute 'savetxt'

This is returned after I call the function in the python module.

Im sure Ive imported numpy correctly, am I using the savetxt() function incorrect?

I tried searching on Stack but couldn't find anything, Thank you.

Yes, the savetext method is not a method on the array, but in the numpy package. You can use it by calling the method and passing in the array:

numpy.savetxt("predictions.txt", predictions, delimiter=',', fmt='%d',  header='', footer='')

or np.savetext(..) if you did import numpy as np

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM