Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

NumPy IO

NumPy IO

Numpy can read and write text data or binary data on disk.

NumPy introduces a simple file format for ndarray objects: npy.

npy files are used to store the data, shape, dtype, and other information needed to reconstruct an ndarray.

Commonly used IO functions include:

numpy.save()

The numpy.save() function saves an array to a file with the .npy extension.

numpy.save(file, arr, allow_pickle=True, fix_imports=True)

Parameter Description:

Example

import

numpy

as

np

a

=

np

.

array

(

[

1

,

2

,

3

,

4

,

5

]

)

Save to the outfile.npy file

np

.

save

(

'

outfile.npy

'

,

a

)

Save to the outfile2.npy file; if the file path does not end with the .npy extension, it will be added automatically

np

.

save

(

'

outfile2

'

,

a

)

We can view the file contents:

$ cat outfile.npy 

?NUMPYv{'descr': '

You can see that the files are garbled, because they are data in Numpy's proprietary binary format.

We can use the load() function to read the data and it will display normally:

Example

import

numpy

as

np

b

=

np

.

load

(

'

outfile.npy

'

)

print

(

b

)

The output is:

[1 2 3 4 5]

np.savez

The numpy.savez() function saves multiple arrays to a file with the npz extension.

numpy.savez(file, *args, **kwds)

Parameter Description:

  • file: The file to save, with the extension .npz, if the file path does not end with the .npz extension, it will be added automatically.
  • args: the array to be saved; you can use a keyword argument to give the array a name, and arrays passed as non-keyword arguments are automatically named arr_0, arr_1, … 。
  • kwds: Use key names for the array to be saved.

Example

import

numpy

as

np

a

=

np

.

array

(

[

[

1

,

2

,

3

]

,

[

4

,

5

,

6

]

]

)

b

=

np

.

arange

(

0

,

1.0

,

0.1

)

c

=

np

.

sin

(

b

)

c uses the keyword argument sin_array

np

.

savez

(

"

runoob.npz

"

,

a

,

b

,

sin_array

=

c

)

r

=

np

.

load

(

"

runoob.npz

"

)

print

(

r

.

files

)

View the names of each array

print

(

r

[

"

arr_0

"

]

)

Array a

print

(

r

[

"

arr_1

"

]

)

Array b

print

(

r

[

"

sin_array

"

]

)

Array c

The output is:

['sin_array', 'arr_0', 'arr_1']

[[1 2 3]

 [4 5 6]]

[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]

savetxt()

The savetxt() function stores data in a simple text file format, and the corresponding loadtxt() function is used to retrieve the data.

np.loadtxt(FILENAME, dtype=int, delimiter=' ')

np.savetxt(FILENAME, a, fmt="%d", delimiter=",")

The delimiter parameter can specify various delimiters, converter functions for specific columns, the number of rows to skip, and so on.

Example

import

numpy

as

np

a

=

np

.

array

(

[

1

,

2

,

3

,

4

,

5

]

)

np

.

savetxt

(

'

out.txt

'

,

a

)

b

=

np

.

loadtxt

(

'

out.txt

'

)

print

(

b

)

The output is:

[1. 2. 3. 4. 5.]

Use the delimiter parameter:

Example

import

numpy

as

np

a

=

np

.

arange

(

0

,

10

,

0.5

)

.

reshape

(

4

,-

1

)

np

.

savetxt

(

"

out.txt

"

,

a

,

fmt

=

"

%d

"

,

delimiter

=

"

,

"

)

Change to save as integers, separated by commas

b

=

np

.

loadtxt

(

"

out.txt

"

,

delimiter

=

"

,

"

)

When using load, it must also be specified as comma-separated

print

(

b

)

The output is:

[[0. 0. 1. 1. 2.]

 [2. 3. 3. 4. 4.]

 [5. 5. 6. 6. 7.]

 [7. 8. 8. 9. 9.]]