Python之Numpy

文章目录

tile

参考

numpy.tile(A, reps)

通过重复reps次A来构造出一个新数组。

>>> import numpy as np
>>> a = np.array([0, 1, 2])
>>> np.tile(a, 2)
array([0, 1, 2, 0, 1, 2])

如果reps不是一个数,存在长度 d = len(reps) 的话,那么函数返回的新数组的维数ndim就是 max(d, A.ndim)。

>>> a = np.array([0, 1, 2])
>>> np.tile(a, (2, 2))
# len(reps) = 2 , A.ndim = 1
array([[0, 1, 2, 0, 1, 2],
       [0, 1, 2, 0, 1, 2]])
>>> np.tile(a, (2, 1, 2))
># len(reps) = 3 , A.ndim = 1
array([[[0, 1, 2, 0, 1, 2]],
       [[0, 1, 2, 0, 1, 2]]])
>>> np.tile(a, (2,3,4))
># len(reps) = 3 , A.ndim = 1
array([[[0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2],
        [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2],
        [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2]],

       [[0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2],
        [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2],
        [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2]]])
shape与ndim

shape表示数组维度
ndim表示数组维数
shape是一个可写属性,作用同reshape

import numpy as np 
 
a = np.array([[1,2,3],[4,5,6]]) 
print (a)
a.shape =  (3,2)  
print (a)
b = a.reshape(3,2)  
print (b)
转置
x = np.array([1,2,3,4]) #对其转置没有效果
xx = np.array([[1,2,3,4]]) # 是个1*4的数组,可以实现转置
c = np.zeros((4,3))
c[:,0]=x-x.mean()
c[:,1]=xx-x.mean()
print(c)  # 说明对于一维数组,不过是行向量还是列向量,都可以直接对应加减
[[-1.5 -1.5  0. ]
 [-0.5 -0.5  0. ]
 [ 0.5  0.5  0. ]
 [ 1.5  1.5  0. ]]
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值