numpy day02

导入numpy并查看版本

In [2]:
 
 
 
 
 
import numpy as np 
np.__version__
Out[2]:
'1.13.1'
 

什么是numpy?

Numpy即Numeric Python,python经过扩展可以支持数组和矩阵类型,并且具有大量的函数可以计算这些数组和矩阵。这些数组一般是多维的,而这个扩展的程序包就是numpy。

【注】是数据分析和机器学习中最基本的工具,后面许多API和工具都是建立在他得基础上,如:pandas、scipy、matplotlib等

 

一、创建ndarray

numpy中最基础数据结构就是ndarray:即数组

 

1. 使用np.array()由python list创建

In [3]:
 
 
 
 
 
data = [1,2,3]
nd = np.array(data)
nd
 
 
Out[3]:
array([1, 2, 3])
In [3]:
 
 
 
 
 
data
 
 
Out[3]:
[1, 2, 3]
In [4]:
 
 
 
 
 
type(nd),type(data)
 
 
Out[4]:
(numpy.ndarray, list)
In [5]:
 
 
 
 
 
nd.dtype # 数组中的元素的类型
 
. . .
In [8]:
 
 
 
 
 
nd2 = np.array([1,2,3,4.5,True,"1234"])
nd2
 
. . .
In [7]:
 
 
 
 
 
nd2.dtype
 
. . .
 

注意:1、数组中所有的元素类型相同 2、如果通过列表来创建的时候,列表中元素不一样,会被统一成某种类型(优先级:str>float>int)

In [9]:
 
 
 
 
 
# 注意:图片也可在numpy里面用数组来表示
# 引入一张图片
import matplotlib.pyplot as plt # 这个是用于处理数据可视化的一个工具
 
 
In [165]:
 
 
 
 
 
girl = plt.imread("./source/girl.jpg")
girl # 彩色图片是一个三维的数组
 
. . .
In [11]:
 
 
 
 
 
girl.shape # 数据的形状
 
 
Out[11]:
(900, 1440, 3)
In [13]:
 
 
 
 
 
plt.imshow(girl)
plt.show()
 
. . .
In [14]:
 
 
 
 
 
boy = np.array([[0.21,0.14,0.12343,0.14],
                [0.21,0.14,0.12343,0.14],
                [0.21,0.14,0.12343,0.14],
               ])
 
 
In [17]:
 
 
 
 
 
plt.imshow(boy,cmap="gray")
plt.show()
 
. . .
In [19]:
 
 
 
 
 
g = girl[:200,:300]
 
 
In [20]:
 
 
 
 
 
plt.imshow(g)
plt.show()
 
 
 
In [ ]:
 
 
 
 
 
 
 
 

2. 使用np的routines函数创建

 

1)np.ones(shape,dtype=None,order='C')

In [26]:
 
 
 
 
 
# 参数shape数据的形状传一个元组,元组的第0个元素代表第0维中的元素个数,依次类推
np.ones((3,2,4,5,6,7,8))
 
. . .
In [28]:
 
 
 
 
 
ones
 
. . .
In [27]:
 
 
 
 
 
ones = np.ones((168,59,3))
plt.imshow(ones)
plt.show()
 
. . .
In [29]:
 
 
 
 
 
ones[:,:,1:] = 0 # 数组是可以切片赋值的
 
 
In [34]:
 
 
 
 
 
ones[:,:,0] = 0.3
 
 
In [35]:
 
 
 
 
 
ones
 
. . .
In [36]:
 
 
 
plt.imshow(ones)
plt.show()
 
. . .
In [ ]:
 
 
 
 
 
 
 

2)np.zeros(shape,dtype="float",order="C")

In [40]:
 
 
 
np.zeros((2,3),dtype="int32")
 
 
Out[40]:
array([[0, 0, 0],
       [0, 0, 0]])
 

3)np.full(shape,fill_value,dtype=None)

