lijun255sysu的个人博客分享 http://blog.sciencenet.cn/u/lijun255sysu

博文

Python_机器学习_总结1:Rossenblatt感知器

已有 1546 次阅读 2018-8-20 16:04 |系统分类:科研笔记

 #摘至《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 



https://blog.sciencenet.cn/blog-3377553-1130222.html

上一篇:国自然Fail,Mark!
下一篇:Python_机器学习_总结1:基于Rossenblatt感知器分类Iris
收藏 IP: 61.145.151.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-3-19 19:48

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部