Undocumented
Variable | array_function_dispatch |
Undocumented |
Class | BagObj |
BagObj(obj) |
Class | DataSource |
DataSource(destpath='.') |
Class | NpzFile |
NpzFile(fid) |
Function | _floatconv |
Undocumented |
Function | _genfromtxt_dispatcher |
Undocumented |
Function | _getconv |
Find the correct dtype converter. Adapted from matplotlib. |
Function | _loadtxt_dispatcher |
Undocumented |
Function | _loadtxt_flatten_dtype_internal |
Unpack a structured data-type, and produce a packer function. |
Function | _loadtxt_pack_items |
Pack items into nested lists based on re-packing info. |
Function | _save_dispatcher |
Undocumented |
Function | _savetxt_dispatcher |
Undocumented |
Function | _savez |
Undocumented |
Function | _savez_compressed_dispatcher |
Undocumented |
Function | _savez_dispatcher |
Undocumented |
Function | fromregex |
Construct an array from a text file, using regular expression parsing. |
Function | genfromtxt |
Load data from a text file, with missing values handled as specified. |
Function | load |
Load arrays or pickled objects from .npy, .npz or pickled files. |
Function | loadtxt |
Load data from a text file. |
Function | recfromcsv |
Load ASCII data stored in a comma-separated file. |
Function | recfromtxt |
Load ASCII data from a file and return it in a record array. |
Function | save |
Save an array to a binary file in NumPy .npy format. |
Function | savetxt |
Save an array to a text file. |
Function | savez |
Save several arrays into a single file in uncompressed .npz format. |
Function | savez_compressed |
Save several arrays into a single file in compressed .npz format. |
Function | zipfile_factory |
Create a ZipFile. |
Constant | _CONVERTERS |
Undocumented |
Variable | _genfromtxt_with_like |
Undocumented |
Variable | _loadtxt_chunksize |
Undocumented |
Variable | _loadtxt_with_like |
Undocumented |
Undocumented
Find the correct dtype converter. Adapted from matplotlib.
Even when a lambda is returned, it is defined at the toplevel, to allow testing for equality and enabling optimization for single-type data.
Undocumented
Undocumented
Construct an array from a text file, using regular expression parsing.
The returned array is always a structured array, and is constructed from all matches of the regular expression in the file. Groups in the regular expression are converted to fields of the structured array.
Filename or file object to read.
os.PathLike
implementations.Encoding used to decode the inputfile. Does not apply to input streams.
file
that
was matched by regexp
. output
is always a structured array.dtype
is not a valid dtype for a structured array.fromstring, loadtxt
Dtypes for structured arrays can be specified in several forms, but all
forms specify at least the data type and field name. For details see
basics.rec
.
>>> from io import StringIO >>> text = StringIO("1312 foo\n1534 bar\n444 qux")
>>> regexp = r"(\d+)\s+(...)" # match [digits, whitespace, anything] >>> output = np.fromregex(text, regexp, ... [('num', np.int64), ('key', 'S3')]) >>> output array([(1312, b'foo'), (1534, b'bar'), ( 444, b'qux')], dtype=[('num', '<i8'), ('key', 'S3')]) >>> output['num'] array([1312, 1534, 444])
Load data from a text file, with missing values handled as specified.
Each line past the first skip_header
lines is split at the delimiter
character, and characters following the comments
character are discarded.
skiprows
was removed in numpy 1.10. Please use skip_header
instead.missing
was removed in numpy 1.10. Please use missing_values
instead.names
is True, the field names are read from the first line after
the first skip_header
lines. This line can optionally be preceded
by a comment delimiter. If names
is a sequence or a single-string of
comma-separated names, the names will be used to define the field names
in a structured dtype. If names
is None, the names of the dtype
fields will be used, if any.file
would become file_
.The maximum number of rows to read. Must not be used with skip_footer at the same time. If given, the value must be at least 1. Default is to read the entire file.
Encoding used to decode the inputfile. Does not apply when fname
is
a file object. The special value 'bytes' enables backward compatibility
workarounds that ensure that you receive byte arrays when possible
and passes latin1 encoded strings to converters. Override this value to
receive unicode arrays and pass strings as input to converters. If set
to None the system default is used. The default value is 'bytes'.
${ARRAY_FUNCTION_LIKE}
New in version 1.20.0.
usemask
is True, this is a
masked array.numpy.loadtxt : equivalent function when no data is missing.
names
),
there must not be any header in the file (else a ValueError
exception is raised).[1] | NumPy User Guide, section I/O with NumPy. |
>>> from io import StringIO >>> import numpy as np
Comma delimited file with mixed dtype
>>> s = StringIO(u"1,1.3,abcde") >>> data = np.genfromtxt(s, dtype=[('myint','i8'),('myfloat','f8'), ... ('mystring','S5')], delimiter=",") >>> data array((1, 1.3, b'abcde'), dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', 'S5')])
Using dtype = None
>>> _ = s.seek(0) # needed for StringIO example only >>> data = np.genfromtxt(s, dtype=None, ... names = ['myint','myfloat','mystring'], delimiter=",") >>> data array((1, 1.3, b'abcde'), dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', 'S5')])
Specifying dtype and names
>>> _ = s.seek(0) >>> data = np.genfromtxt(s, dtype="i8,f8,S5", ... names=['myint','myfloat','mystring'], delimiter=",") >>> data array((1, 1.3, b'abcde'), dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', 'S5')])
An example with fixed-width columns
>>> s = StringIO(u"11.3abcde") >>> data = np.genfromtxt(s, dtype=None, names=['intvar','fltvar','strvar'], ... delimiter=[1,3,5]) >>> data array((1, 1.3, b'abcde'), dtype=[('intvar', '<i8'), ('fltvar', '<f8'), ('strvar', 'S5')])
An example to show comments
>>> f = StringIO(''' ... text,# of chars ... hello world,11 ... numpy,5''') >>> np.genfromtxt(f, dtype='S12,S12', delimiter=',') array([(b'text', b''), (b'hello world', b'11'), (b'numpy', b'5')], dtype=[('f0', 'S12'), ('f1', 'S12')])
Load arrays or pickled objects from .npy, .npz or pickled files.
Warning
Loading files that contain object arrays uses the pickle module, which is not secure against erroneous or maliciously constructed data. Consider passing allow_pickle=False to load data that is known not to contain object arrays for the safer handling of untrusted sources.
numpy.memmap
for a detailed description of the modes). A
memory-mapped array is kept on disk. However, it can be accessed
and sliced like any ndarray. Memory mapping is especially useful
for accessing small fragments of large files without reading the
entire file into memory.Allow loading pickled object arrays stored in npy files. Reasons for disallowing pickles include security, as loading pickled data can execute arbitrary code. If pickles are disallowed, loading object arrays will fail. Default: False
fix_imports
is True, pickle will try to map the old Python 2 names to the new names
used in Python 3.save, savez, savez_compressed, loadtxt memmap : Create a memory-map to an array stored in a file on disk. lib.format.open_memmap : Create or load a memory-mapped .npy file.
If the file contains pickle data, then whatever object is stored in the pickle is returned.
If the file is a .npy file, then a single array is returned.
If the file is a .npz file, then a dictionary-like object is returned, containing {filename: array} key-value pairs, one for each file in the archive.
If the file is a .npz file, the returned value supports the context manager protocol in a similar fashion to the open function:
with load('foo.npz') as data: a = data['a']
The underlying file descriptor is closed when exiting the 'with' block.
Store data to disk, and load it again:
>>> np.save('/tmp/123', np.array([[1, 2, 3], [4, 5, 6]])) >>> np.load('/tmp/123.npy') array([[1, 2, 3], [4, 5, 6]])
Store compressed data to disk, and load it again:
>>> a=np.array([[1, 2, 3], [4, 5, 6]]) >>> b=np.array([1, 2]) >>> np.savez('/tmp/123.npz', a=a, b=b) >>> data = np.load('/tmp/123.npz') >>> data['a'] array([[1, 2, 3], [4, 5, 6]]) >>> data['b'] array([1, 2]) >>> data.close()
Mem-map the stored array, and then access the second row directly from disk:
>>> X = np.load('/tmp/123.npy', mmap_mode='r') >>> X[1, :] memmap([4, 5, 6])
Load data from a text file.
Each row in the text file must have the same number of values.
genfromtxt
): converters = {3: lambda s: float(s.strip() or 0)}.
Default: None.skiprows
lines, including comments; default: 0.Which columns to read, with 0 being the first. For example, usecols = (1,4,5) will extract the 2nd, 5th and 6th columns. The default, None, results in all columns being read.
The returned array will have at least ndmin
dimensions.
Otherwise mono-dimensional axes will be squeezed.
Legal values: 0 (default), 1 or 2.
Encoding used to decode the inputfile. Does not apply to input streams. The special value 'bytes' enables backward compatibility workarounds that ensures you receive byte arrays as results if possible and passes 'latin1' encoded strings to converters. Override this value to receive unicode arrays and pass strings as input to converters. If set to None the system default is used. The default value is 'bytes'.
Read max_rows
lines of content after skiprows
lines. The default
is to read all the lines.
${ARRAY_FUNCTION_LIKE}
New in version 1.20.0.
load, fromstring, fromregex genfromtxt : Load data with missing values handled as specified. scipy.io.loadmat : reads MATLAB data files
This function aims to be a fast reader for simply formatted files. The
genfromtxt
function provides more sophisticated handling of, e.g.,
lines with missing values.
The strings produced by the Python float.hex method can be used as input for floats.
>>> from io import StringIO # StringIO behaves like a file object >>> c = StringIO("0 1\n2 3") >>> np.loadtxt(c) array([[0., 1.], [2., 3.]])
>>> d = StringIO("M 21 72\nF 35 58") >>> np.loadtxt(d, dtype={'names': ('gender', 'age', 'weight'), ... 'formats': ('S1', 'i4', 'f4')}) array([(b'M', 21, 72.), (b'F', 35, 58.)], dtype=[('gender', 'S1'), ('age', '<i4'), ('weight', '<f4')])
>>> c = StringIO("1,0,2\n3,0,4") >>> x, y = np.loadtxt(c, delimiter=',', usecols=(0, 2), unpack=True) >>> x array([1., 3.]) >>> y array([2., 4.])
This example shows how converters
can be used to convert a field
with a trailing minus sign into a negative number.
>>> s = StringIO('10.01 31.25-\n19.22 64.31\n17.57- 63.94') >>> def conv(fld): ... return -float(fld[:-1]) if fld.endswith(b'-') else float(fld) ... >>> np.loadtxt(s, converters={0: conv, 1: conv}) array([[ 10.01, -31.25], [ 19.22, 64.31], [-17.57, 63.94]])
Load ASCII data stored in a comma-separated file.
The returned array is a record array (if usemask=False, see
recarray
) or a masked record array (if usemask=True,
see ma.mrecords.MaskedRecords
).
fname, kwargs : For a description of input parameters, see genfromtxt
.
numpy.genfromtxt : generic function to load ASCII data.
By default, dtype
is None, which means that the data-type of the output
array will be determined from the data.
Load ASCII data from a file and return it in a record array.
If usemask=False a standard recarray
is returned,
if usemask=True a MaskedRecords array is returned.
fname, kwargs : For a description of input parameters, see genfromtxt
.
numpy.genfromtxt : generic function
By default, dtype
is None, which means that the data-type of the output
array will be determined from the data.
Save an array to a binary file in NumPy .npy format.
fix_imports
is True, pickle
will try to map the new Python 3 names to the old module names used in
Python 2, so that the pickle data stream is readable with Python 2.savez : Save several arrays into a .npz archive savetxt, load
For a description of the .npy format, see numpy.lib.format
.
Any data saved to the file is appended to the end of the file.
>>> from tempfile import TemporaryFile >>> outfile = TemporaryFile()
>>> x = np.arange(10) >>> np.save(outfile, x)
>>> _ = outfile.seek(0) # Only needed here to simulate closing & reopening file >>> np.load(outfile) array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> with open('test.npy', 'wb') as f: ... np.save(f, np.array([1, 2])) ... np.save(f, np.array([1, 3])) >>> with open('test.npy', 'rb') as f: ... a = np.load(f) ... b = np.load(f) >>> print(a, b) # [1 2] [1 3]
Save an array to a text file.
loadtxt
understands gzipped files
transparently.A single format (%10.5f), a sequence of formats, or a
multi-format string, e.g. 'Iteration %d -- %10.5f', in which
case delimiter
is ignored. For complex X
, the legal options
for fmt
are:
fmt='%.4e'
, resulting in numbers formatted
like ' (%s+%sj)' % (fmt, fmt)
' %.4e %+.4ej %.4e %+.4ej %.4e %+.4ej'
for 3 columns['%.3e + %.3ej', '(%.15e%+.15ej)']
for 2 columnsString or character separating lines.
String that will be written at the beginning of the file.
String that will be written at the end of the file.
String that will be prepended to the header and footer strings, to mark them as comments. Default: '# ', as expected by e.g. numpy.loadtxt.
Encoding used to encode the outputfile. Does not apply to output streams. If the encoding is something other than 'bytes' or 'latin1' you will not be able to load the file in NumPy versions < 1.14. Default is 'latin1'.
save : Save an array to a binary file in NumPy .npy format savez : Save several arrays into an uncompressed .npz archive savez_compressed : Save several arrays into a compressed .npz archive
Further explanation of the fmt
parameter
(%[flag]width[.precision]specifier):
- : left justify
+ : Forces to precede result with + or -.
0 : Left pad the number with zeros instead of space (see width).
c : character
d or i : signed decimal integer
e or E : scientific notation with e or E.
f : decimal floating point
g,G : use the shorter of e,E or f
o : signed octal
s : string of characters
u : unsigned decimal integer
x,X : unsigned hexadecimal integer
This explanation of fmt is not complete, for an exhaustive specification see [1].
[1] | Format Specification Mini-Language, Python Documentation. |
>>> x = y = z = np.arange(0.0,5.0,1.0) >>> np.savetxt('test.out', x, delimiter=',') # X is an array >>> np.savetxt('test.out', (x,y,z)) # x,y,z equal sized 1D arrays >>> np.savetxt('test.out', x, fmt='%1.4e') # use exponential notation
Save several arrays into a single file in uncompressed .npz format.
Provide arrays as keyword arguments to store them under the corresponding name in the output file: savez(fn, x=x, y=y).
If arrays are specified as positional arguments, i.e., savez(fn,
x, y), their names will be arr_0
, arr_1
, etc.
kwds
below) to assign names to arrays. Arrays specified as
args will be named "arr_0", "arr_1", and so on.None
save : Save a single array to a binary file in NumPy format. savetxt : Save an array to a file as plain text. savez_compressed : Save several arrays into a compressed .npz archive
The .npz file format is a zipped archive of files named after the
variables they contain. The archive is not compressed and each file
in the archive contains one variable in .npy format. For a
description of the .npy format, see numpy.lib.format
.
When opening the saved .npz file with load
a NpzFile
object is
returned. This is a dictionary-like object which can be queried for
its list of arrays (with the .files attribute), and for the arrays
themselves.
Keys passed in kwds
are used as filenames inside the ZIP archive.
Therefore, keys should be valid filenames; e.g., avoid keys that begin with
/ or contain ..
When naming variables with keyword arguments, it is not possible to name a variable file, as this would cause the file argument to be defined twice in the call to savez.
>>> from tempfile import TemporaryFile >>> outfile = TemporaryFile() >>> x = np.arange(10) >>> y = np.sin(x)
Using savez
with *args, the arrays are saved with default names.
>>> np.savez(outfile, x, y) >>> _ = outfile.seek(0) # Only needed here to simulate closing & reopening file >>> npzfile = np.load(outfile) >>> npzfile.files ['arr_0', 'arr_1'] >>> npzfile['arr_0'] array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
Using savez
with **kwds, the arrays are saved with the keyword names.
>>> outfile = TemporaryFile() >>> np.savez(outfile, x=x, y=y) >>> _ = outfile.seek(0) >>> npzfile = np.load(outfile) >>> sorted(npzfile.files) ['x', 'y'] >>> npzfile['x'] array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
Save several arrays into a single file in compressed .npz format.
Provide arrays as keyword arguments to store them under the corresponding name in the output file: savez(fn, x=x, y=y).
If arrays are specified as positional arguments, i.e., savez(fn,
x, y), their names will be arr_0
, arr_1
, etc.
kwds
below) to assign names to arrays. Arrays specified as
args will be named "arr_0", "arr_1", and so on.None
numpy.save : Save a single array to a binary file in NumPy format. numpy.savetxt : Save an array to a file as plain text. numpy.savez : Save several arrays into an uncompressed .npz file format numpy.load : Load the files created by savez_compressed.
The .npz file format is a zipped archive of files named after the
variables they contain. The archive is compressed with
zipfile.ZIP_DEFLATED and each file in the archive contains one variable
in .npy format. For a description of the .npy format, see
numpy.lib.format
.
When opening the saved .npz file with load
a NpzFile
object is
returned. This is a dictionary-like object which can be queried for
its list of arrays (with the .files attribute), and for the arrays
themselves.
>>> test_array = np.random.rand(3, 2) >>> test_vector = np.random.rand(4) >>> np.savez_compressed('/tmp/123', a=test_array, b=test_vector) >>> loaded = np.load('/tmp/123.npz') >>> print(np.array_equal(test_array, loaded['a'])) True >>> print(np.array_equal(test_vector, loaded['b'])) True
Create a ZipFile.
Allows for Zip64, and the file
argument can accept file, str, or
pathlib.Path objects. args
and kwargs
are passed to the zipfile.ZipFile
constructor.
Undocumented
Value |
|