In [43]:
 
 
np.full((3,4),12,dtype="float")
 
 
Out[43]:
array([[ 12.,  12.,  12.,  12.],
       [ 12.,  12.,  12.,  12.],
       [ 12.,  12.,  12.,  12.]])
 

4)np.eye(N,M,k=0,dtype='float')

In [44]:
 
 
np.eye(4)
 
 
Out[44]:
array([[ 1.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0.],
       [ 0.,  0.,  1.,  0.],
       [ 0.,  0.,  0.,  1.]])
 

5)np.linspace(start,stop,num=50)

扫描二维码关注公众号,回复: 6981404 查看本文章
In [49]:
 
np.linspace(0,10,10)
 
Out[49]:
array([  0.        ,   1.11111111,   2.22222222,   3.33333333,
         4.44444444,   5.55555556,   6.66666667,   7.77777778,
         8.88888889,  10.        ])
In [50]:
 
 
 
 
 
np.logspace(1,100,10)
 
 
Out[50]:
array([  1.00000000e+001,   1.00000000e+012,   1.00000000e+023,
         1.00000000e+034,   1.00000000e+045,   1.00000000e+056,
         1.00000000e+067,   1.00000000e+078,   1.00000000e+089,
         1.00000000e+100])
In [ ]:
 
 
 
 
 
 
 
 

6)np.arange([start,]stop,[step,]dtype=None) "[]"中是可选项

In [54]:
 
 
 
 
 
np.arange(0,100,5)
 
 
Out[54]:
array([ 0,  5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80,
       85, 90, 95])
 

7)np.random.randint(low,high=None,size=None,dtype='I')

In [67]:
 
 
 
 
 
np.random.randint(0,10,size=(10,5)) # low下限 high上限 size形状(元组)
 
. . .
 

8)np.random.randn(d0,d1,...,dn) 从第一维度到第n维度生成一个数组,数组中的数字符合标准正态分布

In [70]:
 
 
 
 
 
np.random.randn(5,6,10) # N(0,1)
 
. . .
 

9)np.random.normal(loc=0.0,scale=1.0,size=None)

In [73]:
 
 
 
 
 
np.random.normal(450,100,size=(90,10))
 
. . .
 

10)np.random.random(size=None)

In [74]:
 
 
 
 
 
np.random.random(size=10) # 生成0-1之间的浮点数
 
. . .
 

用随机数生成图片

In [78]:
 
 
 
 
 
boy = np.random.random((667,480,3))
plt.imshow(boy,cmap="gray")
plt.show()
 
. . .
 

二、ndarray的属性

 

数组的常用属性:

维度 ndim, 大小 size, 形状 shape, 元素类型 dtype, 每项大小 itemsize, 数据 data

In [79]:
 
 
 
 
 
tigger = plt.imread("./source/tigger.jpg")
tigger
 
. . .
In [80]:
 
 
 
 
 
tigger.ndim
 
 
Out[80]:
3
In [81]:
 
 
 
 
 
girl.ndim
 
 
Out[81]:
3
In [84]:
 
 
 
 
 
nd
 
 
Out[84]:
array([1, 2, 3])
In [83]:
 
 
 
 
 
nd.size
 
 
Out[83]:
3
In [85]:
 
 
 
 
 
tigger.shape
 
 
Out[85]:
(786, 1200, 3)
In [88]:
 
 
 
 
 
nd.dtype
 
. . .
In [89]:
 
 
 
 
 
tigger.dtype
 
 
Out[89]:
dtype('uint8')
In [86]:
 
 
 
 
 
nd.itemsize
 
 
Out[86]:
4
In [92]:
 
 
 
 
 
tigger.itemsize
 
 
Out[92]:
1
In [94]:
 
 
 
 
 
nd.data
 
 
Out[94]:
<memory at 0x000001C74AA22408>
In [93]:
 
 
 
 
 
tigger.data
 
. . .
 

三、ndarray的基本操作

 

1、索引

In [96]:
 
 
 
 
 
