来自Numpy中文官网
fromfunction
fromfile
numpy.fromfunction
numpy.fromfunction(function, shape, *, dtype=<class ‘float’>, **kwargs)
【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).】
利用一个function,来建立一个array(想到了lambda)
Parameters:
-
function:callable
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]])
设array的size为N,则这N个下标上的值,由下标自己生成
比如二维array第一个元素的下标为[0,0],function是 lambda i,j:i + j,则这个元素的值是0+0=0,以此类推 -
shape(N,) tuple of ints
Shape of the output array, which also
determines the shape of the coordinate arrays passed to function.
决定array的shape,元组型式 -
dtypedata-type, optional
Data-type of the coordinate arrays passed to
function. By default, dtype is float.
数据类型,默认为float
Examples
np.fromfunction(lambda i, j: i == j, (3, 3), dtype=int)
array([[ True, False, False],
[False, True, False],
[False, False, True]])
np.fromfunction(lambda i, j: i + j, (3, 3), dtype=int)
array([[0, 1, 2],
[1, 2, 3],
[2, 3, 4]])
numpy.fromfile
numpy.fromfile(file, dtype=float, count=-1, sep=’’, offset=0)
【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.】
从一个文档,或一个二进制文件读取数据来创建array。作为一个已知数据类型情况下,读取二进制数据的高效方法,它也可以解析简单的格式化文件。用tofile方法写入的数据可以用这个function来读取
Parameters
-
file:file or str or Path
Open file object or filename.Changed in version 1.17.0: pathlib.Path objects are now
accepted.
文件名 -
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.New in version 1.18.0: Complex dtypes.
对于二进制文件,它被用来定义大小,以及文件的中元素的字节顺序。大多数写入的数字类型可被支持,延伸的类型也可能被支持(看不懂) -
count:int
Number of items to read
-1 means all items (i.e., the complete file).
-1代表读取全部 -
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.
如果是一个text文件,则可用于元素之间的分割。"“表示这个文件应该被当作是二进制文件。” "能够匹配0个或者多个空白字符。一个仅有空格组成的分隔符必须配对至少一个空格。 -
offset:int
The offset (in bytes) from the file’s current position. Defaults to 0. Only permitted for binary files.New in version 1.17.0.
偏差,默认为0
Notes: Do not rely on the combination of tofile and fromfile for data storage, as the binary files generated are not platform independent. In particular, no byte-order or data-type information is saved. Data can be stored in the platform independent .npy format using save and load instead.
不要结合使用tofile和fromfile来数据存储,因为生成的二进制文件没有平台独立性。特别的,无字节顺序的或者没有数据类型的信息可以被存储。数据也可以 以.npy后缀存储,使用save和load function
Examples: Construct an ndarray:
# 自定义类型
dt = np.dtype([('time', [('min', np.int64), ('sec', np.int64)]),
('temp', float)])
x = np.zeros((1,), dtype=dt)
x['time']['min'] = 10; x['temp'] = 98.25
x:array([((10, 0), 98.25)],
dtype=[('time', [('min', '<i8'), ('sec', '<i8')]), ('temp', '<f8')])
#niu bi a
#Save the raw data to disk
import tempfile
fname = tempfile.mkstemp()[1] # 这啥
x.tofile(fname) # 存储至fname中
#Read the raw data from disk:
np.fromfile(fname, dtype=dt) # 已知类型为dt,从fname中读取
array([((10, 0), 98.25)], # 得到
dtype=[('time', [('min', '<i8'), ('sec', '<i8')]), ('temp', '<f8')])
#The recommended way to store and load data: # 推荐的方法,通过save,load(文件名+".npy")
np.save(fname, x)
np.load(fname + '.npy')
array([((10, 0), 98.25)],
dtype=[('time', [('min', '<i8'), ('sec', '<i8')]), ('temp', '<f8')])