Numpy复习day_1_2

数组的变形

数组的变形也是一类非常有用的操作。

数组变形最灵活的实现方式是通过 reshape() 函数来实现。例如,如果你希望将数字 1~9 放入一个
3×3 的矩阵中,可以采用如下方法:

import numpy as np
#全部行都能输出
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

reshape改变数组的形状

#  我们先设置随机数种子
np.random.seed(0)
x1 = np.random.randint(10, size=6) # 一维数组
x2 = np.random.randint(10, size=(3, 4)) # 二维数组
x3 = np.random.randint(10, size=(3, 4, 5)) # 三维数组
x1
array([5, 0, 3, 3, 7, 9])
x1.shape
(6,)
# 数组的变形: 只要个数能对的上,怎么变都行
x1.reshape(2,3)
array([[5, 0, 3],
       [3, 7, 9]])
x1.reshape(2,3)
array([[5, 0, 3],
       [3, 7, 9]])
x1.reshape(6,1)
array([[5],
       [0],
       [3],
       [3],
       [7],
       [9]])
x1.reshape(1,6)
array([[5, 0, 3, 3, 7, 9]])
x1
array([5, 0, 3, 3, 7, 9])
x1.reshape(1,1,6)
array([[[5, 0, 3, 3, 7, 9]]])


x1.reshape((3, 2))
array([[5, 0],
       [3, 3],
       [7, 9]])
x1.reshape((2, 3))
array([[5, 0, 3],
       [3, 7, 9]])
x1.reshape((1, 6))
array([[5, 0, 3, 3, 7, 9]])
x1.reshape((1, 6)).shape
(1, 6)
x1.reshape((6, 1))
array([[5],
       [0],
       [3],
       [3],
       [7],
       [9]])
x1.reshape((2, -1))
array([[5, 0, 3],
       [3, 7, 9]])
x1.reshape((-1, 3))
array([[5, 0, 3],
       [3, 7, 9]])
x2
array([[3, 5, 2, 4],
       [7, 6, 8, 8],
       [1, 6, 7, 7]])
x2.reshape((6,2))
array([[3, 5],
       [2, 4],
       [7, 6],
       [8, 8],
       [1, 6],
       [7, 7]])
x2.reshape((2, 6))
array([[3, 5, 2, 4, 7, 6],
       [8, 8, 1, 6, 7, 7]])
x2
array([[3, 5, 2, 4],
       [7, 6, 8, 8],
       [1, 6, 7, 7]])
# 因为元素个数不能变, 当所有维度中只剩下一个维度没有确定的时候,实际上这个维度已经确定了
# 可以省略一个维度用-1代替
x2.reshape(-1,6)
array([[3, 5, 2, 4, 7, 6],
       [8, 8, 1, 6, 7, 7]])
x2.reshape((6, 2))
array([[3, 5],
       [2, 4],
       [7, 6],
       [8, 8],
       [1, 6],
       [7, 7]])
x2
x2.reshape((2, 6))
x2.reshape((6, 2))
x2.reshape((1, 12))
x2.reshape((1, -1))
array([[3, 5, 2, 4],
       [7, 6, 8, 8],
       [1, 6, 7, 7]])






array([[3, 5, 2, 4, 7, 6],
       [8, 8, 1, 6, 7, 7]])






array([[3, 5],
       [2, 4],
       [7, 6],
       [8, 8],
       [1, 6],
       [7, 7]])






array([[3, 5, 2, 4, 7, 6, 8, 8, 1, 6, 7, 7]])






array([[3, 5, 2, 4, 7, 6, 8, 8, 1, 6, 7, 7]])

请注意,如果希望该方法可行,那么原始数组的大小必须和变形后数组
的大小一致。如果满足这个条件,reshape 方法将会用到原始数组的一
个非副本视图。


小技巧

技巧:在使用 reshape 时,可以将其中的一个维度指定为 -1,Numpy 会自动计算出它的真实值

a.reshape(3, -1)
array([[100.,   0.,   4.,   0.],
       [  4.,   9.,   3.,   4.],
       [  6.,   8.,   9.,   2.]])


