感知机是古老的统计学习方法,主要应用于二类线性可分数据,策略是在给定的超平面上对误差点进行纠正,从而保证所有的点都是正确可分的。

用到的方法是随机梯度下降法,由于是线性可分的,可保证最终在有限步内收敛。具体可参考李航的《统计学习方法》

#include<iostream>
#include<algorithm>
#include<vector>
#include<fstream> using namespace std; typedef vector<double> feature;
typedef int label; class PercepMachine
{
private:
vector<feature> dataset;
vector<label> labelset;
double learningrate;
double vector_multi (const feature &x, const feature &y)
{
double sum = 0.0;
for (int i = 0; i != x.size(); ++i)
{
sum += x[i] * y[i];
}
return sum;
}
feature vector_multi(double x, const feature &y)
{
feature temp;
for (int i = 0; i != y.size(); ++i)
{
temp.push_back(x*y[i]);
}
return temp;
}
feature vector_add(const feature &x, const feature &y)
{
feature temp(0);
for (int i = 0; i != x.size(); ++i)
{
temp.push_back(x[i] + y[i]);
}
return temp;
}
public:
feature w;
double b;
PercepMachine(vector<feature> &traindata, vector<label> &trainlabel, feature &startw, double startb, double rate) :dataset(traindata), labelset(trainlabel), w(startw), b(startb), learningrate(rate){}
void calculate_percep();
}; void PercepMachine::calculate_percep()
{
vector<int> flag(dataset.size(), 1);
while (find(flag.begin(), flag.end(), 1) != flag.end())
{
for (int i = 0; i != dataset.size(); ++i)
{
double multi = vector_multi(dataset[i], w);
if ((multi + b)*labelset[i] <= 0)//有误分类点
{
flag[i] = 1;
w = vector_add(w, vector_multi(learningrate*labelset[i], dataset[i]));
b = b + learningrate*labelset[i]; }
else
{
flag[i] = 0;
}
}
}
} int main()
{
ifstream fin("data.txt");
if (!fin)
{
cout << "can not open the file data.txt" << endl;
exit(1);
}
/* input the dataSet 假设是平面数据,存储在txt文件中3列多行,最后一列存储类别信息1或-1*/
int feature_dimension = 2; vector<feature> traindata;
vector<label> trainlabel;
while (!fin.eof())
{
feature temp_data;
double temp;
for (int i = 0; i < feature_dimension; ++i)
{
fin >> temp;
temp_data.push_back(temp);
}
traindata.push_back(temp_data);
label mylabel;
fin >> mylabel;
trainlabel.push_back(mylabel);
}
feature startw(2,1);
double startb = 1.0;
double rate = 0.5; PercepMachine permachine(traindata, trainlabel, startw, startb, rate);
permachine.calculate_percep();
cout << "w=" << "("<<permachine.w[0] << " " << permachine.w[1]<<")" << endl;
cout << "b=" << permachine.b << endl; return 0; }

统计学习中感知机的C++代码的更多相关文章

  1. 【StatLearn】统计学习中knn算法实验(2)

    接着统计学习中knn算法实验(1)的内容 Problem: Explore the data before classification using summary statistics or vis ...

  2. 【StatLearn】统计学习中knn算法的实验(1)

    Problem: Develop a k-NN classifier with Euclidean distance and simple voting Perform 5-fold cross va ...

  3. 深度学习中正则化技术概述(附Python代码)

    欢迎大家关注我们的网站和系列教程:http://www.tensorflownews.com/,学习更多的机器学习.深度学习的知识! 磐石 介绍 数据科学研究者们最常遇见的问题之一就是怎样避免过拟合. ...

  4. 强化学习中REIINFORCE算法和AC算法在算法理论和实际代码设计中的区别

    背景就不介绍了,REINFORCE算法和AC算法是强化学习中基于策略这类的基础算法,这两个算法的算法描述(伪代码)参见Sutton的reinforcement introduction(2nd). A ...

  5. [译]针对科学数据处理的统计学习教程(scikit-learn教程2)

    翻译:Tacey Wong 统计学习: 随着科学实验数据的迅速增长,机器学习成了一种越来越重要的技术.问题从构建一个预测函数将不同的观察数据联系起来,到将观测数据分类,或者从未标记数据中学习到一些结构 ...

  6. 4.机器学习——统计学习三要素与最大似然估计、最大后验概率估计及L1、L2正则化

    1.前言 之前我一直对于“最大似然估计”犯迷糊,今天在看了陶轻松.忆臻.nebulaf91等人的博客以及李航老师的<统计学习方法>后,豁然开朗,于是在此记下一些心得体会. “最大似然估计” ...

  7. 卷积在深度学习中的作用(转自http://timdettmers.com/2015/03/26/convolution-deep-learning/)

    卷积可能是现在深入学习中最重要的概念.卷积网络和卷积网络将深度学习推向了几乎所有机器学习任务的最前沿.但是,卷积如此强大呢?它是如何工作的?在这篇博客文章中,我将解释卷积并将其与其他概念联系起来,以帮 ...

  8. 记录Python学习中的几个小问题

    记录Python学习中的几个小问题,和C#\JAVA的习惯都不太一样. 1.Django模板中比较两个值是否相等 错误的做法 <option value="{{group.id}}&q ...

  9. 深度学习中的Data Augmentation方法(转)基于keras

    在深度学习中,当数据量不够大时候,常常采用下面4中方法: 1. 人工增加训练集的大小. 通过平移, 翻转, 加噪声等方法从已有数据中创造出一批"新"的数据.也就是Data Augm ...

随机推荐

  1. Qt之布局管理--基本布局

    Qt提供的布局类以及他们之间的继承关系QLayout-----QGirdLayout | ---QBoxLayout----QHBoxLayout | --QVBoxLayout----------- ...

  2. C#开发Windows服务 入门

    Microsoft Windows 服务(即,以前的 NT 服务)使您能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序. 服务可以在计算机启动时自动启动,可以暂停和重新启动而且 ...

  3. ABP dynamic API

    打开ABP的事例项目SimpleTaskSystem.WebSpaAngular 中LayoutView <!-- Dynamic scripts of ABP system (They are ...

  4. JS数组操作示意图(shift,unshift,pop,push)

    shift:删除原数组第一项,并返回删除元素的值:如果数组为空则返回undefined var a = [1,2,3,4,5]; var b = a.shift(); //a:[2,3,4,5] b: ...

  5. font-family:“微软雅黑” OR font-family:Microsoft Yahei

    sublime对中文编码支持的不好,可以考虑用后者.

  6. c++中的<<函义

    1.一个是左移运算:x = 4<< 2; 2.输出流运算:cout <<x;//X的值输出流到设备中.

  7. ORACLE表的恢复

         对误删的表,只要没有使用PURGE永久删除选项,那么从flash back区恢复回来希望是挺大的.一般步骤有: 1.从flash back里查询被删除的表     select * from ...

  8. spring 在静态工具类中使用注解注入bean

    /** * @author: jerry * @Email: * @Company: * @Action: 日志处理工具类 * @DATE: 2016-9-19 */ @Component//泛指组件 ...

  9. easyui dialog iframe

    function toGrant(obj,url,showMsg) {                                        var dialog=$('#dlg_grant' ...

  10. Linux内核分析:dup、dup2的实现

    一.首先需要看一下这两个函数的作用: #include <unistd.h> int dup(int oldfd); int dup2(int oldfd, int newfd); 根据m ...