【Análisis y visualización de datos】 Operaciones de matriz y matriz

Crea matrices rápidamente

import numpy as np
# 返回符合正态分布的数组
np.random.randn(10)
array([-0.05382978,  0.57450604,  0.08319436, -1.54601915,  0.6517896 ,
       -1.31985884, -0.68791036,  2.4913952 ,  0.31322135,  0.83022095])
# 返回指定范围的一个随机数
np.random.randint(10)
9
# 创建一个随机数组
np.random.randint(10, size=20)
array([9, 4, 1, 2, 3, 3, 3, 2, 4, 0, 8, 8, 7, 6, 3, 8, 7, 4, 3, 3])
# 一维变二维 数组
np.random.randint(10, size=20).reshape(4, 5)
array([[3, 0, 6, 1, 2],
       [3, 3, 4, 4, 9],
       [6, 2, 6, 2, 5],
       [0, 9, 1, 6, 9]])
np.random.randint(10, size=(2,3))
array([[3, 8, 9],
       [5, 1, 2]])

Operación de matriz

# 范围为10,长度size
a = np.random.randint(10, size=20).reshape(4,5)
a
array([[3, 2, 8, 8, 8],
       [9, 4, 1, 4, 4],
       [5, 4, 6, 6, 3],
       [6, 5, 7, 3, 1]])
b = np.random.randint(10, size=20).reshape(4,5)
b
array([[4, 2, 0, 6, 9],
       [3, 3, 0, 2, 1],
       [0, 8, 7, 7, 1],
       [0, 2, 2, 7, 8]])
# 数组加法
a + b
array([[ 7,  4,  8, 14, 17],
       [12,  7,  1,  6,  5],
       [ 5, 12, 13, 13,  4],
       [ 6,  7,  9, 10,  9]])
# 数组减法
a - b
array([[-1,  0,  8,  2, -1],
       [ 6,  1,  1,  2,  3],
       [ 5, -4, -1, -1,  2],
       [ 6,  3,  5, -4, -7]])
# 数组乘法-对应相乘
a * b
array([[12,  4,  0, 48, 72],
       [27, 12,  0,  8,  4],
       [ 0, 32, 42, 42,  3],
       [ 0, 10, 14, 21,  8]])
# 数组除法-对应相除(除数不能为0,否则为inf提示报错)
a / b 
/Users/bennyrhys/opt/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:2: RuntimeWarning: divide by zero encountered in true_divide






array([[0.75      , 1.        ,        inf, 1.33333333, 0.88888889],
       [3.        , 1.33333333,        inf, 2.        , 4.        ],
       [       inf, 0.5       , 0.85714286, 0.85714286, 3.        ],
       [       inf, 2.5       , 3.5       , 0.42857143, 0.125     ]])
# 创建矩阵(matrix标记)
np.mat([[1,2,3],[4,5,6]])
matrix([[1, 2, 3],
        [4, 5, 6]])
a
array([[3, 2, 8, 8, 8],
       [9, 4, 1, 4, 4],
       [5, 4, 6, 6, 3],
       [6, 5, 7, 3, 1]])
# 数组转矩阵
np.mat(a)
matrix([[3, 2, 8, 8, 8],
        [9, 4, 1, 4, 4],
        [5, 4, 6, 6, 3],
        [6, 5, 7, 3, 1]])

Operaciones matriciales

A = np.mat(a)
B = np.mat(b)
A
matrix([[3, 2, 8, 8, 8],
        [9, 4, 1, 4, 4],
        [5, 4, 6, 6, 3],
        [6, 5, 7, 3, 1]])
B
matrix([[4, 2, 0, 6, 9],
        [3, 3, 0, 2, 1],
        [0, 8, 7, 7, 1],
        [0, 2, 2, 7, 8]])
A - B
matrix([[-1,  0,  8,  2, -1],
        [ 6,  1,  1,  2,  3],
        [ 5, -4, -1, -1,  2],
        [ 6,  3,  5, -4, -7]])
A + B
matrix([[ 7,  4,  8, 14, 17],
        [12,  7,  1,  6,  5],
        [ 5, 12, 13, 13,  4],
        [ 6,  7,  9, 10,  9]])
# 矩阵相乘(A列=B行才可以)
A * B
---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-43-38f193791a6b> in <module>
      1 # 矩阵相乘(A列=B行才可以)
----> 2 A * B


~/opt/anaconda3/lib/python3.7/site-packages/numpy/matrixlib/defmatrix.py in __mul__(self, other)
    218         if isinstance(other, (N.ndarray, list, tuple)) :
    219             # This promotes 1-D vectors to row vectors
--> 220             return N.dot(self, asmatrix(other))
    221         if isscalar(other) or not hasattr(other, '__rmul__') :
    222             return N.dot(self, other)


<__array_function__ internals> in dot(*args, **kwargs)


ValueError: shapes (4,5) and (4,5) not aligned: 5 (dim 1) != 4 (dim 0)
# 范围为10,长度size
a = np.mat(np.random.randint(10, size=20).reshape(4,5))
a
matrix([[0, 2, 0, 8, 1],
        [9, 0, 8, 1, 4],
        [5, 2, 1, 1, 1],
        [6, 0, 2, 9, 7]])
b = np.mat(np.random.randint(10, size=20).reshape(5,4))
b
matrix([[3, 8, 0, 8],
        [2, 2, 6, 6],
        [7, 4, 0, 3],
        [9, 6, 5, 5],
        [1, 5, 5, 0]])
a * b
matrix([[ 77,  57,  57,  52],
        [ 96, 130,  25, 101],
        [ 36,  59,  22,  60],
        [120, 145,  80,  99]])

Matriz de funciones de uso común

a = np.random.randint(10, size=20).reshape(4,5)
a
array([[6, 8, 8, 4, 2],
       [1, 8, 8, 0, 8],
       [3, 5, 3, 6, 0],
       [7, 5, 9, 3, 7]])
# 返回当前数组元素的唯一值
np.unique(a)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
# 列求和
sum(a)
array([17, 26, 28, 13, 17])
# 第一行求和
sum(a[0])
28
# 第一列的和
sum(a[:,0])
17
# 最大值第一行
max(a[0])
8
# 最大值第一列
max(a[:,0])
7
234 artículos originales publicados · Me gusta 164 · Visitas 140,000+

Supongo que te gusta

Origin blog.csdn.net/weixin_43469680/article/details/105546179
Recomendado
Clasificación