拉伸成一维数组(数组的平铺)

x2
array([[3, 5, 2, 4],
       [7, 6, 8, 8],
       [1, 6, 7, 7]])
# 拉成一维
x2.ravel()
array([3, 5, 2, 4, 7, 6, 8, 8, 1, 6, 7, 7])
x3.ravel()
array([8, 1, 5, 9, 8, 9, 4, 3, 0, 3, 5, 0, 2, 3, 8, 1, 3, 3, 3, 7, 0, 1,
       9, 9, 0, 4, 7, 3, 2, 7, 2, 0, 0, 4, 5, 5, 6, 8, 4, 1, 4, 9, 8, 1,
       1, 7, 9, 9, 3, 6, 7, 2, 0, 3, 5, 9, 4, 4, 6, 4])

x2.ravel()
array([3, 5, 2, 4, 7, 6, 8, 8, 1, 6, 7, 7])
x3
array([[[8, 1, 5, 9, 8],
        [9, 4, 3, 0, 3],
        [5, 0, 2, 3, 8],
        [1, 3, 3, 3, 7]],

       [[0, 1, 9, 9, 0],
        [4, 7, 3, 2, 7],
        [2, 0, 0, 4, 5],
        [5, 6, 8, 4, 1]],

       [[4, 9, 8, 1, 1],
        [7, 9, 9, 3, 6],
        [7, 2, 0, 3, 5],
        [9, 4, 4, 6, 4]]])
x3.ravel()
array([8, 1, 5, 9, 8, 9, 4, 3, 0, 3, 5, 0, 2, 3, 8, 1, 3, 3, 3, 7, 0, 1,
       9, 9, 0, 4, 7, 3, 2, 7, 2, 0, 0, 4, 5, 5, 6, 8, 4, 1, 4, 9, 8, 1,
       1, 7, 9, 9, 3, 6, 7, 2, 0, 3, 5, 9, 4, 4, 6, 4])

转置

x2
array([[3, 5, 2, 4],
       [7, 6, 8, 8],
       [1, 6, 7, 7]])
# 交换行和列
x2.T
array([[3, 7, 1],
       [5, 6, 6],
       [2, 8, 7],
       [4, 8, 7]])
x2.T
array([[3, 7, 1],
       [5, 6, 6],
       [2, 8, 7],
       [4, 8, 7]])
x2.T.shape
(4, 3)
x2.shape
(3, 4)

无论是ravel、reshape、T,它们都不会更改原有的数组形状,都是返回一个新的数组。

也就是我们之前学过的浅复制





数组的拼接

拼接或连接 NumPy 中的两个数组主要由
np.concatenate实现

x = np.array([34, 43, 12])
y = np.array([343, 34, 676])
x
array([34, 43, 12])
y
array([343,  34, 676])
# 拼接
np.concatenate((x,y))
array([ 34,  43,  12, 343,  34, 676])

np.concatenate((x, y))
array([ 34,  43,  12, 343,  34, 676])

x = np.array([1, 2, 3])
y = np.array([3, 2, 1])
np.concatenate([x, y])
array([1, 2, 3, 3, 2, 1])
np.concatenate([x, y], axis=0)
array([1, 2, 3, 3, 2, 1])
np.concatenate([x, y], axis=1)  #报错, 只有一个维度
---------------------------------------------------------------------------

AxisError                                 Traceback (most recent call last)

 in ()
----> 1 np.concatenate([x, y], axis=1)  #报错, 只有一个维度


AxisError: axis 1 is out of bounds for array of dimension 1
z = [0, 0, 0]
x2 = np.concatenate([x, y, z]).reshape((3, 3))
x2
array([[1, 2, 3],
       [3, 2, 1],
       [0, 0, 0]])
x3 = x2.copy()
x3
array([[1, 2, 3],
       [3, 2, 1],
       [0, 0, 0]])
x2
x3
array([[1, 2, 3],
       [3, 2, 1],
       [0, 0, 0]])






array([[1, 2, 3],
       [3, 2, 1],
       [0, 0, 0]])
