``python >>> a1 = np.array([1,2,3,4,5]) >>> a1 array([1, 2, 3, 4, 5]) >>> a1.shape (5,) # 括号内1个值表示一维数组
>>> a2 = np.array([[1,2,3,4],[5,6,7,8]]) >>> a2 array([[1, 2, 3, 4], [5, 6, 7, 8]]) >>> a2.shape (2, 4) # 括号2个值表示二维数组
>>> a3 = np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]]) >>> a3 array([[[ 1, 2, 3], [ 4, 5, 6]], [[ 7, 8, 9], [10, 11, 12]]]) >>> a3.shape (2, 2, 3) # 括号3个值表示三维数组
语句: reshape(shape, order='C' )
作用:不改变数据的条件下修改形状
参数说明:
>>>a = np.array([[1,2,3,4],[5,6,7,8]]) >>> a.reshape(4,2) array([[1, 2], [3, 4], [5, 6], [7, 8]]) # 再次查看数组形状发现只是临时修改 >>> a.shape (2, 4) # 可以用一个变量来接收返回值 >>> b = a.reshape(4,2) >>> b array([[1, 2], [3, 4], [5, 6], [7, 8]])
语句: flatten( order='C')
作用:展平的数组元素并拷贝一份,顺序通常是"C风格"
注意:修改返回的数组不会对原数组产生影响
参数说明:
>>> c = np.array([[1, 2],[3, 4],[5, 6],[7, 8]]) >>> c array([[1, 2], [3, 4], [5, 6], [7, 8]]) >>> c.shape (4, 2) # 方法1 >>> c.reshape(c.shape[0]*c.shape[1],) array([1, 2, 3, 4, 5, 6, 7, 8]) # 方法2 >>> c.flatten() array([1, 2, 3, 4, 5, 6, 7, 8])
numpy的广播机制造成的,在运算过程中,加减乘除的值被广播到所有的元素上面.
>>> a = np.array([[1,2,3,4,5],[6,7,8,9,10]]) >>> a array([[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10]]) >>> a + 1 array([[ 2, 3, 4, 5, 6], [ 7, 8, 9, 10, 11]]) >>> a - 1 array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]) >>> a * 2 array([[ 2, 4, 6, 8, 10], [12, 14, 16, 18, 20]]) >>> a / 10 array([[0.1, 0.2, 0.3, 0.4, 0.5], [0.6, 0.7, 0.8, 0.9, 1. ]])
两数组相同维度下加减乘除
>>> a = np.array([[1,2,3,4,5],[6,7,8,9,10]]) >>> a array([[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10]]) >>> b = np.arange(11,21).reshape(2,5) >>> b array([[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]) >>> a + b array([[12, 14, 16, 18, 20], [22, 24, 26, 28, 30]]) >>> a - b array([[-10, -10, -10, -10, -10], [-10, -10, -10, -10, -10]]) >>> a * b array([[ 11, 24, 39, 56, 75], [ 96, 119, 144, 171, 200]]) >>> a / b array([[0.09090909, 0.16666667, 0.23076923, 0.28571429, 0.33333333], [0.375 , 0.41176471, 0.44444444, 0.47368421, 0.5 ]])
二行5列 VS 一行5列
>>> a = np.arange(1,6).reshape(5,) >>> a array([1, 2, 3, 4, 5]) >>> b = np.arange(11,21).reshape(2,5) >>> b array([[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]) >>> a + b array([[12, 14, 16, 18, 20], [17, 19, 21, 23, 25]]) >>> a * b array([[ 11, 24, 39, 56, 75], [ 16, 34, 54, 76, 100]])
2行一列 VS 2行5列
>>> a = np.arange(1,3).reshape(2,1) >>> a array([[1], [2]]) >>> b = np.arange(11,21).reshape(2,5) >>> b array([[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]) >>> a + b array([[12, 13, 14, 15, 16], [18, 19, 20, 21, 22]]) >>> a * b array([[11, 12, 13, 14, 15], [32, 34, 36, 38, 40]])
不同维度
>>> a = np.arange(1,13).reshape(3,4) >>> a array([[ 1, 2, 3, 4], [ 5, 6, 7, 8], [ 9, 10, 11, 12]]) >>> b = np.arange(1,11).reshape(2,5) >>> b array([[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10]]) >>> a + b Traceback (most recent call last): File "", line 1, in ValueError: operands could not be broadcast together with shapes (3,4) (2,5)
转置是一种变换,对于numpy中的数组来说,就是在对角线方向交换数据,目的也是为了更方便的去处理数据
>>> t array([[ 0, 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10, 11], [12, 13, 14, 15, 16, 17]]) >>> t.transpose() array([[ 0, 6, 12], [ 1, 7, 13], [ 2, 8, 14], [ 3, 9, 15], [ 4, 10, 16], [ 5, 11, 17]]) >>> t.swapaxes(1,0) array([[ 0, 6, 12], [ 1, 7, 13], [ 2, 8, 14], [ 3, 9, 15], [ 4, 10, 16], [ 5, 11, 17]]) >>> t.T array([[ 0, 6, 12], [ 1, 7, 13], [ 2, 8, 14], [ 3, 9, 15], [ 4, 10, 16], [ 5, 11, 17]])
以上的三种方法都可以实现二维数组的转置的效果,大家能够看出来,转置和交换轴的效果一样
数组水平或者竖直拼接很简单,但是拼接之前应该注意什么?
竖直拼接的时候:每一列代表的意义相同!!!否则牛头不对马嘴
如果每一列的意义不同,这个时候应该交换某一组的数的列,让其和另外一类相同
>>> t1 = np.arange(0,12).reshape(2,6) >>> t1 array([[ 0, 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10, 11]]) >>> t2 = np.arange(12,24).reshape(2,6) >>> t2 array([[12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23]]) >>> np.vstack((t1,t2)) # 竖直拼接 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]]) >>> np.hstack((t1,t2)) # 水平拼接 array([[ 0, 1, 2, 3, 4, 5, 12, 13, 14, 15, 16, 17], [ 6, 7, 8, 9, 10, 11, 18, 19, 20, 21, 22, 23]])
>>> t = np.arange(12,24).reshape(3,4) >>> t array([[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]) >>> t[[1,2],:] = t[[2,1],:] # 行交换 >>> t array([[12, 13, 14, 15], [20, 21, 22, 23], [16, 17, 18, 19]]) >>> t[:,[0,2]] = t[:,[2,0]] # 列交换 >>> t array([[14, 13, 12, 15], [22, 21, 20, 23], [18, 17, 16, 19]])
>>> t = np.arange(0,20).reshape(5,4) >>> t array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19]]) >>> t.clip(9,10) # 小于9的修改为9,大于10的修改为10 array([[ 9, 9, 9, 9], [ 9, 9, 9, 9], [ 9, 9, 10, 10], [10, 10, 10, 10], [10, 10, 10, 10]])
>>> t = np.arange(0,20).reshape(5,4) >>> t array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19]]) >>> t[0,0] = 100 >>> t array([[100, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [ 12, 13, 14, 15], [ 16, 17, 18, 19]]) >>> t[:,0:2] = 0 >>> t array([[ 0, 0, 2, 3], [ 0, 0, 6, 7], [ 0, 0, 10, 11], [ 0, 0, 14, 15], [ 0, 0, 18, 19]]) >>> # 把数组中小于20的数全部修改为0 >>> t[t<20] = 0 >>> t array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]) >>> t[t==0] = 1 # 等于0的都修改为1 >>> t array([[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]])
1)获取最大值最小值的位置
# 一维数组 >>> t1 array([11, 12, 13, 14, 15, 16, 17, 18, 19]) >>> np.argmax(t1,axis=0) #返回最大值索引 8 >>> np.argmin(t1,axis=0) #返回最小值索引 0 # 多维数组就返回每行的最大或最小值 >>> t2 = np.arange(0,16).reshape(4,4) >>> t2 array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15]]) >>> np.argmax(t2,axis=0) array([3, 3, 3, 3], dtype=int64) >>> np.argmin(t2,axis=0) array([0, 0, 0, 0], dtype=int64)
2)创建一个全0的数组: np.zeros((3,4))
>>> np.zeros((3,4)) array([[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]])
3)创建一个全1的数组: np.ones((3,4))
>>> np.ones((3,4)) array([[1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.]])
4)创建一个对角线为1的正方形数组(方阵): np.eye(3)
>>> np.eye(3) array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]) >>> np.eye(10) array([[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 1., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 1., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 1., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 1., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 1., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 1., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]])
方法 | 说明 |
---|---|
np.random.rand(d0, d1,.. dn) |
创建d0-dn维度的均匀分布的随机数数组,浮点数,范围从0-1 |
np.random.randn(d0,d1,. .dn) |
创建d0-dn维度的标准正态分布随机数,浮点数,平均数0,标准差1 |
np.random.randint(low, high,(shape)) (常用) |
从给定上下限范围选取随机数整数,范围是low,high,形状是shape |
np.random.uniform( low, high,(size)) |
产生具有均匀分布的小数数组,low起始值,high结束值,size形状 |
np.random.normal(loc, scale,(size)) |
从指定正态分布中随机抽取样本,分布中心是loc (概率分布的均值) ,标准差是scale,形状是size |
np.random.seed(s) |
随机数种子,s是给定的种子值。因为计算机生成的是伪随机数,所以通过设定相同的随机数种子,可以每次生成相同的随机数 |
>>> np.random.randint(10,20,(3,3)) array([[13, 15, 18], [18, 17, 15], [15, 10, 12]]) >>> np.random.randint(10,20,(3,3)) array([[17, 12, 16], [10, 10, 14], [15, 14, 12]])
a=b 完全不复制,a和b相互影响
a = b[:],视图的操作,一种切片,会创建新的对象a,但是a的数据完全由b保管,他们两个的数据变化是一致的,
a = b.copy(),复制,a和b互不影响