l = [1,3,4,6,7,8,0]
l[-1]
 
 
Out[96]:
0
In [98]:
 
 
 
 
 
nd = np.random.randint(0,4,size=4)
nd
 
 
Out[98]:
array([1, 2, 3, 1])
In [101]:
 
 
 
 
 
nd[0]
 
 
Out[101]:
1
In [104]:
 
 
 
 
 
lp = [[1,2,4],
    [4,5,6]
    ]
lp
 
 
Out[104]:
[[1, 2, 4], [4, 5, 6]]
In [107]:
 
 
 
 
 
lp[0][1]
 
 
Out[107]:
2
In [108]:
 
 
 
 
 
lp[0,1]
 
. . .
In [ ]:
 
 
 
 
 
 
 
In [109]:
 
 
 
 
 
nd = np.random.randint(0,10,size=(6,6))
nd
 
. . .
In [110]:
 
 
 
 
 
nd[0]
 
 
Out[110]:
array([2, 7, 2, 9, 3, 3])
In [111]:
 
 
 
 
 
nd[0][1]
 
 
Out[111]:
7
In [112]:
 
 
 
 
 
nd[0,1] # 先访问第0个维度的第0个,再访问第1个维度中第1
 
 
Out[112]:
7
In [113]:
 
 
 
 
 
nd[1,2,3] # 维度不足3个,不这样访问
 
. . .
In [114]:
 
 
 
 
 
nd[1,4] = 20000
nd
 
. . .
In [116]:
 
 
 
 
 
nd
 
. . .
In [118]:
 
 
 
 
 
nd[[1,1,1,1,1]]
nd[[1,4,5,2,0]] # 连续访问多个,可以把你要访问的那些下标写成一个列表
 
. . .
 

2、切片

In [120]:
 
 
 
 
 
nd = np.random.randint(0,10,size=(4,4))
nd
 
 
Out[120]:
array([[7, 5, 7, 2],
       [7, 5, 9, 6],
       [5, 9, 9, 4],
       [2, 1, 8, 2]])
In [126]:
 
 
 
 
 
nd[0:5] # 区间左闭右开
 
 
Out[126]:
array([[7, 5, 7, 2],
       [7, 5, 9, 6],
       [5, 9, 9, 4],
       [2, 1, 8, 2]])
In [129]:
 
 
 
 
 
l[0:10]
 
 
Out[129]:
[1, 3, 4, 6, 7, 8, 0]
In [130]:
 
 
 
 
 
nd[1:]
 
 
Out[130]:
array([[7, 5, 9, 6],
       [5, 9, 9, 4],
       [2, 1, 8, 2]])
In [134]:
 
 
 
 
 
nd[::-1]
 
 
Out[134]:
array([[2, 1, 8, 2],
       [5, 9, 9, 4],
       [7, 5, 9, 6],
       [7, 5, 7, 2]])
In [135]:
 
 
 
 
 
nd
 
 
Out[135]:
array([[7, 5, 7, 2],
       [7, 5, 9, 6],
       [5, 9, 9, 4],
       [2, 1, 8, 2]])
In [138]:
 
 
 
 
 
nd[:,0:2]
 
 
Out[138]:
array([[7, 5],
       [7, 5],
       [5, 9],
       [2, 1]])
 

把girl调头

In [139]:
 
 
 
 
 
girl
 
. . .
In [145]:
 
 
 
 
 
girl2 = girl[::-3]
 
 
In [146]:
 
 
 
 
 
plt.imshow(girl2)
plt.show()
 
 
 
In [166]:
 
 
 
 
 
girl3 = girl[::2,::-2]
plt.imshow(girl3)
plt.show()
 
 
 
In [167]:
 
 
 
 
 
girl4 = girl[:,:,::1]
girl4[:,:,:2] = 0
plt.imshow(girl4)
plt.show()
 
. . .
In [ ]:
 
 
 
 
 
 
 
 