np.concatenate((x2,x3),axis=0 )
array([[1, 2, 3],
       [3, 2, 1],
       [0, 0, 0],
       [1, 2, 3],
       [3, 2, 1],
       [0, 0, 0]])
np.concatenate((x2,x3),axis=1 )
array([[1, 2, 3, 1, 2, 3],
       [3, 2, 1, 3, 2, 1],
       [0, 0, 0, 0, 0, 0]])
np.concatenate((x2,x3),axis=-2 )
array([[1, 2, 3],
       [3, 2, 1],
       [0, 0, 0],
       [1, 2, 3],
       [3, 2, 1],
       [0, 0, 0]])
np.concatenate((x2,x3),axis=-1 )
array([[1, 2, 3, 1, 2, 3],
       [3, 2, 1, 3, 2, 1],
       [0, 0, 0, 0, 0, 0]])


np.concatenate([x2, x3])
array([[1, 2, 3],
       [3, 2, 1],
       [0, 0, 0],
       [1, 2, 3],
       [3, 2, 1],
       [0, 0, 0]])
np.concatenate([x2, x3], axis=1)
array([[1, 2, 3, 1, 2, 3],
       [3, 2, 1, 3, 2, 1],
       [0, 0, 0, 0, 0, 0]])
x2
array([[1, 2, 3],
       [3, 2, 1],
       [0, 0, 0]])
x2_copy = x2.copy()
np.concatenate([x2, x2_copy])
array([[1, 2, 3],
       [3, 2, 1],
       [0, 0, 0],
       [1, 2, 3],
       [3, 2, 1],
       [0, 0, 0]])
np.concatenate([x2, x2_copy], axis=1)
array([[1, 2, 3, 1, 2, 3],
       [3, 2, 1, 3, 2, 1],
       [0, 0, 0, 0, 0, 0]])

数据的分片

将一个数组分成几个较小的数组

既然可以将多个数组进行对堆叠,自然也可以将一个数组拆分成多个小数组。

使用split,可以指定均匀切割成几份,也可以指定沿着哪个位置进行切割, 还可以指定沿着哪个轴进行切割.

np.split(ary, indices_or_sections, axis=0)


a = np.arange(24).reshape(4,6)
a
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]])
a.shape
(4, 6)
# sections均匀切分成几份
# indices 手动指定切分点
np.split(a,2 ,axis = 0)
[array([[ 0,  1,  2,  3,  4,  5],
        [ 6,  7,  8,  9, 10, 11]]), array([[12, 13, 14, 15, 16, 17],
        [18, 19, 20, 21, 22, 23]])]
np.split(a,4 ,axis = 0)
[array([[0, 1, 2, 3, 4, 5]]),
 array([[ 6,  7,  8,  9, 10, 11]]),
 array([[12, 13, 14, 15, 16, 17]]),
 array([[18, 19, 20, 21, 22, 23]])]
a
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.split(a,  6, axis = 1)[3]
array([[ 3],
       [ 9],
       [15],
       [21]])
a
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.split(a,[2,5] , axis= 1 )
[array([[ 0,  1],
        [ 6,  7],
        [12, 13],
        [18, 19]]), array([[ 2,  3,  4],
        [ 8,  9, 10],
        [14, 15, 16],
        [20, 21, 22]]), array([[ 5],
        [11],
        [17],
        [23]])]
np.split(a,[2,5] , axis= 1 )[0]
array([[ 0,  1],
       [ 6,  7],
       [12, 13],
       [18, 19]])
np.split(a,[2,5] , axis= 1 )[1]
array([[ 2,  3,  4],
       [ 8,  9, 10],
       [14, 15, 16],
       [20, 21, 22]])
np.split(a,[2,5] , axis= 1 )[2]
array([[ 5],
       [11],
       [17],
       [23]])

np.split(a,2,axis=0)
[array([[ 0,  1,  2,  3,  4,  5],
        [ 6,  7,  8,  9, 10, 11]]), array([[12, 13, 14, 15, 16, 17],
        [18, 19, 20, 21, 22, 23]])]
