numpy系列(1)-数组创建及属性

1. 预

a               //查看a
a.dtype         //查看dtype类型
a.astype(int)   //将a转为int型

2. 创建ndarray

  • 使用np.array方法
np.array([[1, 2, 3], [4, 5, 6]])
np.array([(1, 2), (3, 4), (5, 6)])
  • b. 使用arange
numpy.arange(start, stop, step, dtype=None)  //[开始, 停止)
  • c. 使用linspace
numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
    	□ start:序列的起始值。
    	□ stop:序列的结束值。
    	□ num:生成的样本数。默认值为50。
    	□ endpoint:布尔值,如果为真,则最后一个样本包含在序列内。
    	□ retstep:布尔值,如果为真,返回间距。
    	□ dtype:数组的类型。
  • d. 使用ones/zeros
numpy.ones(shape, dtype=None, order='C')
	□ shape:用于指定数组形状,例如(1, 2)或 3。
	□ dtype:数据类型。
	□ order:{'C','F'},按行或列方式储存数组。
  • e. 使用eye对角narray
numpy.eye(N, M=None, k=0, dtype=<type 'float'>)
	□ N:输出数组的行数。
	□ M:输出数组的列数。
	□ k:对角线索引:0(默认)是指主对角线,正值是指上对角线,负值是指下对角线。
  • f. 从已知数据创建,我们还可以从已知数据文件、函数中创建 ndarray。NumPy 提供了下面 5 个方法:
■ frombuffer(buffer):将缓冲区转换为 1 维数组。
	□ np.fromfunction(lambda a, b: a + b, (5, 4))
	
■ fromfile(file,dtype,count,sep):从文本或二进制文件中构建多维数组。
■ fromfunction(function,shape):通过函数返回值来创建多维数组。
■ fromiter(iterable,dtype,count):从可迭代对象创建 1 维数组。
■ fromstring(string,dtype,count,sep):从字符串中创建 1 维数组。

3. ndarray数组属性,以a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])为例

  • a. 转置
a.T
a.transpose()
  • b. 输出数组类型
a.dtype
  • c. 输出元素实部/虚部
a.real
a.imag
  • d. 输出元素个数
a.size
  • e. 输出单个元素字节数/总元素字节数
a.itemsize
a.nbytes
  • f. 输出数组维度
a.ndim
  • g. 输出数组形状
a.shape
  • h. 用来遍历数组时,输出每个维度中步进的字节数组。注意是字节数
a.strides
原文地址:https://www.cnblogs.com/thgpddl/p/14236161.html