Python学习笔记:Numpy说明

#Numpy数组的创建

a1  = np.array([1,3,5,7,9])
a2 =  np.zeros(5)
a3 =  np.empty(5)
a4 = np.empty([3,2], dtype = int)                                               # 3行2列的空数组 整形数据
a5 = np.array([[1,  2],  [3,  4],  [5,  6]]) 
a6 =  np.arange(1,10,2,int)
a7 =  np.arange(1,10,2,float)
a8 =  np.arange(10)
a9 =  np.arange(32).reshape((8,4))                                               # 8行4列的数组
a9 =  np.linspace(start=1,stop=10,num=9,endpoint=True,retstep=True)              # 等差数列 endpoint=true数列中包含stop值
a10 =  np.logspace(1,10,base =3,num =10)                                          # 等比数列  对数 log 的底数
arr = [1,3,5,7]
a11 =  np.array(arr)                                                              #列表转数组 
#-------------------------------------------------------------------------------------------------------------------------
#NumPy 切片和索引
a =  np.arange(10)
b= a[2:7:2]                                                                        #整数切片 由索引2-索引7 步长为2                                                               
c =a[5:]
a = np.array([[1,2,3],[3,4,5],[4,5,6]])
b = a[1:2]                                                                         # 可以获得行元素b = array([[3, 4, 5]])
a = np.array([[1,2,3],[3,4,5],[4,5,6],[7,8,9]])
b = a[3,...]                                                                       # 选取第3行的元素 b = array([7, 8, 9])
a = np.array([[1,2,3],[3,4,5],[4,5,6],[7,8,9]])
n = a[...,2]                                                                       #选取第3列的元素 array([3, 5, 6, 9])
x = np.array([[1,  2],  [3,  4],  [5,  6]]) 
y = x[[0,1,2],[0,1,0]]                                                             # y=[1  4  5] 分别按照行索引[0,1,2]和列索引[0,1,0]对应为(0,0)(1,1)(2,0)索引选择元素
x = np.array([[  0,  1,  2],[  3,  4,  5],[  6,  7,  8],[  9,  10,  11]])  
x[x>=5]                                                                           # 返回数组  array([ 5,  6,  7,  8,  9, 10, 11])
a = np.array([np.nan,  1,2,np.nan,3,4,5])  
a[~np.isnan(a)]                                                                    # 通过取补运算返回[ 1.2.3.4.5.]
x=np.arange(32).reshape((8,4))                                                   
x[[4,2,1,7]]                                                                       # 花式索引 选邓第4行,第2行,第1行及第7行的元素形成新数组

#-------------------------------------------------------------------------------------------------------------------------
#NumPy 迭代数组
a = np.arange(6).reshape(2,3)
for x in np.nditer(a):
    print (x)                                                                     # 返回 1,2,3,4,5,6  按行的方式进行迭代
for x in np.nditer(a, order='F'):
    print (x)                                                                     # 返回0,3,1,4,2,5按列方式进行迭代
for x in np.nditer(a.T,order='C'):                         
     print (x)                                                                    # 返回0,3,1,4,2,5按列方式进行迭代  a.T 产倒置
for x in np.nditer(a, op_flags=['readwrite']):                                    #迭代过程中允许数组修改
    x[...]=2*x 
    
a = np.arange(9).reshape(3,3)                     
for x in a.flat:                                                                 
    print(x)
    
a = np.arange(8).reshape(2,4)
print (a.flatten(order = 'F'))                                                    #返回一组拷贝结果[0 1 2 3 4 5 6 7]order:'C'-按行,'F'-按列,'A'-原顺序,
   
#------------------------------------------------------------------------------------------------------------------------
#修改数组形状
a = np.arange(8)
b = a.reshape(4,2)
#翻转数组
a = np.arange(12).reshape(3,4)
print (a.T)


版权声明:本文为weixin_51303362原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。