3、变形

 

reshape()

resize()

In [170]:
 
 
 
 
 
tigger
 
. . .
In [171]:
 
 
 
 
 
tigger.shape
 
. . .
In [172]:
 
 
 
 
 
nd = np.random.randint(0,20,size=12)
nd (1*12)
 
. . .
In [173]:
 
 
 
 
 
nd.reshape((3,4))
 
. . .
In [174]:
 
 
 
 
 
nd
 
 
Out[174]:
array([ 2,  1,  0, 17, 18, 13, 11,  7,  8, 19,  2, 16])
In [175]:
 
 
 
 
 
nd.reshape((4,4)) # 元素总数不一致,是不能相互转变的
 
. . .
In [176]:
 
 
 
 
 
nd.reshape((1,6))
 
. . .
In [178]:
 
 
 
 
 
nd.reshape((1,1,2,6))
 
 
Out[178]:
array([[[[ 2,  1,  0, 17, 18, 13],
         [11,  7,  8, 19,  2, 16]]]])
In [179]:
 
 
 
 
 
nd.resize((3,4))
 
 
In [180]:
 
 
 
 
 
nd
 
 
Out[180]:
array([[ 2,  1,  0, 17],
       [18, 13, 11,  7],
       [ 8, 19,  2, 16]])
 

【注意】

1、resize()和reshape()都是对数组进行变形,resize()是在原来数组上直接变形,reshape()生成一个新的数组

2、变形的时候,新数组的总元素个数要和原来的保持一致

 

拼图游戏

In [181]:
 
 
 
 
 
girl = plt.imread("./source/girl2.jpg")
plt.imshow(girl)
plt.show()
 
. . .
In [186]:
 
 
 
 
 
tigger[150:450,200:500] = girl
plt.imshow(tigger)
plt.show()
 
. . .
In [ ]:
 
 
 
 
 
 
 
 

4、级联

In [4]:
 
 
 
 
 
nd1 = np.random.randint(0,100,size=(4,4))
nd2 = np.random.randint(0,100,size=(4,3))
print(nd1)
print(nd2)
 
 
 
[[86 87 27  4]
 [30 93 58 74]
 [37 83 74 48]
 [ 9  4 81 19]]
[[10 36 77]
 [41  5 32]
 [38 30  6]
 [17 20 39]]
 

级联:就是按照指定的维度把两个数组链接在一起

 

级联注意点:

1、参与级联的数组要构成一个列表(或元组)

2、维度必须一样

3、形状必须相符(如:二维数组axis=0时,列数必须一样,axis=1的时候行数必须一样)

4、级联的默认方向shape的第一个值所代表的那个维度(axis默认为0)

In [8]:
 
 
 
 
 
np.concatenate([nd1,nd2],axis=1) 
# 参数一,参与级联的那些数组;参数二,级联的维度
 
. . .
In [13]:
 
 
 
 
 
nd3 = np.random.randint(0,100,size=(6,4))
nd3
 
 
Out[13]:
array([[55, 91,  5, 82],
       [95,  0, 17, 78],
       [91, 89, 27, 35],
       [37, 82, 78, 61],
       [ 6, 58, 14, 62],
       [21, 62, 45, 35]])
In [14]:
 
 
 
 
 
np.concatenate([nd1,nd3])
 
 
Out[14]:
array([[86, 87, 27,  4],
       [30, 93, 58, 74],
       [37, 83, 74, 48],
       [ 9,  4, 81, 19],
       [55, 91,  5, 82],
       [95,  0, 17, 78],
       [91, 89, 27, 35],
       [37, 82, 78, 61],
       [ 6, 58, 14, 62],
       [21, 62, 45, 35]])
 

hstack()和vstack()

 

hstack()把列数组改成一个行数组(把所有行上数组拼接成一个一行的数组)

vstack()把一个单行的数组拆成多行(每个行上只有一个数字)

In [16]:
 
 
 
 
 
