NumPy之array
ndarray是一个包含了相同元素类型和大小的多维数组。
创建数组:
1、使用系统方法
empty(shape[, dtype, order]) # 根据给定的参数创建一个ndarray数组,值用随机数填充
例:
>>> np.empty([2, 2])
array([[ -9.74499359e+001, 6.69583040e-309],[ 2.13182611e-314, 3.06959433e-309]])
empty_like(a[, dtype, order, subok]) #和empty不同的是,需要给出一个array的模板,就是a参数,新生成的ndarray继承了a的shape和dtype
例:
>>> a = ([1,2,3], [4,5,6])
>>> np.empty_like(a)
array([[-1073741821, -1073741821, 3], #random[ 0, 0, -1073741821]])
eye(N[, M, k, dtype]) #生成一个N行M列的数组,K指定一条斜线,这条斜线上的值都是1,数组的其他元素维0
例:生成一个5行4列的数组,索引为1的斜线上全部是1,其他元素为0
>>> np.eye(5,4,1)
array([[ 0., 1., 0., 0.],[ 0., 0., 1., 0.],[ 0., 0., 0., 1.],[ 0., 0., 0., 0.],[ 0., 0., 0., 0.]])
identity(n[, dtype]) #生成一个正方形的数组即N×N类型的数组,且索引万恶哦0的斜线上维1,其他元素维0
例:
>>> np.identity(3)
array([[ 1., 0., 0.],[ 0., 1., 0.], [ 0., 0., 1.]])
ones(shape[, dtype, order]) #生成一个指定shape和dtype的数组,用1填充
例:
>>> np.ones(5)
array([ 1., 1., 1., 1., 1.])
>>> np.ones((2, 1))
array([[ 1.],[ 1.]])
ones_like(a[, dtype, order, subok]) #和ones的区别就是需要给定一个dnarray模板,新生成的array继承了a的shape和dtype
例:
>>> x = np.arange(6)
>>> x = x.reshape((2, 3))
>>> x
array([[0, 1, 2],[3, 4, 5]])
>>> np.ones_like(x)
array([[1, 1, 1],[1, 1, 1]])
zeros(shape[, dtype, order]) #根据给定的shape,和dtype生成一个由0填充的数组
例:
>>> np.zeros(5)
array([ 0., 0., 0., 0., 0.])
zeros_like(a[, dtype, order, subok]) #根据a模板生成一个新的用0 填充的ndarray数组
例:
>>> 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]])
full(shape, fill_value[, dtype, order]) #用指定的值填充数组
例:
>>> np.full((2, 2), 10)
array([[10, 10],[10, 10]])
full_like(a, fill_value[, dtype, order, subok]) #根据a模板的shape和dtype生成一个数组,如果指定的填充数不是a的dtype类型,会向下取整,这时候也可以指定新数组的dtype类型。
例:
>>> x = np.arange(6, dtype=np.int)
>>> np.full_like(x, 1)
array([1, 1, 1, 1, 1, 1])
>>> np.full_like(x, 0.1) #如果full_value设置为1.2则就是用1填充
array([0, 0, 0, 0, 0, 0])
>>> np.full_like(x, 0.1, dtype=np.double)
array([ 0.1, 0.1, 0.1, 0.1, 0.1, 0.1])
>>> np.full_like(x, np.nan, dtype=np.double)
array([ nan, nan, nan, nan, nan, nan])
2、用指定的数据填充
array(object[, dtype, copy, order, subok, ndmin]) #用对象直接填充数组
例:
>>> np.array([1, 2, 3]) #一维数组
array([1, 2, 3])
>>> np.array([[1, 2], [3, 4]]) #二维数组
array([[1, 2],[3, 4]])
>>> np.array([1, 2, 3], ndmin=2) #只有一个元素的二维数组
array([[1, 2, 3]])
>>> np.array(np.mat('1 2; 3 4')) #从子类创建
array([[1, 2],[3, 4]])
asarray(a[, dtype, order]) #把lists, lists of tuples, tuples, tuples of tuples, tuples of lists and ndarrays转化为array
例:
>>> a = [1, 2]
>>> np.asarray(a)
array([1, 2])
asanyarray(a[, dtype, order]) #通过ndarray的子类创建array
>>> a = [1, 2]
>>> np.asanyarray(a)
array([1, 2])
ascontiguousarray(a[, dtype]) #返回一个地址连续的数组(C order)
>>> x = np.arange(6).reshape(2,3)
>>> np.ascontiguousarray(x, dtype=np.float32)
array([[ 0., 1., 2.],[ 3., 4., 5.]], dtype=float32)
>>> x.flags['C_CONTIGUOUS']
True
asmatrix(data[, dtype]) # 把数组转化为矩阵,新的变量没有copy数据,只是指向原有的数据
>>> x = np.array([[1, 2], [3, 4]])
>>> m = np.asmatrix(x)
>>> x[0,0] = 5
>>> m
matrix([[5, 2], [3, 4]])
copy(a[, order]) #顾名思义就是复制的意思
>>> x = np.array([1, 2, 3])
>>> y = x
>>> z = np.copy(x)
>>> x[0] = 10
>>> x[0] == y[0]
True
>>> x[0] == z[0]
False
frombuffer(buffer[, dtype, count, offset]) #把buffer数据转化为1维数组ps:如果数据不是机器字节顺序,需要指定他的dtype类型
>>> s = 'hello world'
>>> np.frombuffer(s, dtype='S1', count=5, offset=6)
array(['w', 'o', 'r', 'l', 'd'],dtype='|S1')
frombuffer(buffer[, dtype, count, offset]) #从文件读取数据 ps:该方法不长用用save替代
fromfunction(function, shape, **kwargs) #用方法计算出来的数据填充数组
>>> np.fromfunction(lambda i, j: i + j, (3, 3), dtype=int)
array([[0, 1, 2],[1, 2, 3], [2, 3, 4]])
fromiter(iterable, dtype[, count]) #通过迭代器生成一个一维数组
>>> iterable = (x*x for x in range(5))
>>> np.fromiter(iterable, np.float)
array([ 0., 1., 4., 9., 16.])
fromstring(string[, dtype, count, sep]) #把二进制流或者字符串转化维数组
>>> np.fromstring('\x01\x02', dtype=np.uint8)
array([1, 2], dtype=uint8)
>>> np.fromstring('1 2', dtype=int, sep=' ')
array([1, 2])
>>> np.fromstring('1, 2', dtype=int, sep=',')
array([1, 2])
>>> np.fromstring('\x01\x02\x03\x04\x05', dtype=np.uint8, count=3)
array([1, 2, 3], dtype=uint8)
arange([start,] stop[, step,][, dtype]) |
#根据给定的区间创建连续的值
>>> np.arange(3)
array([0, 1, 2])
>>> np.arange(3.0)
array([ 0., 1., 2.])
>>> np.arange(3,7)
array([3, 4, 5, 6])
>>> np.arange(3,7,2)
array([3, 5])
numpu.linspace(start,stop,num=50,endpoint=True,retstep=False,dtype=None) #arrange一样,主要是生成浮点型
>>> np.linspace(2.0, 3.0, num=5)
array([ 2. , 2.25, 2.5 , 2.75, 3. ])
>>> np.linspace(2.0, 3.0, num=5, endpoint=False)
array([ 2. , 2.2, 2.4, 2.6, 2.8])
>>> np.linspace(2.0, 3.0, num=5, retstep=True)
(array([ 2. , 2.25, 2.5 , 2.75, 3. ]), 0.25)
numpy.logspace(start,stop,num=50,endpoint=True,base=10.0,dtype=None) #log函数.base默认值为10
>>> np.logspace(2.0, 3.0, num=4)
array([ 100. ,215.443469 , 464.15888336, 1000. ])
>>> np.logspace(2.0, 3.0, num=4, endpoint=False)
array([ 100. ,177.827941 , 316.22776602, 562.34132519])
>>> np.logspace(2.0, 3.0, num=4, base=2.0)
array([ 4. ,5.0396842 , 6.34960421, 8. ])
numpy.geomspace(start,stop,num=50,endpoint=True,dtype=None) #几何级增长
>>> np.geomspace(1, 1000, num=4)
array([ 1., 10., 100., 1000.])
>>> np.geomspace(1, 1000, num=3, endpoint=False)
array([ 1., 10., 100.])
>>> np.geomspace(1, 1000, num=4, endpoint=False)
array([ 1. ,5.62341325, 31.6227766 , 177.827941 ])
>>> np.geomspace(1, 256, num=9)
array([ 1., 2., 4., 8., 16., 32., 64., 128., 256.])
numpy.emshgrid(*xi,**kwargs) #把向量坐标转化为矩阵坐标;在二维度数组中长度为M,N的的俩个数组作为输入:如果indexing='ij',则shape(M,N)如果indexing='xy'则shape(N.M)
>>> nx, ny = (3, 2)
>>> x = np.linspace(0, 1, nx)
>>> y = np.linspace(0, 1, ny)
>>> xv, yv = np.meshgrid(x, y)
>>> xv
array([[ 0. , 0.5, 1. ],[ 0. , 0.5, 1. ]])
>>> yv
array([[ 0., 0., 0.],[ 1., 1., 1.]])
numpy.diag(v,k=0) #提取对角或构建一个对角阵
>>> x = np.arange(9).reshape((3,3))
>>> xarray([[0, 1, 2],[3, 4, 5], [6, 7, 8]])
>>> np.diag(x)
array([0, 4, 8])
>>> np.diag(x, k=1)
array([1, 5])
>>> np.diag(x, k=-1
array([3, 7])
numpy.diagflat(v,k=0) #一个扁平输入作为一个二维数组的对角
>>> np.diagflat([[1,2], [3,4]])
array([[1, 0, 0, 0],[0, 2, 0, 0], [0, 0, 3, 0], [0, 0, 0, 4]])
numpy.tri(N,M=None,k=0,dtype=<type 'float'>) #这个不会翻译,但是看数据有点映像
>>> np.tri(3, 5, 2, dtype=int)
array([[1, 1, 1, 0, 0],[1, 1, 1, 1, 0], [1, 1, 1, 1, 1]])
numpy.vander(x,N=None,incresing=False) # 范德蒙式,程序不重要,重要的是科学计算
>>> x = np.array([1, 2, 3, 5])
>>> N = 3
>>> np.vander(x, N)
array([[ 1, 1, 1],[ 4, 2, 1], [ 9, 3, 1], [25, 5, 1]])
numpy.mat(data,dtype=None) #输入转化为矩阵,创建一个新变量,指向旧的数据
>>> x = np.array([[1, 2], [3, 4]])
>>> m = np.asmatrix(x)
>>> x[0,0] = 5
>>> m
matrix([[5, 2], [3, 4]])
numpy.bmat(obj,ldict=None,gdict=None) #用字符串,嵌套序列,或者数组创建矩阵
>>> A = np.mat('1 1; 1 1')
>>> B = np.mat('2 2; 2 2')
>>> C = np.mat('3 4; 5 6')
>>> D = np.mat('7 8; 9 0')>>> np.bmat([[A, B], [C, D]])
matrix([[1, 1, 2, 2],[1, 1, 2, 2], [3, 4, 7, 8], [5, 6, 9, 0]])
>>> np.bmat(np.r_[np.c_[A, B], np.c_[C, D]])
matrix([[1, 1, 2, 2],[1, 1, 2, 2], [3, 4, 7, 8], [5, 6, 9, 0]])
>>> np.bmat('A,B; C,D')
matrix([[1, 1, 2, 2],[1, 1, 2, 2], [3, 4, 7, 8], [5, 6, 9, 0]])
转载于:https://blog.51cto.com/qianqiansun/1964097
NumPy之array相关推荐
- python中的[:-1] [:,:-1] python矩阵numpy中array的冒号 逗号
如何理解[:-1] [:,:-1] data = np.loadtxt('data.csv', delimiter = ',') X = data[:,:-1] #取所有行,取列到倒数 ...
- python numpy:array、asarray、asanyarray的区别
array对目标做一个拷贝,而asarray不会 np.asanyarray 会返回 ndarray 或者ndarray的子类,而np.asarray 只返回 ndarray. (这个我没看懂是啥意思 ...
- (python numpy) np.array.shape 中 (3,)、(3,1)、(1,3)的区别
(python numpy) np.array.shape 中 (3,).(3,1).(1,3)的区别 被人问到这个问题,就记录一下吧 1. (3,) (3,)是[x,y,z][x,y,z][x,y, ...
- numpy数组array的shape属性-1维、2维···
numpy数组array的shape属性-1维.2维··· numpy 创建的数组都有一个shape属性,它是一个元组,返回各个维度的维数.有时候我们可能需要知道某一维的特定维数. 一维情况 二维 可 ...
- python的numpy(array)
import numpy as np # 导入numpy模块 a = np.array([1,2,3]) print(type(a)) # a的类型是numpy.ndarray print(a.sha ...
- numpy 的array 用于矩阵运算 与 python的列表表示矩阵的区别
import numpy as np X= np.array([[1, 11], [2, 22], [3, 33], [4, 44], [5 ,55]]) #numpy.array batch ...
- numpy的array合并-【老鱼学numpy】
概述 本节主要讲述如何把两个数组按照行或列进行合并. 按行进行上下合并 例如: import numpy as np a = np.array([1, 1, 1]) b = np.array([2, ...
- python的数组属性_[Python]numpy use array属性,pythonnumpy,使用,数组
numpy数组属性 目录 首先生成个数组 import numpy as np a = np.array([[1,2,3],[4,5,6]]) print(a) 结果输出: array([[1, 2, ...
- A02_Python(基本数据类型,容器,函数,类),Numpy(数组array,数组索引,数据类型,数组中的数学,广播)
Python的版本 基本数据类型 与大多数语言一样,Python有许多基本类型,包括整数,浮点数,布尔值和字符串.这些数据类型的行为方式与其他编程语言相似. Numbers(数字类型):代表的是整数和 ...
最新文章
- fill value must be in categories解决一例
- 在Java 8之前,您编写了几行代码来对对象集合进行排序?
- JavaScript中的arguments对象
- XML——XML的那些事
- phpstorm设置背景图片
- 项目管理PMP高频考点复习资料(2-2)
- 《HelloGitHub》第 71 期
- DXP2004/Altium Desinger 自己画元器件和封装,及注意事项
- 萝卜内存对比工具使用说明
- 【听说】比付费软件更好的94个免费软件
- 电商平台数据仓库搭建01-项目介绍
- flux 中的 buffer 的原理
- 键盘事件(onkeyup onkeydown)
- Oracle函数之单值函数
- Python 08-文件读写
- 初面蚂蚁金服,培训java知识
- spring注解及扩展
- 5G消息赋能,菊风助力银行业加速融入数字化场景生态
- 妙味课堂H5音乐播放器实战视频课程 ajax实战教程
- 【转帖】360密盘的惊人内幕 — 当然包括QQ文件保险箱
热门文章
- var_dump()
- .net OCX 无法获取“****”控件的窗口句柄,不支持无窗口的ActiveX控 新解决方法...
- [Asp.net]绝对路径和相对路径
- 基于OpenCV之视频读取,处理和显示框架的搭建(一)
- SBO应用技术一则-格式化
- ThinkSNS+ 是如何计算字符显示长度的
- 湖南省第八届大学生程序设计大赛原题 D - 平方根大搜索 UVA 12505 - Searching in sqrt(n)...
- cocos dos命令
- iis下 ActiveSync插件无法访问(下)
- 纵深防御仍对付得了当今的网络威胁吗?