机器学习基石作业1-Pocket_PLA 的c++实现

来源:互联网 发布:vmware 端口转发 编辑:程序博客网 时间:2024/05/19 16:34
#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];
}
void copy_weight(double* dest, double* sour, int length) {
for (int i = 0; i < length; i++)
dest[i] = sour[i];
}


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();
}




double test_PLA(vector<vector<double>> & test_Data,double *weight) {//返回某个权值在测试集上的正确率
double rate;
vector<vector<double>>::iterator it1;
vector<double>::iterator it2;
int mis = 0;
int total = test_Data.size();
for (it1 = test_Data.begin(); it1 != test_Data.end(); it1++) {
double *X=new double[6];
int i = 0;
for (it2 = (*it1).begin(), i = 0; it2 != (*it1).end(); it2++, i++)
X[i] = (*it2);
double sum = Sum(weight, X, 5);//计算wT*X
if (X[5] != sign(sum))
mis++;
delete X;
}
rate = mis / (1.0*total);
//cout << "rate: " << rate << endl;
return rate;
}
void Pocket_PLA(double* Pocket_weight,double* weight,vector < vector < double >> &data, int times) {
int step = 0;
int index = 0;
int n = data.size();
while (step<times)//限制迭代次数
{
double X[6];
int i = 0;
for (int i=0; i<6; i++)
X[i] = data[index][i];
double sum = Sum(weight, X, 5);//计算wT*X
if (X[5] != sign(sum)) {//找到错误点
double temp[5];
multiply(temp, X, 5, X[5]); //计算wT*X
add(weight, temp, 5);//暂存权值
if (test_PLA(data, weight) < test_PLA(data, Pocket_weight))//需要替换
copy_weight(Pocket_weight, weight, 5);
step++;//进行一次修正,修正次数+1
}
if (index == n - 1) index = 0;
else index++;
}
}
int main()
{
double sum = 0;
string filename = "train.txt";
vector<vector<double>> training_Data;//训练数据
getData(training_Data, filename);
double P_weight[2000][5] = {0};
for (int i = 0; i < 2000; i++) {
double weight[5] = { 0 };
random_shuffle(training_Data.begin(), training_Data.end());
Pocket_PLA(P_weight[i],weight,training_Data,50);
}
string filename1 = "test.txt";
vector<vector<double>> testing_Data;//测试数据
getData(testing_Data, filename1);
double rate[2000];
for (int i = 0; i < 2000; i++){
rate[i] = test_PLA(testing_Data, P_weight[i]);
}
for (int i = 0; i < 2000; i++){
sum += rate[i];
cout << "第" << i + 1 << "次rate: " << rate[i] << endl;
}
cout << "average error rate: " << sum/2000<< endl;
return 0;
}
阅读全文
0 0
原创粉丝点击