nd3 = np.random.randint(0,10,size=(10,1))
nd3
 
. . .
In [18]:
 
 
 
 
 
np.hstack(nd3)
 
 
Out[18]:
array([0, 1, 6, 2, 1, 8, 1, 0, 4, 6])
In [19]:
 
 
 
 
 
nd4 = np.random.randint(0,50,size=(4,3))
nd4
 
 
Out[19]:
array([[16, 41, 36],
       [45, 18,  3],
       [37,  9,  0],
       [43, 18, 46]])
In [20]:
 
 
 
 
 
np.hstack(nd4)
 
 
Out[20]:
array([16, 41, 36, 45, 18,  3, 37,  9,  0, 43, 18, 46])
In [21]:
 
 
 
 
 
np.vstack(nd4)
 
 
Out[21]:
array([[16, 41, 36],
       [45, 18,  3],
       [37,  9,  0],
       [43, 18, 46]])
In [22]:
 
 
 
 
 
nd5 = np.random.randint(0,10,size=10)
nd5
 
 
Out[22]:
array([2, 6, 3, 0, 2, 0, 3, 0, 7, 1])
In [23]:
 
 
 
 
 
np.vstack(nd5)
 
. . .
 

5、切分

 

split()

vsplit()

hsplit()

In [28]:
 
 
 
 
 
nd = np.random.randint(0,100,size=(5,6))
nd
 
. . .
In [27]:
 
 
 
 
 
np.hsplit(nd,[1,3]) # 代表在水平方向切分
# 参数一,被切的数组  参数二,切分点(列表,在切分点前切分)
 
. . .
In [29]:
 
 
 
 
 
np.vsplit(nd,[1,4]) # 纵向上切分 参数和横向类似
 
. . .
In [30]:
 
 
 
 
 
np.split(nd,[1,3],axis=0) # 切行
 
. . .
In [32]:
 
 
 
 
 
np.split(nd,(1,3),axis=1) # 切列
 
. . .
 

6、副本

In [33]:
 
 
 
 
 
nd = np.random.randint(0,100,size=6)
nd
 
 
Out[33]:
array([13, 89, 33, 62, 74, 55])
In [34]:
 
 
 
 
 
nd1 = nd 
 
 
In [36]:
 
 
 
 
 
nd1[0] = 1000
nd1
 
 
Out[36]:
array([1000,   89,   33,   62,   74,   55])
In [38]:
 
 
 
 
 
nd # 此时nd1和nd变量引用是同一个数组对象(nd和nd1中存储的地址都是一样的)
# 在某种意义上这也是一种浅拷贝
 
 
Out[38]:
array([1000,   89,   33,   62,   74,   55])
 

用copy()函数对数组对象创建副本

In [40]:
 
 
 
 
 
nd2 = nd.copy()
nd2
 
 
Out[40]:
array([1000,   89,   33,   62,   74,   55])
In [41]:
 
 
 
 
 
nd2[2] = 2000
nd2
 
 
Out[41]:
array([1000,   89, 2000,   62,   74,   55])
In [42]:
 
 
 
 
 
nd
 
 
Out[42]:
array([1000,   89,   33,   62,   74,   55])
In [43]:
 
 
 
 
 
nd1
 
 
Out[43]:
array([1000,   89,   33,   62,   74,   55])
 

用列表创建数组的过程有木有创建副本

In [44]:
 
 
 
 
 
l = [1,2,3,4]
nd = np.array(l) # 把l拷贝了一份放在了数组nd中
 
 
In [45]:
 
 
 
 
 
nd[0] = 100
nd
 
 
Out[45]:
array([100,   2,   3,   4])
In [46]:
 
 
 
 
 
l
 
 
Out[46]:
[1, 2, 3, 4]
In [ ]:
 
 
 
 
 
 
 
 

四、ndarray的聚合操作

 

1、求和

In [55]:
 
 
 
 
 
