Skip to content

Commit 3fd73e4

Browse files
📝
1 parent 07761d2 commit 3fd73e4

File tree

7 files changed

+133
-7
lines changed

7 files changed

+133
-7
lines changed

docs/docs/机器学习/传统算法/K均值算法.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
sidebar_position: 1
2+
sidebar_position: 2
33
title: K均值算法
44
---
55

docs/docs/机器学习/传统算法/支持向量机.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
sidebar_position: 7
2+
sidebar_position: 8
33
title: 支持向量机
44
---
55

docs/docs/机器学习/传统算法/朴素贝叶斯.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
sidebar_position: 6
2+
sidebar_position: 7
33
title: 朴素贝叶斯
44
---
55

docs/docs/机器学习/传统算法/线性回归.md

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
sidebar_position: 4
2+
sidebar_position: 3
33
title: 线性回归
44
---
55

@@ -29,6 +29,70 @@ title: 线性回归
2929

3030
我们预期中,理想效果应该是 0、0 好于 -4、4 好于 7、1。只有均方误差正确的反应了这一点。
3131

32+
通过误差的大小,我们可以慢慢修正我们的参数让线性拟合更好,导数可以反应数据变化的趋势,所以我们可以求导来修改参数。
33+
34+
```python showLineNumbers
35+
import numpy as np
36+
from matplotlib import pyplot as plt
37+
38+
39+
class Line:
40+
def __init__(self, data):
41+
self.w = 1
42+
self.b = 0
43+
self.learning_rate = 0.01
44+
self.fig, (self.ax1, self.ax2) = plt.subplots(2, 1)
45+
self.loss_list = []
46+
47+
def get_data(self, data):
48+
self.X = np.array(data)[:, 0]
49+
self.y = np.array(data)[:, 1]
50+
51+
def predict(self, x):
52+
return self.w * x + self.b
53+
54+
def train(self, epoch_times):
55+
for epoch in range(epoch_times):
56+
total_loss = 0
57+
for x, y in zip(self.X, self.y):
58+
y_pred = self.predict(x)
59+
# Calculate gradients
60+
gradient_w = -2 * x * (y - y_pred)
61+
gradient_b = -2 * (y - y_pred)
62+
# Update weights
63+
self.w -= self.learning_rate * gradient_w
64+
self.b -= self.learning_rate * gradient_b
65+
# Calculate loss
66+
loss = (y - y_pred) ** 2
67+
total_loss += loss
68+
epoch_loss = total_loss / len(self.X)
69+
self.loss_list.append(epoch_loss)
70+
if epoch % 10 == 0:
71+
print(f"loss: {epoch_loss}")
72+
self.plot()
73+
plt.ioff()
74+
plt.show()
75+
76+
def plot(self):
77+
plt.ion() # Enable interactive mode
78+
self.ax2.clear()
79+
self.ax1.clear()
80+
x = np.linspace(0, 10, 100)
81+
self.ax1.scatter(self.X, self.y, c="g")
82+
self.ax1.plot(x, self.predict(x), c="b")
83+
self.ax2.plot(list(range(len(self.loss_list))), self.loss_list)
84+
plt.show()
85+
plt.pause(0.1)
86+
87+
if __name__ == "__main__":
88+
# Input data
89+
data = [(1, 1), (1.8, 2), (2.5, 3), (4.2, 4), (5, 5), (6, 6), (7, 7)]
90+
s = Line(data)
91+
s.get_data(data)
92+
s.train(100)
93+
```
94+
95+
## 使用sklearn模块完成
3296

3397
```python showLineNumbers
3498
import numpy as np

docs/docs/机器学习/传统算法/逻辑回归.md

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
sidebar_position: 3
2+
sidebar_position: 4
33
title: 逻辑回归
44
---
55

@@ -31,6 +31,68 @@ $y = f(β0 + β1x1 + β2x2+… βnxn)$
3131

3232
逻辑回归分类器更接近 KNN,要解决多分类问题时,常常需要针对不同类别分别建立多个模型。
3333

34+
```python showLineNumbers
35+
import numpy as np
36+
from matplotlib import pyplot as plt
37+
38+
class Sline:
39+
def __init__(self, data):
40+
self.w = 0
41+
self.b = 0
42+
self.learning_rate = 0.1
43+
self.fig, (self.ax1, self.ax2) = plt.subplots(2, 1)
44+
self.loss_list = []
45+
46+
47+
def get_data(self, data):
48+
self.X = np.array(data)[:, 0]
49+
self.y = np.array(data)[:, 1]
50+
51+
def sigmoid(self, x):
52+
return 1 / (1 + np.exp(-(self.w * x + self.b)))
53+
54+
def train(self, epoch_times):
55+
for epoch in range(epoch_times):
56+
total_loss = 0
57+
for x, y in zip(self.X, self.y):
58+
y_pred = self.sigmoid(x)
59+
# w新 = w旧 - 学习率 * 梯度
60+
grad = -2 * (y - y_pred) * (1 - y_pred) * y_pred * x
61+
self.w = self.w - self.learning_rate * grad * x
62+
# b新 = b旧 - 学习率 * 梯度
63+
self.b = self.b - self.learning_rate * grad
64+
loss = (y - y_pred) ** 2
65+
total_loss += loss
66+
epoch_loss = total_loss / len(self.X)
67+
self.loss_list.append(epoch_loss)
68+
if epoch % 10 == 0:
69+
print(f"loss: {epoch_loss}")
70+
self.plot()
71+
plt.ioff()
72+
plt.show()
73+
74+
def plot(self):
75+
plt.ion() # 启用交互模式
76+
self.ax2.clear()
77+
self.ax1.clear()
78+
x = np.linspace(0, 10, 100)
79+
self.ax1.scatter(self.X, self.y, c="g")
80+
self.ax1.plot(x, self.sigmoid(x), c="b")
81+
self.ax2.plot(list(range(len(self.loss_list))), self.loss_list)
82+
plt.show()
83+
plt.pause(0.1)
84+
85+
if __name__ == "__main__":
86+
# 散点输入
87+
data = [(1, 0), (1.8, 0), (2.5, 0), (4.2, 1), (5, 1), (6, 1), (7, 1)]
88+
s = Sline(data)
89+
s.get_data(data)
90+
s.train(1000)
91+
92+
```
93+
94+
## 使用sklearn框架
95+
3496
```python showLineNumbers
3597
# 绘制逻辑回归的不同回归系数的sigmoid函数
3698

docs/docs/机器学习/传统算法/降维算法.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
sidebar_position: 2
2+
sidebar_position: 9
33
title: 降维算法
44
---
55

docs/docs/机器学习/传统算法/随机森林.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
sidebar_position: 8
2+
sidebar_position: 6
33
title: 随机森林
44
---
55

0 commit comments

Comments
 (0)