1.条件和布尔数组
1)生成随机数组,并将随机数组中大于0.5的替换成true其余替换成false
2)生成随机数组,并找出其中大于0.5的元素组成一个新数组
>>> import numpy as np
>>> a=np.random.random((4,4))
>>> a
array([[ 0.61604941, 0.51504974, 0.03828809, 0.0370244 ],
[ 0.20851217, 0.41995464, 0.07776302, 0.80532054],
[ 0.6124996 , 0.20123953, 0.29438641, 0.06040345],
[ 0.12745757, 0.20475085, 0.99330538, 0.48598423]])
>>> a>0.5
array([[ True, True, False, False],
[False, False, False, True],
[ True, False, False, False],
[False, False, True, False]], dtype=bool)
>>> a[a>0.5]
array([ 0.61604941, 0.51504974, 0.80532054, 0.6124996 , 0.99330538])
2.reshape和shape,reshape是对象的函数,shape是对象的属性
>>> import numpy as np
>>> a=np.random.random(12)
>>> a #a是一个一维数组
array([ 0.49758835, 0.46985048, 0.11273019, 0.70660268, 0.47732115,
0.78287341, 0.82160077, 0.14400892, 0.76857194, 0.0131592 ,
0.0115676 , 0.33726287])
>>> b=np.random.random(12) #b是一个一维数组
>>> c=b.reshape(3,4) #利用reshape方法,将b的元素组合成3x4的矩阵,赋值给c生成一个新矩阵
>>> c
array([[ 0.79760159, 0.08712535, 0.55475014, 0.77885859],
[ 0.37734364, 0.15936913, 0.8979942 , 0.14335809],
[ 0.77517327, 0.39024958, 0.28439001, 0.02566306]])
>>> a.shape=(3,4) #a的shape属性,改变成3,4 于是将a直接变动为3x4的矩阵,没有生成新的对象
>>> a
array([[ 0.49758835, 0.46985048, 0.11273019, 0.70660268],
[ 0.47732115, 0.78287341, 0.82160077, 0.14400892],
[ 0.76857194, 0.0131592 , 0.0115676 , 0.33726287]])
>>> b
array([ 0.79760159, 0.08712535, 0.55475014, 0.77885859, 0.37734364,
0.15936913, 0.8979942 , 0.14335809, 0.77517327, 0.39024958,
0.28439001, 0.02566306])
>>> c
array([[ 0.79760159, 0.08712535, 0.55475014, 0.77885859],
[ 0.37734364, 0.15936913, 0.8979942 , 0.14335809],
[ 0.77517327, 0.39024958, 0.28439001, 0.02566306]])
>>> a.ravel(12) #通过shape属性改变从而改变原a对象的形状,可以利用ravel进行恢复
array([ 0.49758835, 0.47732115, 0.76857194, 0.46985048, 0.78287341,
0.0131592 , 0.11273019, 0.82160077, 0.0115676 , 0.70660268,
0.14400892, 0.33726287])
>>> a
array([[ 0.49758835, 0.46985048, 0.11273019, 0.70660268],
[ 0.47732115, 0.78287341, 0.82160077, 0.14400892],
[ 0.76857194, 0.0131592 , 0.0115676 , 0.33726287]])
3.交换行列的方法transpose(),交换后生成新对象
>>> import numpy as np
>>> a=np.arange(0,9)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8])
>>> b=a.reshape(3,3)
>>> b
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8])
>>> a.shape=(3,3)
>>> a
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> a.transpose()
array([[0, 3, 6],
[1, 4, 7],
[2, 5, 8]])
>>> a
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> c=a.transpose()
>>> c
array([[0, 3, 6],
[1, 4, 7],
[2, 5, 8]])
4.数组操作
1)链接数组
将多个数组连接成一个新数组
vstack()函数执行垂直连接,将两个数组上下组合,把第二个数组作为行添加到第一个数组底下,整个新的大数组朝垂直方向发展
hstack()函数执行水平链接,将两个数组左右组合,把第二个数组作为列添加到第一个数组右边,整个新的大数组朝水平方向发展
>>> import numpy as np
>>> a=np.array([1,2,3,4,5,6,7,8,9])
>>> a
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> a.reshape(3,3)
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
>>> a
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> a.shape=(3,3)
>>> a
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
>>> b=np.arange(10,19).reshape(3,3)
>>> b
array([[10, 11, 12],
[13, 14, 15],
[16, 17, 18]])
>>> c=np.vstack((a,b))
>>> c
array([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12],
[13, 14, 15],
[16, 17, 18]])
>>> d=np.hstack((a,b))
>>> d
array([[ 1, 2, 3, 10, 11, 12],
[ 4, 5, 6, 13, 14, 15],
[ 7, 8, 9, 16, 17, 18]])
2)数组拆分
将一个数组拆分成多个新数组(平均拆分,每一部分的行列数都相等)
hsplit()函数将对大数组进行水平拆分,会横向砍几刀
vsplit()函数将对大数组进行纵向拆分,会纵向砍几刀
>>> import numpy as np
>>> a=np.arange(16)
>>> a
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
>>> a.shape=(4,4)
>>> a
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
>>> [b,c]=np.hsplit(a,2)
>>> b
array([[ 0, 1],
[ 4, 5],
[ 8, 9],
[12, 13]])
>>> c
array([[ 2, 3],
[ 6, 7],
[10, 11],
[14, 15]])
>>> [d,e]=np.vsplit(a,2)
>>> d
array([[0, 1, 2, 3],
[4, 5, 6, 7]])
>>> e
array([[ 8, 9, 10, 11],
[12, 13, 14, 15]])
3)一堆一位数组和合并
>>> import numpy as np
>>> a=np.ones(3)
>>> a
array([ 1., 1., 1.])
>>> b=np.zeros(3)
>>> b
array([ 0., 0., 0.])
>>> c=np.random.random(3)
>>> c
array([ 0.53712859, 0.00297176, 0.85618323])
>>> d=np.column_stack((a,b,c))
>>> d
array([[ 1. , 0. , 0.53712859],
[ 1. , 0. , 0.00297176],
[ 1. , 0. , 0.85618323]])
>>> e=np.row_stack((a,b,c))
>>> e
array([[ 1. , 1. , 1. ],
[ 0. , 0. , 0. ],
[ 0.53712859, 0.00297176, 0.85618323]])
4)利用split函数进行不对称拆分:不平均拆分
函数参数有
第一个参数:被拆矩阵对象名
第二个对象:指定被切部分索引
第三个对象:axis=1代表按照列进行拆封就是纵向砍,axis=0代表行进行拆封就是横向砍
>>> import numpy as np
>>> a=np.arange(21)
>>> a
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20])
>>> a.shape=(3,7)
>>> a
array([[ 0, 1, 2, 3, 4, 5, 6],
[ 7, 8, 9, 10, 11, 12, 13],
[14, 15, 16, 17, 18, 19, 20]])
>>> [a1,a2,a3,a4]=np.split(a,[1,3,5],axis=1)
>>> a1
array([[ 0],
[ 7],
[14]])
>>> a2
array([[ 1, 2],
[ 8, 9],
[15, 16]])
>>> a3
array([[ 3, 4],
[10, 11],
[17, 18]])
>>> a4
array([[ 5, 6],
[12, 13],
[19, 20]])
>>> b=np.arange(24)
>>> b
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23])
>>> b.shape=(8,3)
>>> b
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17],
[18, 19, 20],
[21, 22, 23]])
>>> [b1,b2,b3,b4,b5]=np.split(b,[1,4,5,7],axis=0)
>>> b1
array([[0, 1, 2]])
>>> b2
array([[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
>>> b3
array([[12, 13, 14]])
>>> b4
array([[15, 16, 17],
[18, 19, 20]])
>>> b5
array([[21, 22, 23]])
>>>
5.数组对象的副本和视图
副本就是一个被复制出的数组,原数组的变动不会影响副本
视图不是一个被复制的,只是对原数组进行一个引用,所以视图的变化会影响原数组
1)赋值产生的视图案例
>>> import numpy as np
>>> a=np.array([1,2,3,4])
>>> b=a
>>> a
array([1, 2, 3, 4])
>>> b
array([1, 2, 3, 4])
>>> a[2]
3
>>> b[2]
3
>>> a[2]=6
>>> b
array([1, 2, 6, 4])
>>> a
array([1, 2, 6, 4]) #由于b是a的视图说以改变a会改变b
2)切片产生的视图案例
>>> import numpy as np
>>> a=np.array([1,2,3,4,5])
>>> a
array([1, 2, 3, 4, 5])
>>> b=a[0:2]
>>> b
array([1, 2])
>>> a[0]=9
>>> a
array([9, 2, 3, 4, 5])
>>> b
array([9, 2])
3)利用copy函数产生副本
>>> import numpy as np
>>> a=np.array([1,2,3,4,5])
>>> a
array([1, 2, 3, 4, 5])
>>> b=a.copy()
>>> b
array([1, 2, 3, 4, 5])
>>> a[4]=9
>>> a
array([1, 2, 3, 4, 9])
>>> b
array([1, 2, 3, 4, 5])
网友评论