nd = np.random.randint(0,10,size=(3,4))
nd
 
 
Out[55]:
array([[2, 8, 0, 9],
       [8, 2, 4, 6],
       [4, 0, 3, 6]])
In [56]:
 
 
 
 
 
np.sum(nd,axis=0) # 把行按照列标对应加起来
 
 
Out[56]:
array([14, 10,  7, 21])
In [57]:
 
 
 
 
 
np.sum(nd,axis=1) # 把列按照对应行标加起来
 
 
Out[57]:
array([19, 20, 13])
In [59]:
 
 
 
 
 
np.sum(nd) # 把所有的元素加起来
 
. . .
In [60]:
 
 
 
 
 
nd.sum()
 
. . .
 

2、最值

In [69]:
 
 
 
 
 
nd
 
 
Out[69]:
array([[2, 8, 0, 9],
       [8, 2, 4, 6],
       [4, 0, 3, 6]])
In [63]:
 
 
 
 
 
np.max(nd)
 
 
Out[63]:
9
In [65]:
 
 
 
 
 
np.max(nd,axis=0) # 求每个列里面最大值
 
 
Out[65]:
array([8, 8, 4, 9])
In [66]:
 
 
 
 
 
np.max(nd,axis=1) # 求每一行里面的最大值
 
 
Out[66]:
array([9, 8, 6])
In [67]:
 
 
 
 
 
np.argmax(nd)
 
 
Out[67]:
3
In [68]:
 
 
 
 
 
np.argmin(nd)
 
 
Out[68]:
2
In [71]:
 
 
 
 
 
np.argmax(nd,axis=0) # 在列上求最大值并返回最大值的行标
 
 
Out[71]:
array([1, 0, 1, 0], dtype=int64)
In [72]:
 
 
 
 
 
np.argmin(nd,axis=1) # 在每个行上求最小值返回列标
 
 
Out[72]:
array([2, 1, 1], dtype=int64)
 

3、其他聚合操作

 
Function Name    NaN-safe Version    Description
np.sum    np.nansum    Compute sum of elements
np.prod    np.nanprod    Compute product of elements
np.mean    np.nanmean    Compute mean of elements
np.std    np.nanstd    Compute standard deviation
np.var    np.nanvar    Compute variance
np.min    np.nanmin    Find minimum value
np.max    np.nanmax    Find maximum value
np.argmin    np.nanargmin    Find index of minimum value
np.argmax    np.nanargmax    Find index of maximum value
np.median    np.nanmedian    Compute median of elements
np.percentile    np.nanpercentile    Compute rank-based statistics of elements
np.any    N/A    Evaluate whether any elements are true
np.all    N/A    Evaluate whether all elements are true
np.power 幂运算
In [73]:
 
 
 
 
 
nd = np.array([1,2,3,np.nan])
nd
 
 
Out[73]:
array([  1.,   2.,   3.,  nan])
In [76]:
 
 
 
 
 
np.nan + 100
np.nan*100
# nan表示缺失(默认是浮点型),缺失和任何数做运算结果都是缺失
 
 
Out[76]:
nan
In [74]:
 
 
 
 
 
nd.sum()
 
 
Out[74]:
nan
In [77]:
 
 
 
 
 
np.nansum(nd) # 以nan开头的聚合方法,可在聚合运算的直接把nan剔除
 
 
Out[77]:
6.0
 
思考题:给定一个4维矩阵,如何得到最后两维的和?
In [78]:
 
 
 
 
 
nd = np.random.randint(0,10,size=(2,3,2,2))
nd
 
 
Out[78]:
array([[[[4, 9],
         [5, 1]],

        [[2, 1],
         [7, 5]],

        [[2, 7],
         [4, 9]]],


       [[[8, 9],
         [6, 4]],

        [[7, 3],
         [5, 4]],

        [[5, 4],
         [7, 6]]]])
In [81]:
 
 
 
 
 
