YouTip LogoYouTip

Numpy Io

NumPy IO \n\nNumPy can read and write text data or binary data on disk.\n\nNumPy introduces a simple file format for ndarray objects: npy.\n\nnpy files are used to store data, shapes, dtype and other information needed to reconstruct ndarray.\n\nCommon IO functions are:\n\n* load() and save() functions are the two main functions for reading and writing file array data. By default, arrays are saved in uncompressed raw binary format in files with the extension .npy.\n* savez() function is used to write multiple arrays to a file. By default, arrays are saved in uncompressed raw binary format in files with the extension .npz.\n* loadtxt() and savetxt() functions handle normal text files (.txt, etc.)\n\n### numpy.save()\n\nThe numpy.save() function saves an array to a file with the .npy extension.\n\nnumpy.save(file, arr, allow_pickle=True, fix_imports=True)\n**Parameter Description:**\n\n* **file**: File to save, with .npy extension. If the file path does not end with .npy, the extension will be added automatically.\n* **arr**: Array to save\n* **allow_pickle**: Optional, boolean, allows using Python pickles to save object arrays. Pickle in Python is used to serialize and deserialize objects before saving to disk files or reading from disk files.\n* **fix_imports**: Optional, forconvenient for Python2 inreading Python3 savedofdata。\n\n## Example\n\nimport numpy as np a = np.array([1,2,3,4,5])np.save('outfile.npy',a)np.save('outfile2',a)\n\nWe can view the file content:\n\n$ cat outfile.npy ?NUMPYv{'descr': '<i8', 'fortran_order': False, 'shape': (5,), } $ cat outfile2.npy ?NUMPYv{'descr': '<i8', 'fortran_order': False, 'shape': (5,), } \nIt can be seen that the file is garbled because they are data in Numpy's proprietary binary format.\n\nWe can use the load() function to read the data and it will display normally:\n\n## Example\n\nimport numpy as np b = np.load('outfile.npy')print(b)\n\nThe output is:\n\n\n### np.savez\n\nThe numpy.savez() function saves multiple arrays to a file with the .npz extension.\n\nnumpy.savez(file, *args, **kwds)\nParameter Description:\n\n* **file**: File to save, with .npz extension. If the file path does not end with .npz, the extension will be added automatically.\n* **args**: Arrays to save, you can use keyword arguments to name the arrays. Arrays passed as non-keyword arguments will automatically be named **arr_0**, **arr_1**, … .\n* **kwds**: Arrays to save using keyword names.\n\n## Example\n\nimport numpy as np a = np.array([[1,2,3],[4,5,6]])b = np.arange(0, 1.0, 0.1)c = np.sin(b)np.savez("tutorial.npz", a, b, sin_array = c)r = np.load("tutorial.npz")print(r.files)print(r["arr_0"])print(r["arr_1"])print(r["sin_array"])\n\nThe output is:\n\n['sin_array', 'arr_0', 'arr_1'][ ][0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9][0. 0.09983342 0.19866933 0.29552021 0.38941834 0.47942554 0.56464247 0.64421769 0.71735609 0.78332691]\n### savetxt()\n\nThe savetxt() function stores data in a simple text file format, and the corresponding loadtxt() function is used to retrieve data.\n\nnp.loadtxt(FILENAME, dtype=int, delimiter=' ') np.savetxt(FILENAME, a, fmt="%d", delimiter=",")\nThe delimiter parameter can specify various delimiters, converter functions for specific columns, number of rows to skip, etc.\n\n## Example\n\nimport numpy as np a = np.array([1,2,3,4,5])np.savetxt('out.txt',a)b = np.loadtxt('out.txt')print(b)\n\nThe output is:\n\n[1. 2. 3. 4. 5.]\nUsing the delimiter parameter:\n\n## Example\n\nimport numpy as np a=np.arange(0,10,0.5).reshape(4,-1)np.savetxt("out.txt",a,fmt="%d",delimiter=",")b = np.loadtxt("out.txt",delimiter=",")print(b)\n\nThe output is:\n\n[[0. 0. 1. 1. 2.] [2. 3. 3. 4. 4.] [5. 5. 6. 6. 7.] [7. 8. 8. 9. 9.]]
← Python3 Func OrdNumpy Byte Swapping β†’