Python 高数篇 3

Python 高数篇 3

零蚀


矩阵的运用

  • 矩阵的输出
import numpy as np

# 设置矩阵

data = np.array([
    [[1, 2, 3], [1, 2, 3], [1, 2, 3]],
    [[1, 2, 3], [1, 2, 3], [1, 2, 3]],

])
# Returns 
# -------
# out : ndarray
# An array object satisfying the specified requirements.
# 打印数据的维度(2, 3, 3)第一个括号2个元素,每个元素的3个维度,每个维度里有3个元素维度
print(data.shape)  
print(data*3)

矩阵相乘:需要第一个矩阵的列数等于第二个举证行数

  • 矩阵的点乘
matrix = np.array([
    [1, 2, 3],
    [2, 4, 6]

])

matrix2 = np.array([
    [3, 4],
    [6, 5],
    [6, 5]
])

# 矩阵的点乘(不能写*,而且要保持第一个的列等于第二个的行)

当矩阵出现以下的情况的时候,不满足点乘的需求则,会自动补1,刚好形成了线性回归的公式:

|x1|
|x2|
|x3|            |m|
|x4|     *      |b|
|x5|
|x6|
不满足矩阵点乘

补充后:

|x1|1|                      |mx1+b|
|x2|1|                      |mx2+b|
|x3|1|            |m|       |mx3+b|
|x4|1|     *      |b|    =  |mx4+b|
|x5|1|                      |mx5+b|
|x6|1|                      |mx6+b|

因此可得Feature * weight,此公式时偏导数中公式的一部分。

  • 一次性求出偏导的矩阵公式

F e a t u r e T ∗ ( ( F e a t u r e ∗ W e i g h t − L a b e l s ) ) n \frac{Feature^T*((Feature*Weight-Labels))}{n}\quad nFeatureT((FeatureWeightLabels))

这里的Feature*Weight-Labels相当于偏导数中(mx+b)- Actual,Feature的转秩相当于用x和1分别呈上偏导数(mx+b)- Actual,则可以一次得出m和b。

代码如下:

data = np.array([
    [80, 200],
    [95, 230],
    [104, 245],
    [112, 274],
    [125, 259],
    [135, 262],
])
m = 1
b = 1

weight = np.array([
    [m],
    [b]
])

# 将label数组换算成矩阵
label = np.expand_dims(data[:, -1], axis=1)
featrue = np.append(data[:, 0:1], np.ones(shape=(6, 1)), axis=1)

learningrate = 0.00001


def grandentdecent():
    '''返回的array,矩阵第0行是对m的偏导,第1行对b的求导'''
    array = np.dot(featrue.T, (np.dot(featrue, weight) - label)) / len(featrue) * 2
    return array


def train():
    for i in range(1,3):
        result = grandentdecent()
        global weight
        weight = weight - result * learningrate
        print("数据m={},b={}".format(weight[0][0], weight[1][0]))
        if abs(weight[0][0]) < 0.5 and abs(weight[1][0]) < 0.5:
            break


train()
print("数据m={},b={}".format(weight[0][0], weight[1][0]))

首先初等数学运用了很多的for循环,导致时间成本加大很多,而矩阵运算节约了很多的时间成本,初等数学是在cpu中计算,而矩阵可以在gpu中进行运算。

  • 多维度计算
data = np.loadtxt('cars.csv', delimiter=',', skiprows=1, usecols=(4, 5, 1))

m1 = 1
m2 = 1
b = 1

weight = np.array([
    [m1],
    [m2],
    [b]
])



# 将label数组换算成矩阵
label = np.expand_dims(data[:, -1], axis=1)
featrue = np.append(data[:, 0:2], np.ones(shape=(len(data), 1)), axis=1)

learningrate = 0.00001


def grandentdecent():
    '''返回的array,矩阵第0行是对m的偏导,第1行对b的求导'''
    array = np.dot(featrue.T, (np.dot(featrue, weight) - label)) / len(featrue) * 2
    return array


def train():
    for i in range(1, 30):
        result = grandentdecent()
        global weight
        weight = weight - result * learningrate
        print("数据m1={},m2={},b={}".format(weight[0][0], weight[1][0],weight[2][0]))
        if abs(weight[0][0]) < 0.5 and abs(weight[1][0]) and abs(weight[2][0])< 0.5:
            break

train()
print("数据m1={},m2={},b={}".format(weight[0][0], weight[1][0],weight[2][0]))

矩阵和图片

  • 计算机图形变化

    • 引入画图的科学库
    # 引入可续图形库
    import matplotlib.pyplot as plt
    import numpy as np
    
    point = np.array([
        [1, 2],
        [5, 10],
        [6, 4],
        [13,10]
    ])
    
    plt.plot(point[:, 0], point[:, 1])
    plt.show()
    

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dUyE1k4o-1579446578278)(media/15791810079195/15794381438423.jpg)]

    • 设置x和y的坐标轴
    # 指定x,y轴范围
    plt.xlim(-10, 10)
    plt.ylim(-10, 10)
    
    • 图像平移
    # 图形平移
    move = np.array([[1, 0]])
    new_point = point + move
    
    plt.plot(point[:, 0], point[:, 1])
    plt.plot(new_point[:, 0], new_point[:, 1])
    plt.show()
    

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pzjsWz8O-1579446578279)(media/15791810079195/15794386228160.jpg)]

  • 2D旋转

    ∣ x ′ y ′ ∣ = ∣ c o s ( θ ) − s i n ( θ ) s i n ( θ ) c o s ( θ ) ∣ ∣ x y ∣ \begin{vmatrix}x^{'}\\y^{'}\end{vmatrix}=\begin{vmatrix}cos(\theta)&-sin(\theta)\\sin(\theta)& cos(\theta)\end{vmatrix}\begin{vmatrix}x\\y\end{vmatrix} xy=cos(θ)sin(θ)sin(θ)cos(θ)xy

    # 引入可续图形库
    import matplotlib.pyplot as plt
    import numpy as np
    import math
    
    point = np.array([
        [1, 2],
        [5, 10],
        [6, 4],
        [13, 10]
    ])
    # 图形旋转公式,以原点为中心,旋转30度
    move = np.array([
        [math.cos(math.pi / 60), -math.sin(math.pi / 6)],
        [math.sin(math.pi / 6), math.cos(math.pi / 6)]
    ])
    
    new_point = np.dot(point, move.T)
    plt.plot(point[:, 0], point[:, 1])
    plt.plot(new_point[:, 0], new_point[:, 1])
    plt.show()
    

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ETzSuczF-1579446578280)(media/15791810079195/15794427039865.jpg)]

    如果翻转的话直接点乘上 ∣ − 1 0 0 − 1 ∣ \begin{vmatrix}-1&0\\0&-1\end{vmatrix} 1001就可以进行图形的翻转。


🔗 前言
🔗 Python 高数列表
🔗 Python 高数篇 1
🔗 Python 高数篇 2
🔗 Python 高数篇 4

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

零蚀zero eclipse

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值