# 梯度下降求损失函数Minimizing cost functions with gradient descent

`损失函数与梯度，从上图可以看出梯度向下，`
`偏导数`
`可以看出计算样本y误差向量乘以样本x列向量,算出w需要使用所有的样本，然后再次迭代`
```import pandas as pd
import numpy as np
import matplotlib
import matplotlib.pyplot as plt

y = df.iloc[0:100, 4].values
y = np.where(y=='Iris-setosa',1,-1)
x = df.iloc[0:100, [0,2]].values

class Perceptron():
def __init__(self, eta, X, Y, N):
self.eta = eta
self.X = X
self.Y = Y
self.N = N
self.w = [0]*len(X[0])
self.w0 = 0
self.m = len(X)
self.n = len(X[0])
def output_y(self, x):
return np.dot(x,self.w)+self.w0
def training(self):
self.errors = []
for times in xrange(self.N):
delta_y = self.Y-self.output_y(self.X)
error = (delta_y**2).sum()/2.0
self.w0 += self.eta*delta_y.sum()
self.w += self.eta*np.dot(delta_y,self.X)
self.errors.append(error)

per = Perceptron(0.0001, x, y, 300)

per.training()

print per.w0,per.w

def f(x, y):
z = per.w0+np.dot(per.w,zip(x,y))
z = np.where(z>0,1,-1)
return z

n = 200

mx = np.linspace(4, 7.5, n)
my = np.linspace(0, 6, n)
# 生成网格数据
X, Y = np.meshgrid(mx, my)
fig, axes = plt.subplots(1,2)
axes0, axes1 = axes.flatten()
axes0.plot(per.errors, marker='o')
axes0.set_title('errors')
axes1.contourf(X, Y, f(X, Y), 2, alpha = 0.75, cmap = plt.cm.RdBu)
axes1.scatter(x[:,0][0:50], x[:, 1][0:50],s=80,edgecolors='r', marker='o')
axes1.scatter(x[:,0][50:100], x[:, 1][50:100], marker='x', color='g')
axes1.annotate(r'versicolor',xy=(5.5,4.5),xytext=(4.5,5.5),arrowprops=dict(arrowstyle='->', facecolor='blue'))
axes1.annotate(r'setosa',xy=(5.8,2),xytext=(6.5,3),arrowprops=dict(arrowstyle='->', facecolor='blue'))
fig.set_size_inches(15.5, 10.5)

plt.show()```
`数据标准化，收敛更快`
```x_std = np.copy(x)
x_std[:, 0] = (x[:,0]-x[:,0].mean())/x[:,0].std()
x_std[:, 1] = (x[:,1]-x[:,1].mean())/x[:,1].std()```
`图形如下：`

95 篇文章31 人订阅

0 条评论

## 相关文章

### Bagging算法(R语言)

Bagging算法（bootstrap aggregation）由Leo Breiman提出。是一种在每个自助样本集上建立基分类器，通过投票指派得到测试样本最终...

421100

37260

48560

44190

22560

28720

52850

31660

37760

### 图像二值化方法汇总介绍

ImageJ中图像二值化方法介绍 概述 二值图像分析在对象识别与模式匹配中有重要作用，同时也在机器人视觉中也是图像处理的关键步骤，选择不同图像二值化方法得到的结...

63350