"""np.arrayobject 数组或嵌套的数列dtype 数组元素的数据类型,可选copy 对象是否需要复制,可选order 创建数组的样式,C为行方向,F为列方向,A为任意方向(默认)subok 默认返回一个与基类类型一致的数组ndmin 指定生成数组的最小维度"""## num = np.array([[x * x for x in range(10)], [x for x in range(20, 30)]], dtype=np.float32)# print num.ndim# print num.shape# print num.size# print num.dtype# print num.itemsize# print num.flags# # print num.data# num = np.empty([4, 3], dtype=np.int32, order="F")# print num# num = np.zeros([4, 3], order="F")# print num# num = np.ones([4, 3], order="C")# print num# asarray a 可以是列表 元祖 多维数组# lis = [x for x in range(10)]# num = np.asarray(a=lis, dtype=None, order=None)# print num## # Python2 frombuffer# s = 'Hello World'# a = np.frombuffer(s, dtype='S1')# print a# # Python3 字符需要转换为bytestrint## s = b'Hello World'# a = np.frombuffer(s, dtype='S1')# print a## # 使用迭代器创建 ndarray# x = np.fromiter(iter(range(3)), dtype=float)# print x"""numpy.arangestart 起始值,默认为0stop 终止值(不包含)step 步长,默认为1dtype 返回ndarray的数据类型,如果没有提供,则会使用输入数据的类型。"""# num = np.arange(5)# print num## num = np.arange(5, 20, 5)# print num"""numpy.linspacenumpy.linspace 函数用于创建一个一维数组,数组是一个等差数列构成的参数 描述start 序列的起始值stop 序列的终止值,如果endpoint为true,该值包含于数列中num 要生成的等步长的样本数量,默认为50endpoint 该值为 ture 时,数列中中包含stop值,反之不包含,默认是True。retstep 如果为 True 时,生成的数组中会显示间距,反之不显示。dtype ndarray 的数据类型"""# num = np.linspace(0, 10, num=3, endpoint=True, retstep=False, dtype=None)# print num## num = np.linspace(10, 20, 5, endpoint=False)# print num"""numpy.logspacenumpy.logspace 函数用于创建一个于等比数列。格式如下:np.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None)base 参数意思是取对数的时候 log 的下标。参数 描述start 序列的起始值为:base ** startstop 序列的终止值为:base ** stop。如果endpoint为true,该值包含于数列中num 要生成的等步长的样本数量,默认为50endpoint 该值为 ture 时,数列中中包含stop值,反之不包含,默认是True。base 对数 log 的底数。dtype ndarray 的数据类型"""# num = np.logspace(3, 50, 20, base=2)# print num#"""NumPy 切片和索引ndarray对象的内容可以通过索引或切片来访问和修改,与 Python 中 list 的切片操作一样。ndarray 数组可以基于 0 - n 的下标进行索引,切片对象可以通过内置的 slice 函数,并设置 start, stop 及 step 参数进行,从原数组中切割出一个新数组。"""# a = np.arange(10)# s = slice(2, 7, 2) # 从索引 2 开始到索引 7 停止,间隔为2# print "origin:", a# print (a[s])# print a[1]# a = np.array([[1, 2, 3], [3, 4, 5], [4, 5, 6]])# print a# print(a[2:])# print(a[2:, 1])# print(a[2:, 1:])a = np.array([[1, 2, 3], [3, 4, 5], [4, 5, 6]])print (a[..., 1:]) # 第2列元素# print (a[1, ...]) # 第2行元素# print (a[..., 1:]) # 第2列及剩下的所有元素
来源:https://www.cnblogs.com/jian-gao/p/10937973.html