logistics regression 的C++实现

#include<iostream>
#include<vector>
#include<string>
#include<fstream>
#include<cmath>
using namespace std;

typedef struct features_label{
	vector<double> x;	//features
	double y;		//label
}data;

/*read examples from the file specified by path*/
void ReadExample(vector<data> &training_set, string path){
	training_set.clear();
	data a;

	int i=0;
	double x[6] = {0,0,0,0,0,0};	//modify the numbers of featrures here
	char temp[1024];
	path = "training_set.txt";
	ifstream infile;
	infile.open("training_set.txt");
	if(infile){
		while(infile.getline(temp, 1024)){
			cout<<temp<<endl;
			sscanf(temp,"%lf %lf %lf %lf %lf %lf", &x[0], &x[1], &x[2], &x[3], &x[4], &x[5]);
			a.x.push_back(1);		//features( let X0 = 1)
			a.x.push_back(x[0]);
			a.x.push_back(x[1]);
			a.x.push_back(x[2]);
			a.x.push_back(x[3]);
			a.x.push_back(x[4]);
			a.y = x[5];		//label
			training_set.push_back(a);
			a.x.clear();
			i++;
		}
	}
	cout<<training_set.size()<<endl;
}

/*Theta multiply by x*/
double ThetaMultiplyByX(vector<double> theta, vector<double> x){
	int i = 0;
	double temp = 0;
	for(i = 0;i<x.size();i++)		//sigma
		temp = temp + theta[i]*x[i];
	return temp;
}

/*sigmoid function*/
double Sigmoid(double x){
	double temp;
	temp = 1.0/(1+exp(-x));
	return temp;
}

/*likelihood of the parameters theta*/
double LTheta( vector<data> training_set, vector<double> theta){
	int i = 0;
	int m = training_set.size();
	double temp = 1,exp;
	for(i=0; i<m; i++){
		exp = Sigmoid(ThetaMultiplyByX(theta, training_set[i].x));
		cout<<training_set[i].y<<endl;
		temp *= pow(exp, training_set[i].y)*pow((1-exp), 1-training_set[i].y);
	}
	return temp;
}

/*logistic regression*/
void Logistic(vector<data> training_set, vector<double> &theta, double &alpha){
	int i = 0, j = 0;
	double oldval = 0, newval = 0;
	newval = LTheta(training_set, theta);
	while(abs(newval - oldval) > 0.001){		//gradient ascent                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
		for(j=0; j<theta.size(); j++)
			theta[j] += alpha*(training_set[i].y - Sigmoid(ThetaMultiplyByX(theta, training_set[i].x)))*training_set[i].x[j];
		oldval = newval;
		newval = LTheta(training_set, theta);
	}
}

int main(){

	vector<data> training_set; 
	vector<double> theta;
	double alpha = 0.1;
	int i = 0;

	ReadExample(training_set, "training_set.txt");
	cout<<training_set.size()<<endl;
	for(i=0; i<training_set[0].x.size(); i++)
		theta.push_back(1);
	Logistic(training_set, theta, alpha);
	for(i=0; i<theta.size(); i++)
		cout<<theta[i]<<endl;
	cout<<"hello world"<<endl;
	
	getchar();
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值