nd.sum(axis=-1)
nd.sum(axis=-2)
nd.sum(axis=(-1,-2))
 
 
Out[81]:
array([[19, 15, 22],
       [27, 19, 22]])
 
思考题:如何根据第3列来对一个5*5矩阵排序?
In [84]:
 
 
 
 
 
nd = np.random.randint(0,20,size=(5,5))
nd
 
 
Out[84]:
array([[ 5, 19,  1,  4, 12],
       [13, 16, 17, 18, 17],
       [19, 12,  4, 19, 10],
       [16, 11, 18,  3, 14],
       [14,  0, 15, 15, 10]])
In [85]:
 
 
 
 
 
nd[[3,0,4,1,2]] #??
 
 
Out[85]:
array([[16, 11, 18,  3, 14],
       [ 5, 19,  1,  4, 12],
       [14,  0, 15, 15, 10],
       [13, 16, 17, 18, 17],
       [19, 12,  4, 19, 10]])
In [92]:
 
 
 
 
 
nd
 
 
Out[92]:
array([[ 1,  4,  5, 12, 19],
       [13, 16, 17, 17, 18],
       [ 4, 10, 12, 19, 19],
       [ 3, 11, 14, 16, 18],
       [ 0, 10, 14, 15, 15]])
In [94]:
 
 
 
 
 
sort_ind = np.argsort(nd[:,3]) # 排序并且返回排序以后的下标序列
sort_ind
 
 
Out[94]:
array([0, 4, 3, 1, 2], dtype=int64)
In [95]:
 
 
 
 
 
nd[sort_ind]
 
. . .
 

思路

In [ ]:
 
 
 
 
 
 
 
 

五、ndarray的矩阵操作

 

1. 基本矩阵操作

 
1)算术运算(即加减乘除)
In [96]:
 
 
 
 
 
nd = np.random.randint(0,10,size=(5,5))
nd
 
 
Out[96]:
array([[9, 0, 4, 2, 7],
       [7, 3, 9, 6, 6],
       [5, 9, 6, 3, 0],
       [4, 5, 5, 8, 3],
       [2, 5, 3, 3, 3]])
In [97]:
 
 
 
 
 
nd + 3  # 加运算,启动广播机制把缺失的地方补全
 
 
Out[97]:
array([[12,  3,  7,  5, 10],
       [10,  6, 12,  9,  9],
       [ 8, 12,  9,  6,  3],
       [ 7,  8,  8, 11,  6],
       [ 5,  8,  6,  6,  6]])
In [98]:
 
 
 
 
 
nd*2 # 乘法不需要广播机制
 
 
Out[98]:
array([[18,  0,  8,  4, 14],
       [14,  6, 18, 12, 12],
       [10, 18, 12,  6,  0],
       [ 8, 10, 10, 16,  6],
       [ 4, 10,  6,  6,  6]])
In [99]:
 
 
 
 
 
nd/2
 
 
Out[99]:
array([[ 4.5,  0. ,  2. ,  1. ,  3.5],
       [ 3.5,  1.5,  4.5,  3. ,  3. ],
       [ 2.5,  4.5,  3. ,  1.5,  0. ],
       [ 2. ,  2.5,  2.5,  4. ,  1.5],
       [ 1. ,  2.5,  1.5,  1.5,  1.5]])
 
2)矩阵积
In [103]:
 
 
 
 
 
nd1 = np.random.randint(0,5,size=(4,3))
nd2 = np.random.randint(0,5,size=(3,4))
print(nd1)
print(nd2)
 
 
 
[[2 3 3]
 [4 1 1]
 [2 1 2]
 [4 1 0]]
[[4 4 3 4]
 [4 3 2 4]
 [1 4 4 4]]
In [104]:
 
 
 
 
 
np.dot(nd1,nd2)
 
 
Out[104]:
array([[23, 29, 24, 32],
       [21, 23, 18, 24],
       [14, 19, 16, 20],
       [20, 19, 14, 20]])
 

2. 广播机制

 

