机器学习基石作业1-17-PLA 的c++实现
来源:互联网 发布:nginx添加lua模块 编辑:程序博客网 时间:2024/05/19 17:07
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
#include<vector>
#include<algorithm>
using namespace std;
double sign(double x) {
if (x > 0)
return 1;
else
return -1;
}
double Sum(double* a, double* b, int length) {
double sum = 0;
for (int i = 0; i < length; i++)
sum += a[i] * b[i];
return sum;
}
void add(double *v1, double *v2, int demension) {
int i;
for (i = 0; i < demension; i++)
v1[i] += v2[i];
}
void multiply(double *result, double *v, int demension, double num) {//向量的数乘
int i;
for (i = 0; i < demension; i++)
result[i] = num * v[i];
}
int PLA(vector < vector < double >> &data) {
vector<vector<double>>::iterator it1;//外部迭代
vector<double>::iterator it2;//内部迭代
int step = 0;
double W[5] = { 0 };
bool noFinished = true;
while (noFinished)
{
noFinished = false;
for (it1 = data.begin(); it1 != data.end(); it1++) //迭代每一个训练样本
{
double X[6];
int i = 0;
for (it2 = (*it1).begin(), i = 0; it2 != (*it1).end(); it2++, i++)
X[i] = (*it2);
double sum = Sum(W, X, 5);//计算wT*X
if (X[5] != sign(sum)) {//找到错误点
noFinished = true;//标记未完成
double temp[5];
multiply(temp, X, 5, 0.5*X[5]);
add(W, temp, 5);//更新权值
step++;//进行一次修正,修正次数+1
}
}
}
//cout << "step: " << step << endl;
return step;
}
void getData(vector<vector<double>> &data,string filename) {
ifstream file;
string line;
double txt[5];
double x0 = 1;
file.open(filename);
if (!file.is_open()) {
cout << "Cannot open the file !\n";
exit(EXIT_FAILURE);
}
while (getline(file, line)) {//读取数据到字符串中
stringstream ss(line);
ss >> txt[0] >> txt[1] >> txt[2] >> txt[3] >> txt[4];
vector<double> temp;
temp.push_back(x0);//将x0=1写入
for (int i = 0; i < 5; i++)
temp.push_back(txt[i]);
data.push_back(temp);
}
file.close();
}
int main()
{
string filename = "train.txt";
vector<vector<double>> data;//存储训练数据
getData(data, filename);
//PLA(data);
double sum = 0;
for (int i = 0; i < 2000; i++) {
random_shuffle(data.begin(), data.end());
sum += PLA(data);
}
cout << "average step: " << sum / 2000 << endl;
return 0;
}
#include<fstream>
#include<string>
#include<sstream>
#include<vector>
#include<algorithm>
using namespace std;
double sign(double x) {
if (x > 0)
return 1;
else
return -1;
}
double Sum(double* a, double* b, int length) {
double sum = 0;
for (int i = 0; i < length; i++)
sum += a[i] * b[i];
return sum;
}
void add(double *v1, double *v2, int demension) {
int i;
for (i = 0; i < demension; i++)
v1[i] += v2[i];
}
void multiply(double *result, double *v, int demension, double num) {//向量的数乘
int i;
for (i = 0; i < demension; i++)
result[i] = num * v[i];
}
int PLA(vector < vector < double >> &data) {
vector<vector<double>>::iterator it1;//外部迭代
vector<double>::iterator it2;//内部迭代
int step = 0;
double W[5] = { 0 };
bool noFinished = true;
while (noFinished)
{
noFinished = false;
for (it1 = data.begin(); it1 != data.end(); it1++) //迭代每一个训练样本
{
double X[6];
int i = 0;
for (it2 = (*it1).begin(), i = 0; it2 != (*it1).end(); it2++, i++)
X[i] = (*it2);
double sum = Sum(W, X, 5);//计算wT*X
if (X[5] != sign(sum)) {//找到错误点
noFinished = true;//标记未完成
double temp[5];
multiply(temp, X, 5, 0.5*X[5]);
add(W, temp, 5);//更新权值
step++;//进行一次修正,修正次数+1
}
}
}
//cout << "step: " << step << endl;
return step;
}
void getData(vector<vector<double>> &data,string filename) {
ifstream file;
string line;
double txt[5];
double x0 = 1;
file.open(filename);
if (!file.is_open()) {
cout << "Cannot open the file !\n";
exit(EXIT_FAILURE);
}
while (getline(file, line)) {//读取数据到字符串中
stringstream ss(line);
ss >> txt[0] >> txt[1] >> txt[2] >> txt[3] >> txt[4];
vector<double> temp;
temp.push_back(x0);//将x0=1写入
for (int i = 0; i < 5; i++)
temp.push_back(txt[i]);
data.push_back(temp);
}
file.close();
}
int main()
{
string filename = "train.txt";
vector<vector<double>> data;//存储训练数据
getData(data, filename);
//PLA(data);
double sum = 0;
for (int i = 0; i < 2000; i++) {
random_shuffle(data.begin(), data.end());
sum += PLA(data);
}
cout << "average step: " << sum / 2000 << endl;
return 0;
}
阅读全文
0 0
- 机器学习基石作业1-17-PLA 的c++实现
- 机器学习基石 作业1 实现PLA和Pocket算法
- 机器学习基石作业一PLA算法
- 机器学习基石—作业1(15-20题PLA编程)
- 机器学习基石作业1-Pocket_PLA 的c++实现
- 机器学习基石PLA算法c++语言实现
- PLA算法Java实现——机器学习基石
- 机器学习基石学习笔记(1)-PLA
- 机器学习基石第一讲:PLA
- 机器学习基石 2.3 Guarantee of PLA
- 机器学习基石 2.4 More about PLA
- 机器学习基石---第二周PLA
- 《机器学习基石》作业一第17题的程序
- 台湾大学林轩田教授机器学习基石课程理解及python实现----PLA
- Coursera课程-机器学习基石作业一Q18-Q20(pocket on D算法 for PLA/C++ edition)
- PLA(Perceptron Learning Algorithm)--机器学习基石笔记
- 林轩田机器学习基石——Guarantee of PLA
- 机器学习基石2-2 PLA(Perceptron Learning Algorithm)
- Eclipse运行性能调优(含eclipse.ini)
- 使用Jquery插件jsonview来展示json数据
- STM32+W5500使用IAR FOR ARM建立工程详细到编译过程
- 2017.12.16
- 04_张孝祥_Java多线程_传统线程同步通信技术
- 机器学习基石作业1-17-PLA 的c++实现
- 小象学院2017年最新基于Python数据分析班升级版高清精品推荐
- 高斯混合模型聚类算法和K-Means聚类算法
- open layer4加载wms
- vue click ios11 点击第二次才有效,vue click ios11 点击第二次才有效
- yarn资源管理最佳实践
- onLayout笔记(mTop,layout,setFrame,getMeasureWidth,getWidth)
- (1)SpringCloud 整合Python
- 算法学习中遇到的函数(并不全...