Python之路——numpy各函数简介之生成数组函数(Array creation routines)
2010-09-01 13:19
956 查看
1、empty(shape[, dtype, order])
依据给定形状和类型(shape[, dtype, order])返回一个新的空数组。参数:
shape : 整数或者整型元组
定义返回数组的形状;
dtype : 数据类型,可选
定义返回数组的类型。
order : {‘C’, ‘F’}, 可选
规定返回数组元素在内存的存储顺序:C(C语言)-row-major;F(Fortran)column-major。
>>> np.empty([2, 2]) array([[ -9.74499359e+001, 6.69583040e-309], [ 2.13182611e-314, 3.06959433e-309]]) #random >>> np.empty([2, 2], dtype=int) array([[-1073741821, -1067949133], [ 496041986, 19249760]]) #random
2、empty_like(a)
依据给定数组(a)的形状和类型返回一个新的空数组。参数:
a:数组
其形状和类型用来规定返回函数的形状和类型。
返回值:
输出:ndarray
与数组a形状和类型一样的数组。
>>> np.eye(2, dtype=int) array([[1, 0], [0, 1]]) >>> np.eye(3, k=1) array([[ 0., 1., 0.], [ 0., 0., 1.], [ 0., 0., 0.]])4、identity(n[, dtype])
返回一个N维单位方阵。
参数:
n : 整数
返回方阵的行列数;
dtype : 数据类型,可选
返回方阵的数据类型,默认为float.
返回值:
输出: ndarray
n x n 单位方阵。>>> np.ones(5) array([ 1., 1., 1., 1., 1.]) >>> np.ones((5,), dtype=np.int) array([1, 1, 1, 1, 1]) >>> np.ones((2, 1)) array([[ 1.], [ 1.]]) >>> s = (2,2) >>> np.ones(s) array([[ 1., 1.], [ 1., 1.]])6、ones_like()
依据给定数组(a)的形状和类型返回一个新的元素全部为1的数组。等同于a.copy().fill(1),具体使用请参考zeros_like的文档。>>> np.zeros(5) array([ 0., 0., 0., 0., 0.]) >>> np.zeros((5,), dtype=numpy.int) array([0, 0, 0, 0, 0]) >>> np.zeros((2, 1)) array([[ 0.], [ 0.]]) >>> s = (2,2) >>> np.zeros(s) array([[ 0., 0.], [ 0., 0.]]) >>> np.zeros((2,), dtype=[(’x’, ’i4’), (’y’, ’i4’)]) # custom dtype array([(0, 0), (0, 0)], dtype=[(’x’, ’<i4’), (’y’, ’<i4’)])8、zeros_like(a)
依据给定数组(a)的形状和类型返回一个新的元素全部为1的数组。
等同于a.copy().fill(0)。
参数:
a : array_like
输出:ndarray
与a数组形状类型一致的0数组。
>>> x = np.arange(6)
>>> x = x.reshape((2, 3))
>>> x
array([[0, 1, 2],
[3, 4, 5]])
>>> np.zeros_like(x)
array([[0, 0, 0],
[0, 0, 0]])
>>> y = np.arange(3, dtype=np.float)
>>> y
array([ 0., 1., 2.])
>>> np.zeros_like(y)
array([ 0., 0., 0.])
待编辑………………………………
相关文章推荐
- python ——numpy各函数简介之生成数组函数(Array creation routines)
- Python之路——numpy各函数简介之生成数组函数(Array creation routines)
- numpy各函数简介之生成数组函数(Array creation routines)
- Python之路——numpy各函数简介之生成数组函数(Array creation routines)
- Python numpy.array() 生成相同元素数组的方法
- 基于Python Numpy的数组array和矩阵matrix详解
- Python Numpy的数组array和矩阵matrix
- 学习Python数据分析随手笔记【一】numpy数组的函数简单应用
- python numpy:1 numpy.array和numpy.matrix常用函数使用
- 用list去初始化numpy的array数组 numpy的array和python中自带的list之间相互转化
- python 用numpy生成多维随机数组
- Python多维数组之numpy.array
- python Numpy—— 数组array
- Python-Numpy(2)Array数组操作
- 【Python】Numpy扩充数组函数之repeat和tile用法
- 【Python】Numpy扩充数组函数之repeat和tile用法
- python numpy 如何获取和更改数组(array)的形状、维数-shape&reshape
- Python Numpy的数组array和矩阵matrix的用法与区别
- numpy各函数简介之生成数组函数
- 【Python】无须numpy,利用map函数与zip(*)函数对数组转置(转)