np.split(a,2,axis=0)[0]
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11]])
np.split(a,2,axis=0)[1]
array([[12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23]])
np.split(a,3,axis=1)
[array([[ 0,  1],
        [ 6,  7],
        [12, 13],
        [18, 19]]), array([[ 2,  3],
        [ 8,  9],
        [14, 15],
        [20, 21]]), array([[ 4,  5],
        [10, 11],
        [16, 17],
        [22, 23]])]
a
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.split(a, [1, 3])
[array([[0, 1, 2, 3, 4, 5]]), array([[ 6,  7,  8,  9, 10, 11],
        [12, 13, 14, 15, 16, 17]]), array([[18, 19, 20, 21, 22, 23]])]
np.split(a, [1])
[array([[0, 1, 2, 3, 4, 5]]), array([[ 6,  7,  8,  9, 10, 11],
        [12, 13, 14, 15, 16, 17],
        [18, 19, 20, 21, 22, 23]])]
np.split(a, [2, 5], axis=1)
[array([[ 0,  1],
        [ 6,  7],
        [12, 13],
        [18, 19]]), array([[ 2,  3,  4],
        [ 8,  9, 10],
        [14, 15, 16],
        [20, 21, 22]]), array([[ 5],
        [11],
        [17],
        [23]])]

小练习

  1. 创建一个形状为(4, 5)的数组线性序列, 线性序列的值从5开始到25.

  2. 把数字拉伸成一维数组, 直接在原数组上变化

  3. 把数组的形状改为(5, 4)

  4. 对数组进行转置

  5. 将数组分裂成(1, 5)(2, 5)(1, 5)的三个子数组

  6. 把得到的第一个数组和第三个数组进行横向拼接和纵向拼接

x = np.arange(5,25).reshape(4,5)
x
array([[ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24]])
x = x.ravel()
x
array([ 5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
       22, 23, 24])
x = x.reshape(5,4)
x
array([[ 5,  6,  7,  8],
       [ 9, 10, 11, 12],
       [13, 14, 15, 16],
       [17, 18, 19, 20],
       [21, 22, 23, 24]])
x = x.T
x
array([[ 5,  9, 13, 17, 21],
       [ 6, 10, 14, 18, 22],
       [ 7, 11, 15, 19, 23],
       [ 8, 12, 16, 20, 24]])
a1 , a2 ,a3 = np.split(x, [1,3], axis = 0)
a1
array([[ 5,  9, 13, 17, 21]])
a2
array([[ 6, 10, 14, 18, 22],
       [ 7, 11, 15, 19, 23]])
a3
array([[ 8, 12, 16, 20, 24]])
np.concatenate((a1,a3), axis = 0)
array([[ 5,  9, 13, 17, 21],
       [ 8, 12, 16, 20, 24]])
np.concatenate((a1,a3), axis = 1)
array([[ 5,  9, 13, 17, 21,  8, 12, 16, 20, 24]])



广播机制

什么是广播

我们都知道,Numpy中的基本运算(加、减、乘、除、求余等等)都是元素级别的,但是这仅仅局限于两个数组的形状相同的情况下。

可是大家又会发现,如果让一个数组加1的话,结果是整个数组的结果都会加1,这是什么情况呢?

x = np.array(range(12)).reshape(3, 4)
x
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
x + 1
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])
x2 = np.array([1, 2, 3, 4])
x2
array([1, 2, 3, 4])
x
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
x + x2
array([[ 1,  3,  5,  7],
       [ 5,  7,  9, 11],
       [ 9, 11, 13, 15]])

广播broadcast: 当把一个低维和一个高维数据进行计算时,会自动将低维数据拉伸到和高维相同的形状,然后进行对应元素位置操作

其实这就是广播机制:Numpy 可以转换这些形状不同的数组,使它们都具有相同的大小,然后再对它们进行运算。给出广播示意图:

a = np.arange(3)
a
array([0, 1, 2])
b = np.arange(3).reshape(3, 1)
b
array([[0],
       [1],
       [2]])
