How to save and load numpy.array() data properly?

后端 未结 4 2072
别跟我提以往
别跟我提以往 2020-11-27 11:01

I wonder, how to save and load numpy.array data properly. Currently I\'m using the numpy.savetxt() method. For example, if I got an array mar

4条回答
  •  春和景丽
    2020-11-27 12:03

    np.fromfile() has a sep= keyword argument:

    Separator between items if file is a text file. Empty (“”) separator means the file should be treated as binary. Spaces (” ”) in the separator match zero or more whitespace characters. A separator consisting only of spaces must match at least one whitespace.

    The default value of sep="" means that np.fromfile() tries to read it as a binary file rather than a space-separated text file, so you get nonsense values back. If you use np.fromfile('markers.txt', sep=" ") you will get the result you are looking for.

    However, as others have pointed out, np.loadtxt() is the preferred way to convert text files to numpy arrays, and unless the file needs to be human-readable it is usually better to use binary formats instead (e.g. np.load()/np.save()).

提交回复
热议问题