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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_51303362

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值