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∗((Feature∗Weight−Labels))
这里的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()
- 设置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()
-
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} ∣∣∣∣x′y′∣∣∣∣=∣∣∣∣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()
如果翻转的话直接点乘上 ∣ − 1 0 0 − 1 ∣ \begin{vmatrix}-1&0\\0&-1\end{vmatrix} ∣∣∣∣−100−1∣∣∣∣就可以进行图形的翻转。
🔗 前言
🔗 Python 高数列表
🔗 Python 高数篇 1
🔗 Python 高数篇 2
🔗 Python 高数篇 4