a + b
array([[0, 1, 2],
       [1, 2, 3],
       [2, 3, 4]])

广播示例

a = np.arange(0, 40, 10)
a.shape
a
(4,)






array([ 0, 10, 20, 30])
b = np.array([0, 1, 2])
b.shape
b
(3,)






array([0, 1, 2])
c = b.reshape((3, 1))
c
array([[0],
       [1],
       [2]])
a + c
array([[ 0, 10, 20, 30],
       [ 1, 11, 21, 31],
       [ 2, 12, 22, 32]])

Numpy运算

Numpy 中数组上的算术运算符使用元素级别。最后的结果使用新的一个数组来返回。

基本运算操作

a = np.array( [20,30,40,50] )
b = np.arange( 4 )
a
b
array([20, 30, 40, 50])






array([0, 1, 2, 3])
c = a-b
c
array([20, 29, 38, 47])
b**2
array([0, 1, 4, 9])
a<35
array([ True,  True, False, False])

需要注意的是,乘法运算符*的运算在NumPy数组中也是元素级别的(这与许多矩阵语言不同)。如果想要执行矩阵乘积,可以使用dot函数:

# 二维数组就可以当做矩阵用
A = np.array( [[1,1], [0,1]] )
B = np.array( [[2,0], [3,4]] )
A
B
array([[1, 1],
       [0, 1]])






array([[2, 0],
       [3, 4]])
A * B
array([[2, 0],
       [0, 4]])
A.dot(B)
array([[5, 4],
       [3, 4]])
B.dot(A)
array([[2, 2],
       [3, 7]])
# 也可以用函数
np.dot(A,B)
array([[5, 4],
       [3, 4]])
np.dot(B,A)
array([[2, 2],
       [3, 7]])


A.dot(B)
array([[5, 4],
       [3, 4]])
B.dot(A)
array([[2, 2],
       [3, 7]])
np.dot(A, B)
array([[5, 4],
       [3, 4]])
np.dot(B,A)
array([[2, 2],
       [3, 7]])
A*B                         # 元素乘积(elementwise product)
A.dot(B)                    # 矩阵相乘(matrix product)
np.dot(A, B)            # 矩阵相乘的另一种方式(another matrix product)
array([[2, 0],
       [0, 4]])






array([[5, 4],
       [3, 4]])






array([[5, 4],
       [3, 4]])

某些操作(如+=和*=)可以修改现有数组,而不是创建新数组。

a = np.ones((2,3), dtype=np.int32)
a *= 3
a
array([[3, 3, 3],
       [3, 3, 3]], dtype=int32)
b = np.random.random((2,3))
b
array([[0.16494046, 0.36980809, 0.14644176],
       [0.56961841, 0.70373728, 0.28847644]])
a.dtype
b.dtype
dtype('int32')






dtype('float64')

常用函数

数学运算函数
add(x1,x2 [,out]) 按元素添加参数,等效于 x1 + x2
subtract(x1,x2 [,out]) 按元素方式减去参数,等效于x1 - x2
multiply(x1,x2 [,out]) 逐元素乘法参数,等效于x1 * x2
divide(x1,x2 [,out]) 逐元素除以参数,等效于x1 / x2
exp(x [,out]) 计算输入数组中所有元素的指数。
exp2(x [,out]) 对于输入数组中的所有p,计算2 * p*。
log(x [,out]) 自然对数,逐元素。
log2(x [,out]) x的基础2对数。
log10(x [,out]) 以元素为单位返回输入数组的基数10的对数。
expm1(x [,out]) 对数组中的所有元素计算exp(x) - 1
log1p(x [,out]) 返回一个加自然对数的输入数组,元素。
sqrt(x [,out]) 按元素方式返回数组的正平方根。
square(x [,out]) 返回输入的元素平方。
sin(x [,out]) 三角正弦。
cos(x [,out]) 元素余弦。
tan(x [,out]) 逐元素计算切线。

和Math包基本一样

A = np.array( [[1,1], [0,1]] )
B = np.array( [[2,0], [3,4]] )
A
B
array([[1, 1],
       [0, 1]])






