机器学习基石PLA算法c++语言实现
2015-07-21 22:32
621 查看
其中的Item.label为数据中的判断结果,例如在授予信用卡的时候为 是 或者 否,其算法的目的就在于确定其中wight中个分量的值
#include <fstream> #include <iostream> #include <vector> using namespace std; struct Item{ int x0 = 1; //需要C++11 double x1, x2, x3, x4; int label; }; struct Wight{ double w0, w1, w2, w3, w4; }Wit0 = { 0, 0, 0, 0, 0 }; ////////////////////////////////////////////////////////////////////////////////////// int sign(double x){ if (x>0) return 1; else if (x<0) return -1; else return 0; } ////////////////////////////////////////////////////////////////////////////////////// double DotPro(Item item, Wight wit){ return item.x0*wit.w0 + item.x1*wit.w1 + item.x2*wit.w2 + item.x3*wit.w3 + item.x4*wit.w4; } ////////////////////////////////////////////////////////////////////////////////////// Item NumPro(int k, Item item){ Item NewItem; NewItem.x0 = item.x0*k; NewItem.x1 = item.x1*k; NewItem.x2 = item.x2*k; NewItem.x3 = item.x3*k; NewItem.x4 = item.x4*k; return NewItem; } ////////////////////////////////////////////////////////////////////////////////////// Wight WightAnd(Item item, Wight wit){ Wight NewWigth; NewWigth.w0 = item.x0 + wit.w0; NewWigth.w1 = item.x1 + wit.w1; NewWigth.w2 = item.x2 + wit.w2; NewWigth.w3 = item.x3 + wit.w3; NewWigth.w4 = item.x4 + wit.w4; return NewWigth; } ////////////////////////////////////////////////////////////////////////////////////// void main() { ofstream output("D:/data2.txt"); ifstream input("D:/data0.txt"); vector<Item> data; Item temp; while (input >> temp.x1 >> temp.x2 >> temp.x3 >> temp.x4 >> temp.label){ data.push_back(temp); } vector<Item>::iterator it; Wight wit = Wit0; for (it = data.begin(); it != data.end(); it++) { if ((*it).label != sign(DotPro(*it, wit))){ wit = WightAnd(NumPro((*it).label, *it), wit); it = data.begin(); } } cout << wit.w0 << " " << wit.w1 << " " << wit.w2 << " " << wit.w3 << " " << wit.w4 << endl; /* 测试数据 for (it = data.begin(); it != data.end(); it++) { output << sign(DotPro(*it, wit)) << endl; } */ }
相关文章推荐
- C++中的引用
- C语言输入下标数,把下标值改成0并放到最后,再依次打印。
- 在Visual C++中使用内联汇编
- C++ Primer 复习杂记(第二章)
- int to string 整型转化为字符串( C语言接口与实现:创建可重用软件的技术)
- c017--c语言文件操作
- c016---c语言中的预处理
- C语言if语句判断闰年平年
- C语言利用for循环实现1,2,3,4这四个整数互不相同且不重复的三位数
- 基于C语言EOF与getchar()的使用详解【ZT】
- c语言编程
- 黑马程序员----C语言函数
- C++设计模式之 -- 桥接模式 (Bridge)
- c++函数初探
- 多态在 Java 和 C++ 编程语言中的实现比较
- C++继承问题
- java 它 引用(基本类型的包装,构造函数和析构函数c++不同)
- 梅尔频率倒谱系数(MFCC)的提取过程与C++代码实现
- 【C++】容器类
- C语言基础知识小总结(2)