PY-NumPy | 矩阵运算

矩阵是啥真不太了解,经过长时间的分析,得出以下效果(换回pycharm)

#%% md

# 矩阵运算

#%%

import numpy as np 
np 

#%% md

## 矩阵乘法

#%%

# https://baike.baidu.com/item/%E7%9F%A9%E9%98%B5/18069 | 百度百科-矩阵

#%%

a = np.array([[4,2,3],[1,3,1]])
b = np.array([[2,7],[-5,-7],[9,3]])
a, a.shape, b, b.shape

#%%

# 分别相乘再相加
a = np.array([[1,3,5],[7,9,11]])
b = np.array([[2,4],[6,8],[10,11]])
a, b, a.shape, b.shape

#%%

a0,b0 = a[0,0],b[0,0]
a1,b1 = a[0,1],b[1,0]
a2,b2 = a[0,2],b[2,0]
c0 = a0 * b0
c1 = a1 * b1
c2 = a2 * b2
a0,a0,c0,'',a1,b1,c1,'',a2,b2,c2,'',c0+c1+c2

#%%

np.dot(a, b)  # 点乘 | A的最后一维和B的第一维必须一致 

#%%

a @ b 

#%%

a.dot(b)

#%%

b = a * 0.25 # 每个元素依次乘以0.25
a, b

#%%

a * b # 4*1, 2*0.5, 3*0.75, 1*0.25,3*0.75,1*0.25

#%%

np.multiply(a, b)  # 一样的

#%%

# dot 和 matmul 函数的结果是相同的
a = np.array([[1,2,3,4],[5,6,7,8]])
b = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
a, b, a.shape, b.shape

#%%

c = np.matmul(a, b)  # a的列数 = b的行数
c, c.shape, a @ b 

#%%

c = np.matmul(b, a)  # 这可不行

#%%

# 机器学习用到
a = np.array([[1,2,3], [2,3,4], [4,5,8]]) # shape(3,3)
b = np.linalg.inv(a) # 逆矩阵
c = np.linalg.det(a)#计算矩阵行列式
d = np.dot(a, b)
a, b, c, d 

PY-NumPy | 矩阵运算
以下是 矩阵乘法-分析

import numpy as np

a = np.array([[1,3,5,],[7,9,11]]) # (2,3)
b = np.array([[2,4],[6,8],[10,11]]) # (3,2)

# 换一组数据
a = np.array([[4,2,3,1],[1,3,1,4]])
b = np.array([[2,7],[-5,-7],[9,3],[7,2]])

c = np.dot(a, b) # [ [70,83],[178,221] ]

#
[print(' '.join(map(str, _)), end=' ') for _ in c]
print()
"""
a = [ [1,3,5], [7,9,11] ]
b = [ [2,4], [6,8], [10, 11] ]
c = [ [70,83], [178,221] ]
"""

"""
70 83 178 221
[ [A,B], [C,D] ]

A:
a[0,0]*b[0,0] = 1 * 2 = 2
a[0,1]*b[1,0] = 3 * 6 = 18
a[0,2]*b[2,0] = 5 * 10 = 50
2 + 18 + 50 = 70

B:
a[0,0]*b[0,1] = 1 * 4 = 4
a[0,1]*b[1,1] = 3 * 8 = 24
a[0,2]*b[2,1] = 5 * 11 = 55
4 + 24 + 55 = 83

C:
a[1,0]*b[0,0] = 7 * 2 = 14
a[1,1]*b[1,0] = 9 * 6 = 54
a[1,2]*b[2,0] = 11 * 10 = 110
14 + 54 + 110 = 178

D:
a[1,0]*b[0,1] = 7 * 4 = 28
a[1,1]*b[1,1] = 9 * 8 = 72
a[1,2]*b[2,1] = 11 * 11 = 121
28 + 72 + 121 = 221
"""
"""
print(
    # 1*2 + 3*6 + 5*10,
    a[0,0]*b[0,0] + a[0,1]*b[1,0] + a[0,2]*b[2,0],
    # 1*4 + 3*8 + 5*11,
    a[0,0]*b[0,1] + a[0,1]*b[1,1] + a[0,2]*b[2,1],
    # 7*2 + 9*6 + 11*10,
    a[1,0]*b[0,0] + a[1,1]*b[1,0] + a[1,2]*b[2,0],
    # 7*4 + 9*8 + 11*11
    a[1,0]*b[0,1] + a[1,1]*b[1,1] + a[1,2]*b[2,1],
)
print()
"""
"""
# -----------------------------------------------------------------------------
# A
items = []
ar, ac, br, bc = 0, 0, 0, 0

for _ in range(3): # 执行3次 => _: 0,1,2
    aa, bb = a[ar,ac], b[br,bc]
    ab = aa * bb
    items.append(ab)
    print(aa,'*', bb,'=', ab, end=' ')
    ac += 1
    br += 1

print(sum(items))
# -----------------------------------------------------------------------------
# B
items = []
ar, ac, br, bc = 0, 0, 0, 1

for _ in range(3): # 执行3次 => _: 0,1,2
    aa, bb = a[ar,ac], b[br,bc]
    ab = aa * bb
    items.append(ab)
    print(aa,'*', bb,'=', ab, end=' ')
    ac += 1
    br += 1

print(sum(items))
# -----------------------------------------------------------------------------
# C
items = []
ar, ac, br, bc = 1, 0, 0, 0

for _ in range(3): # 执行3次 => _: 0,1,2
    aa, bb = a[ar,ac], b[br,bc]
    ab = aa * bb
    items.append(ab)
    print(aa,'*', bb,'=', ab, end=' ')
    ac += 1
    br += 1

print(sum(items))
# -----------------------------------------------------------------------------
# D
items = []
ar, ac, br, bc = 1, 0, 0, 1

for _ in range(3): # 执行3次 => _: 0,1,2
    aa, bb = a[ar,ac], b[br,bc]
    ab = aa * bb
    items.append(ab)
    print(aa,'*', bb,'=', ab, end=' ')
    ac += 1
    br += 1

print(sum(items))
"""
# -----------------------------------------------------------------------------
# 仅进行二维计算
# print(a.ndim, b.ndim)  # 数组维度
if a.ndim == b.ndim == 2:
    size = ['0000', '0001', '1000', '1001']
    sMax = max(a.shape)

    # print(sMax)

    for _0 in size: # 执行4次
        items = []
        ar, ac, br, bc = map(int, list(_0))
        # print(ar, ac, br, bc)

        for _1 in range(sMax):  # 执行 sMax 次
            aa, bb = a[ar,ac], b[br,bc]
            ab = aa * bb
            items.append(ab)
            print(aa,'*', bb,'=', ab, end=' ')
            ac += 1
            br += 1
        print(sum(items))

else:
    print('暂无法计算')
# -----------------------------------------------------------------------------

PY-NumPy | 矩阵运算
就这样,有待改善。。

上一篇:【学习视频】第3期2020-PostgreSQL+MySQL 联合解决方案课程 - 适合架构师与业务开发者


下一篇:【重新发现PostgreSQL之美】- 48 聚合、窗口过滤器