array([[2, 0],
       [3, 4]])
np.add(A, B)
array([[3, 1],
       [3, 5]])
K = np.array([[0, 0], [0, 0]])
np.add(A, B, out=K , )
array([[3, 1],
       [3, 5]])
K
array([[3, 1],
       [3, 5]])
K = np.array([[0, 0], [0, 0]])
np.add(A, B, out=K , where=[[True, False], [False, True]])
array([[3, 0],
       [0, 5]])

A
array([[1, 1],
       [0, 1]])
np.exp(A)
array([[2.71828183, 2.71828183],
       [1.        , 2.71828183]])
np.exp2(B)
array([[ 4.,  1.],
       [ 8., 16.]])
x = np.random.randint(4, size=6).reshape(2,3)
x
array([[0, 2, 3],
       [3, 2, 3]])
y = np.random.randint(4, size=6).reshape(2,3)
y
array([[1, 1, 1],
       [3, 2, 0]])
x + y
array([[1, 3, 4],
       [6, 4, 3]])
np.add(x, y)
array([[1, 3, 4],
       [6, 4, 3]])
np.square(x)
array([[  0,   1,   4,   9],
       [ 16,  25,  36,  49],
       [ 64,  81, 100, 121]])
x
array([[0, 2, 3],
       [3, 2, 3]])
np.log1p(x)
array([[0.        , 1.09861229, 1.38629436],
       [1.38629436, 1.09861229, 1.38629436]])

规约函数

下面所有的函数都支持axis来指定不同的轴,用法都是类似的。

函数名称 函数功能
ndarray.sum([axis,dtype,out,keepdims]) 返回给定轴上的数组元素的总和。
ndarray.cumsum([axis,dtype,out]) 返回沿给定轴的元素的累积和。
ndarray.mean([axis,dtype,out,keepdims]) 返回沿给定轴的数组元素的平均值。
ndarray.var([axis,dtype,out,ddof,keepdims]) 沿给定轴返回数组元素的方差。
ndarray.std([axis,dtype,out,ddof,keepdims]) 返回给定轴上的数组元素的标准偏差。
ndarray.argmax([axis,out]) 沿着给定轴的最大值的返回索引。
ndarray.min([axis,out,keepdims]) 沿给定轴返回最小值。
ndarray.argmin([axis,out]) 沿着给定轴的最小值的返回索引。
x
array([[5, 0, 3],
       [3, 7, 9],
       [3, 5, 2],
       [4, 7, 6]])
# 返回最大值的位置(索引)
np.argmax(x, axis = 0)
array([0, 1, 1])
np.argmax(x, axis = 1)
array([0, 2, 1, 1])


np.random.seed(0)
x = np.random.randint(10, size=12).reshape(4,3)
x
array([[5, 0, 3],
       [3, 7, 9],
       [3, 5, 2],
       [4, 7, 6]])
np.sum(x) # 默认全部相加
54
np.sum(x, axis=0)
array([15, 19, 20])
np.sum(x, axis = 1)
array([ 8, 19, 10, 17])
x
array([[5, 0, 3],
       [3, 7, 9],
       [3, 5, 2],
       [4, 7, 6]])
np.max(x)
9
np.max(x, axis = 0)
array([5, 7, 9])
np.max(x, axis = 1)
array([5, 9, 5, 7])


np.sum(x)
20
np.sum(x,axis=0)
array([ 5, 13,  2])
np.sum(x,axis=1)
array([ 8, 12])
x2
array([[1, 2, 3],
       [3, 2, 1],
       [0, 0, 0]])
np.max(x2, axis=0)
array([3, 2, 3])
np.max(x2, axis=1)
array([3, 3, 0])

x2
array([[1, 2, 3],
       [3, 2, 1],
       [0, 0, 0]])
np.mean(x2, axis=0)
array([1.33333333, 1.33333333, 1.33333333])
np.mean(x2, axis=1)
array([2., 2., 0.])
np.var(x2, axis=0)
array([1.55555556, 0.88888889, 1.55555556])
x2
array([[1, 2, 3],
       [3, 2, 1],
       [0, 0, 0]])
