【感知机模型】手写代码训练 / 使用sklearn的Perceptron模块训练
读取原始数据
import pandas as pd
import numpy as np
in_data = pd.read_table('./origin-data/perceptron_15.dat', sep='\s+', header=None)
X_train = np.array(in_data.loc[:,[0,1,2,3]])
y_train = np.array(in_data[4])
训练感知机模型
class MyPerceptron:
def __init__(self):
self.w = None
self.b = 0
self.l_rate = 1
def fit(self, X_train, y_train):
#用样本点的特征数更新初始w,如x1=(3,3)T,有两个特征,则self.w=[0,0]
self.w = np.zeros(X_train.shape[1])
i = 0
while i < X_train.shape[0]:
X = X_train[i]
y = y_train[i]
# 如果y*(wx+b)≤0 说明是误判点,更新w,b
if y * (np.dot(self.w, X) + self.b) <= 0:
self.w += self.l_rate * np.dot(y, X)
self.b += self.l_rate * y
i=0 #如果是误判点,从头进行检测
else:
i+=1
from sklearn.linear_model import Perceptron
# 使用sklearn中的Perceptron类训练
perceptron = Perceptron()
time1 = datetime.datetime.now()
perceptron.fit(X_train, y_train)
time2 = datetime.datetime.now()
print("共用时:", (time2-time1).microseconds, "微秒")
print(perceptron.coef_)
print(perceptron.intercept_)
共用时: 4769 微秒
[[ 2.9686576 -1.513057 2.211151 4.227677 ]]
[-3.]
# 使用自己写的MyPerceptron类训练
perceptron = MyPerceptron()
time1 = datetime.datetime.now()
perceptron.fit(X_train, y_train)
time2 = datetime.datetime.now()
print("共用时:", (time2-time1).microseconds, "微秒")
print(perceptron.w)
print(perceptron.b)
共用时: 12479 微秒
[ 3.6161856 -2.013502 3.123158 5.49830856]
-4
【感知机模型】手写代码训练 / 使用sklearn的Perceptron模块训练的更多相关文章
- ClownFish:比手写代码还快的通用数据访问层
http://www.cnblogs.com/fish-li/archive/2012/07/17/ClownFish.html 阅读目录 开始 ClownFish是什么? 比手写代码还快的执行速度 ...
- 手写代码自动实现自动布局,即Auto Layout的使用
手写代码自动实现自动布局,即Auto Layout的使用,有需要的朋友可以参考下. 这里要注意几点: 对子视图的约束,若是基于父视图,要通过父视图去添加约束. 对子视图进行自动布局调整,首先对UIVi ...
- 如果选择构建ui界面方式,手写代码,xib和StoryBoard间的博弈
代码手写UI这种方法经常被学院派的极客或者依赖多人合作的大型项目大规模使用. 大型多人合作项目使用代码构建UI,主要是看中纯代码在版本管理时的优势,检查追踪改动以及进行代码合并相对容易一些. 另外,代 ...
- .netER的未来路,关于基础是否重要和应该自己手写代码吗?
http://www.cnblogs.com/onepiece_wang/p/5558341.html#!comments 引用"基础知识的学习,一开始可能是背书,但是在后续若干年的工作过程 ...
- 手写代码UI,xib和StoryBoard间的的优劣比较
在UI制作方面,逐渐分化三种主要流派:使用代码手写UI:使用单个xib文件组织viewController或者view:使用StoryBoard来通过单个或很少的几个文件构建UI.三种方式各有优劣,也 ...
- UI到底应该用xib/storyboard完成,还是用手写代码来完成?
UI到底应该用xib/storyboard完成,还是用手写代码来完成? 文章来源:http://blog.csdn.net/libaineu2004/article/details/45488665 ...
- 2019前端面试系列——JS高频手写代码题
实现 new 方法 /* * 1.创建一个空对象 * 2.链接到原型 * 3.绑定this值 * 4.返回新对象 */ // 第一种实现 function createNew() { let obj ...
- Appium初始化设置:手写代码连接手机、appium-desktop连接手机
一.包名获取的三种方式 1)找开发要2)mac使用命令:adb logcat | grep START win使用命令:adb logcat | findstr START 或者可以尝试使用第3条命令 ...
- gcd手写代码及STL中的使用方法
一.手写代码 inline int gcd(int x,int y){ if(y==0) return x; else return(gcd(y,x%y)); } 二.STL中的使用方法 注:在STL ...
随机推荐
- 文件传输基础----Java IO流
编码问题 一个很著名的奇怪现象:当你在 windows 的记事本里新建一个文件,输入"联通"两个字之后,保存,关闭,然后再次打开,你会发现这两个字已经消失了,代之的是几个乱码!呵呵 ...
- mycat - 数据库中间件 学习记录4
mycat的配置 cacheservice.properties:路由缓存相关配置文件 index_to_charset.properties:字符集映射关系 rule.xml:分片规则 schema ...
- HYSBZ_2002_分块
http://www.lydsy.com/JudgeOnline/problem.php?id=2002 分块基础题,初次接触,很巧妙. OJ好像挂了,没法提交. #include<iostre ...
- CCF_ 201409-3_字符串匹配
水. #include<cstdio> #include<iostream> #include<cstring> using namespace std; int ...
- springcloud微服务feign组件报错
今天在用springcloud搭建微服务时,利用feign做通讯组件,结果报错 java.lang.IllegalStateException: Failed to introspect Class ...
- python OpenCV安装
linux系统 yum install -y libSM.x86_64 libXext.x86_64 libXrender.x86_64 pip install numpy Matplotlib pi ...
- h5笔记1
1.HTML中不支持 空格.回车.制表符,它们都会被解析成一个空白字符 2.适用于大多数 HTML 元素的属性: class 为html元素定义一个或多个类名(classname)(类名从样式文件引入 ...
- 在debian10启动器中添加自定义应用
首先要添加一个desktop类型的文件,搜索一下即可 若将desktop文件放在/usr/share/applicatios/中,需要执行update-desktop-database使新添加的应用生 ...
- linux中文件处理命令
目录 touch cat more less head tail touch 解释 命令名称:touch 命令所在路径:/bin/touch 执行权限:所有用户 功能描述:创建空文件 语法 touch ...
- C#个推SDK推送安卓+iOS
下载个推SDK,找到这两个dll直接引用. using引用 using com.gexin.rp.sdk.dto; using com.igetui.api.openservice; using co ...