最小二乘法回归参数梯度
代码
import pandas as pd
import numpy as np
df = pd.read_csv('https://archive.ics.uci.edu/ml/'
'machine-learning-databases/iris/iris.data', header = None)
import matplotlib.pyplot as plt
y = df.iloc[0:400,4].values
y = np.where(y == 'Iris-setosa', -1, 1)
X = df.iloc[0:400, [0,2]].values
class AdaLineGD(object):
def __init__(self,eta = 0.01, n_iter = 100):
self.eta = eta
self.n_iter = n_iter
def net_input(self,X):
return np.dot(X, self.w[1:]) + self.w[0]
def fit(self, X, y):
self.w = np.zeros(1 + X.shape[1])
self.cost = []
for _ in range(self.n_iter):
output = self.net_input(X)
error = y - output
self.w[1:] += self.eta * X.T.dot(error)
self.w[0] += self.eta * error.sum()
cost = (error ** 2).sum()/