实现梯度下降算法
在该 Lab 中,我们将实现梯度下降算法的基本函数,以便在小数据集中查找数据边界。 首先,我们将从一些函数开始,帮助我们绘制和可视化数据。
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# Some helper functions for plotting and drawing lines
def plot_points(X, y):
admitted = X[np.argwhere (y==1)]
rejected = X[np.argwhere (y==0)]
plt.scatter([s[0][0] for s in rejected], [s[0][1] for s in rejected], s = 25, color = 'blue', edgecolor = 'k')
plt.scatter([s[0][0] for s in admitted], [s[0][1] for s in admitted], s = 25, color = 'red', edgecolor = 'k')
def display(m, b, color= 'g--'):
plt.xlim(-0.05, 1.05)
plt.ylim(-0.05, 1.05)
x = np.arange(-10, 10, 0.1)
plt.plot(x, m*x+b, color)
读取和绘制数据
data = pd.read_csv('data.csv', header=None)
X = np.array(data[[0,1]])
y = np.array(data[2])
plot_points(X,y)
plt.show()
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CRQeJqWx-1573052370819)(output_3_0.png)]
实现基本函数
Sigmoid 激活函数
待办: 实现基本函数
现在轮到你练习了。 如之前所述,实现以下基本函数。
- Sigmoid 激活函数
σ ( x ) = 1 1 + e − x \sigma(x) = \frac{1}{1+e^{-x}} σ(x)=1+