ndarray的广播机制的两条规则:

  • 1、为缺失维度补1
  • 2、假定缺失的元素用已有值填充
In [105]:
 
 
 
 
 
m = np.random.randint(0,10,size=(2,3))
m
 
 
Out[105]:
array([[3, 6, 9],
       [4, 7, 7]])
In [106]:
 
 
 
 
 
a = np.arange(3)
a
 
 
Out[106]:
array([0, 1, 2])
In [107]:
 
 
 
 
 
# 求m+a
m+a
 
 
Out[107]:
array([[ 3,  7, 11],
       [ 4,  8,  9]])
In [108]:
 
 
 
 
 
a = np.arange(3).reshape((3,1))
a
 
 
Out[108]:
array([[0],
       [1],
       [2]])
In [109]:
 
 
 
 
 
b = np.arange(3)
b
 
 
Out[109]:
array([0, 1, 2])
In [110]:
 
 
 
 
 
a+b
 
 
Out[110]:
array([[0, 1, 2],
       [1, 2, 3],
       [2, 3, 4]])
 

0 0 0 0 1 2

1 1 1 + 0 1 2

2 2 2 0 1 2

In [111]:
 
 
 
 
 
a + 3
 
 
Out[111]:
array([[3],
       [4],
       [5]])
In [112]:
 
 
 
 
 
a = np.random.randint(0,5,size=(3,3))
a
 
 
Out[112]:
array([[1, 0, 0],
       [2, 4, 2],
       [0, 3, 1]])
In [113]:
 
 
 
 
 
b = np.random.randint(0,5,size=(2,2))
b
 
 
Out[113]:
array([[4, 1],
       [2, 2]])
In [114]:
 
 
 
 
 
a + b # 如果行和列个数均不相同无法进行补全,不能相加
 
. . .
 

排序

In [120]:
 
 
 
 
 
nd = np.random.randint(0,100,size=(10))
nd
 
 
Out[120]:
array([70, 90, 72, 23, 55, 85, 24, 88, 91, 13])
In [119]:
 
 
 
 
 
nd.sort() # 改变原来的数组
 
 
In [118]:
 
 
 
 
 
nd
 
 
Out[118]:
array([13, 26, 35, 43, 46, 59, 76, 77, 81, 94])
In [121]:
 
 
 
 
 
np.sort(nd)# 不会改变原来的数组,而是生成一个新的数组
 
 
Out[121]:
array([13, 23, 24, 55, 70, 72, 85, 88, 90, 91])
In [122]:
 
 
 
 
 
nd
 
 
Out[122]:
array([70, 90, 72, 23, 55, 85, 24, 88, 91, 13])
In [123]:
 
 
 
 
 
nd1 = np.random.randint(0,100,size=(4,4))
nd1
 
 
Out[123]:
array([[95, 82, 52, 15],
       [70, 87,  2, 54],
       [52, 66, 26, 95],
       [ 9, 49, 63, 48]])
In [126]:
 
 
 
 
 
np.sort(nd1,axis=1) # axis=1排的是列标 axis=0排的是行标
 
 
Out[126]:
array([[15, 52, 82, 95],
       [ 2, 54, 70, 87],
       [26, 52, 66, 95],
       [ 9, 48, 49, 63]])
In [127]:
 
 
 
 
 
np.sort(nd1,axis=0)
 
 
Out[127]:
array([[ 9, 49,  2, 15],
       [52, 66, 26, 48],
       [70, 82, 52, 54],
       [95, 87, 63, 95]])
In [128]:
 
 
 
 
 
# argsort()排的是下标
np.argsort(nd1,axis=0)
 
 
Out[128]:
array([[3, 3, 1, 0],
       [2, 2, 2, 3],
       [1, 0, 0, 1],
       [0, 1, 3, 2]], dtype=int64)
In [ ]:
 
 
 
 
 

猜你喜欢

转载自www.cnblogs.com/soboy03/p/11321092.html