import numpy as np # a = np.array([1, 2, 3]) # print(a[0]) # b = np.array([[1, 2, 3], [4, 5, 6]]) # print(b[0]) # print(b.shape) # 创造numpy的方式 # np.array(), np.zeros(), np.ones(), # np.empty(), np.arange(), np.linspace(), dtype print(np.zeros(4)) print(np.ones(4, dtype=np.int16)) print(np.empty(3)) # 随机赋值 print(np.arange(4)) print(np.arange(4, 12, 4)) print(np.linspace(0, 10, num=6)) # 生成指定范围内的均匀等差数列 # 增添、移除和排序元素 # np.sort(), np.concatenate() # aixs用来指定轴,第0轴为纵向,第1轴为横向,None为平展排序 print(np.sort(np.array([[2, 4, 3], [3, 7, 1]]), axis=0)) dtype = [('name', 'S10'), ('height', float), ('age', int)] values = [('Arthur', 1.8, 41), ('Lancelot', 1.9, 38), ('Galahad', 1.7, 38)] people = np.array(values, dtype=dtype) # create a structured array print(np.sort(people, order=['name', 'age'])) x = np.array([[1, 2], [3, 4]]) y = np.array([[5, 6]]) print(np.concatenate((x, y), axis=0)) # 获取array的shape和size # ndarray.ndim, ndarray.size, ndarray.shape # 分别是维数、总元素、shape(各个维数的元素个数) # reshape array # arr.reshape() ?arr和ndarray的区别 print(np.arange(6)) # order C按行输出 F按列输出 A按内存顺序输出 print(np.arange(6).reshape((2, 3), order='A')) print(np.reshape(np.arange(6), (2, 3), order='F')) # 把1D array转化为2D array # np.newaxis, np.expand_dims print(np.arange(1, 7)[np.newaxis, :, np.newaxis].shape) print(np.expand_dims(np.arange(1, 7), axis=1).shape) # 索引和切片 # 直接索引[index] # 切片索引1[start:end](不包括end) # 切片索引2[start:] # 若start为正数,从前往后索引;负数,从后往前 # 条件索引 test = np.array([[1, 2, 3, 4], [4, 6, 7, 8], [9, 10, 11, 12]]) print(test[test < 5]) five_up = (test > 5) print(test[five_up]) print(five_up) divisible_by_2 = test[test % 2 == 0] print(divisible_by_2) # 返回满足条件的数字的索引,返回值数据类型为tuple print(np.nonzero(test < 5)) print(test[np.nonzero(test < 5)]) # 怎么从已存在的数据创建array
标签:arange,学习,print,shape,test,np,array,numpy From: https://www.cnblogs.com/zqyn/p/17160305.html