np.std(x2, axis=0)
array([1.24721913, 0.94280904, 1.24721913])
k = np.random.normal(5, 2, (80, 80))
np.mean(k)
5.019138146225985
np.std(k, axis=0)
array([1.90857005, 1.93067186, 2.10947361, 1.9179189 , 1.73980266,
       2.16096136, 2.0759987 , 2.17462779, 2.08852479, 2.20888057,
       1.82197311, 2.23281072, 2.12512435, 2.0024872 , 1.88207936,
       2.12088724, 1.93069481, 1.89314531, 2.18514739, 1.88410632,
       2.11402417, 2.06857452, 1.94368281, 2.14702711, 2.00160331,
       2.21524697, 1.86504071, 2.14050971, 1.83418208, 2.03129044,
       2.13525736, 2.10758075, 2.10468089, 1.76431623, 2.0070851 ,
       1.98110869, 1.99611182, 2.02567644, 1.95456891, 1.80297269,
       1.98590209, 2.00985712, 1.64594595, 2.27229785, 1.96161247,
       2.00768777, 1.97751101, 1.85039221, 2.2781135 , 2.16023582,
       1.84169189, 1.99252765, 1.85858191, 1.9433196 , 2.20363386,
       1.82803598, 2.2973705 , 1.68768072, 1.93671806, 2.19355604,
       2.20139093, 2.01738891, 2.15917338, 1.81119446, 1.8131685 ,
       2.14806466, 2.07839643, 2.07429895, 1.90653217, 2.14074046,
       1.79145042, 1.86254919, 1.86888617, 2.01774458, 1.96709182,
       2.01231325, 2.01515944, 2.01240244, 1.89002178, 1.84135625])
np.std(k, axis=1)
array([2.00218067, 1.92521956, 2.16166194, 1.93044082, 2.21236317,
       2.21060889, 1.97408264, 2.05503421, 2.02624428, 1.8383008 ,
       1.92347642, 1.8602528 , 2.20402868, 1.93684749, 2.07391296,
       1.9933529 , 2.23201319, 1.88233931, 1.90197769, 2.00134717,
       2.2499294 , 1.8296063 , 2.14716102, 2.017839  , 1.94738967,
       1.92570439, 2.02875079, 1.79371487, 1.84027046, 1.98899493,
       2.25799484, 1.7739935 , 1.76435613, 1.89030165, 1.77882468,
       2.02904184, 1.97107301, 1.89548092, 2.06012406, 2.11555832,
       1.96928751, 1.90973688, 1.82980445, 2.06555234, 1.72741481,
       1.97529845, 1.96340418, 2.06352364, 1.95432032, 1.85215972,
       1.77907427, 1.72443705, 1.83458035, 1.83792735, 2.03585948,
       1.72080181, 2.07768709, 2.17950262, 2.25242241, 2.10641472,
       2.05852844, 2.0778235 , 1.77421639, 2.01945237, 2.00774073,
       2.28899057, 2.02848533, 1.89539281, 2.02272147, 2.1635638 ,
       2.05949725, 2.0828721 , 1.91725529, 1.9317692 , 2.31393014,
       2.16784311, 2.34284528, 1.85175814, 1.84160923, 2.12723787])
np.sum(x)
20
np.sum(x, axis=0)
array([ 5, 13,  2])
np.sum(x, axis=1)
array([ 8, 12])
np.argmax(x)
4
np.argmax(x, axis=0)
array([0, 1, 0], dtype=int64)
np.argmax(x, axis=1)
array([1, 1], dtype=int64)

x
array([[5, 0, 3],
       [3, 7, 9],
       [3, 5, 2],
       [4, 7, 6]])
np.sum(x , axis = 0)
array([15, 19, 20])
np.sum(x , axis = 1)
array([ 8, 19, 10, 17])
# 当2个轴毁掉了一个之后,就变成一维数据,

你可能感兴趣的:(Numpy复习day_1_2)