1. 读取文件
numpy.fromfile
numpy.fromfile(file, dtype=float, count=-1, sep='', offset=0, *, like=None)
Construct an array from data in a text or binary file.
A highly efficient way of reading binary data with a known data-type, as well as parsing simply formatted text files. Data written using the tofile method can be read using this function.
Parameters:
-
file
file or str or Path Open file object or filename. -
dtype
data-type Data type of the returned array. For binary files, it is used to determine the size and byte-order of the items in the file. Most builtin numeric types are supported and extension types may be supported. -
count
int Number of items to read. -1 means all items (i.e., the complete file). -
sep
str 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. -
offset
int The offset (in bytes) from the file’s current position. Defaults to 0. Only permitted for binary files. -
like
array_like, optional Reference object to allow the creation of arrays which are not NumPy arrays. If an array-like passed in as like supports the array_function protocol, the result will be defined by it. In this case, it ensures the creation of an array object compatible with that passed in via this argument.
New in version 1.20.0.
2. 读取内存
来源:https://numpy.org/doc/2.2/reference/generated/numpy.frombuffer.html
numpy.frombuffer
numpy.frombuffer(buffer, dtype=float, count=-1, offset=0, *, like=None)
Interpret a buffer as a 1-dimensional array.
Parameters:
-
buffer
buffer_like An object that exposes the buffer interface. -
dtype
data-type, optional Data-type of the returned array; default: float. -
count
int, optional Number of items to read. -1 means all data in the buffer. -
offset
int, optional Start reading the buffer from this offset (in bytes); default: 0. -
like
array_like, optional Reference object to allow the creation of arrays which are not NumPy arrays. If an array-like passed in as like supports the array_function protocol, the result will be defined by it. In this case, it ensures the creation of an array object compatible with that passed in via this argument.
New in version 1.20.0.
Returns:
out
: ndarray
3. from_dlpack
numpy.from_dlpack numpy.from_dlpack(x, /, *, device=None, copy=None) Create a NumPy array from an object implementing the dlpack protocol. Generally, the returned NumPy array is a read-only view of the input object. See [1] and [2] for more details.
Parameters : x object A Python object that implements the dlpack and dlpack_device methods.
device device, optional Device on which to place the created array. Default: None. Must be "cpu" if passed which may allow importing an array that is not already CPU available.
copy bool, optional Boolean indicating whether or not to copy the input. If True, the copy will be made. If False, the function will never copy, and will raise BufferError in case a copy is deemed necessary. Passing it requests a copy from the exporter who may or may not implement the capability. If None, the function will reuse the existing memory buffer if possible and copy otherwise. Default: None.
Returns : out ndarray
4. fromfunction
numpy.fromfunction
numpy.fromfunction(function, shape, *, dtype=<class 'float'>, like=None, **kwargs)[source]
Construct an array by executing a function over each coordinate.
The resulting array therefore has a value fn(x, y, z) at coordinate (x, y, z).
Parameters: functioncallable The function is called with N parameters, where N is the rank of shape. Each parameter represents the coordinates of the array varying along a specific axis. For example, if shape were (2, 2), then the parameters would be array([[0, 0], [1, 1]]) and array([[0, 1], [0, 1]])
shape(N,) tuple of ints Shape of the output array, which also determines the shape of the coordinate arrays passed to function.
dtypedata-type, optional Data-type of the coordinate arrays passed to function. By default, dtype is float.
likearray_like, optional Reference object to allow the creation of arrays which are not NumPy arrays. If an array-like passed in as like supports the array_function protocol, the result will be defined by it. In this case, it ensures the creation of an array object compatible with that passed in via this argument.
New in version 1.20.0.
Returns: fromfunction any The result of the call to function is passed back directly. Therefore the shape of fromfunction is completely determined by function. If function returns a scalar value, the shape of fromfunction would not match the shape parameter.
5. fromiter
numpy.fromiter
numpy.fromiter(iter, dtype, count=-1, *, like=None)
Create a new 1-dimensional array from an iterable object.
Parameters: iteriterable object An iterable object providing data for the array.
dtypedata-type The data-type of the returned array.
Changed in version 1.23: Object and subarray dtypes are now supported (note that the final result is not 1-D for a subarray dtype).
countint, optional The number of items to read from iterable. The default is -1, which means all data is read.
like
array_like, optional Reference object to allow the creation of arrays which are not NumPy arrays. If an array-like passed in as like supports the__array_function__
protocol, the result will be defined by it. In this case, it ensures the creation of an array object compatible with that passed in via this argument.
New in version 1.20.0.
Returns: out ndarray The output array.
6. fromstring
numpy.fromstring
numpy.fromstring(string, dtype=float, count=-1, *, sep, like=None)
A new 1-D array initialized from text data in a string.
Parameters: stringstr A string containing the data.
dtypedata-type, optional The data type of the array; default: float. For binary input data, the data must be in exactly this format. Most builtin numeric types are supported and extension types may be supported.
countint, optional Read this number of dtype elements from the data. If this is negative (the default), the count will be determined from the length of the data.
sepstr, optional The string separating numbers in the data; extra whitespace between elements is also ignored.
Deprecated since version 1.14: Passing sep='', the default, is deprecated since it will trigger the deprecated binary mode of this function. This mode interprets string as binary bytes, rather than ASCII text with decimal numbers, an operation which is better spelt frombuffer(string, dtype, count). If string contains unicode text, the binary mode of fromstring will first encode it into bytes using utf-8, which will not produce sane results.
likearray_like, optional Reference object to allow the creation of arrays which are not NumPy arrays. If an array-like passed in as like supports the array_function protocol, the result will be defined by it. In this case, it ensures the creation of an array object compatible with that passed in via this argument.
New in version 1.20.0.
Returns: arr ndarray The constructed array.
Raises: ValueError If the string is not the correct size to satisfy the requested dtype and count.