|
#摘至《Python 机器学习》,作者:Sebastian Raschaka, 机械工业出版社;
import numpy as np class Percentron(object): def __init__(self, eta = 0.1, n_iter = 10): self.eta = eta self.n_iter = n_iter def fit(self, X, y): self.w_ = np.zeros(1 + X.shape[1]) self.errors_ = [] for _ in range(self.n_iter): errors = 0 for xi, target in zip(X, y): update = self.eta * (target -self.predict(xi)) self.w_[1:] += update * xi self.w_[0] += update errors += int(update != 0) self.errors_.append(errors) return self def net_input(self, X): return np.dot(X, self.w_[1:]) + self.w_[0] def predict(self, X): return np.where(self.net_input(X) >= 0.0, 1, -1)
***********************************************************************************************************************
补充:
1. zeros():
https://docs.scipy.org/doc/numpy/reference/generated/numpy.zeros.html?highlight=zeros#numpy.zeros
2.shape(): https://docs.scipy.org/doc/numpy/user/quickstart.html#shape-manipulation
ndarray.shape
the dimensions of the array. This is a tuple of integers indicating the size of the array in each dimension. For a matrix with n rows and mcolumns, shape
will be (n,m)
. The length of the shape
tuple is therefore the number of axes, ndim
.
3. dot():
https://docs.scipy.org/doc/numpy/reference/generated/numpy.dot.html
4. where():
https://docs.scipy.org/doc/numpy/reference/generated/numpy.where.html?highlight=where#numpy.where
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-4-26 16:53
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社