`
daweibalong
  • 浏览: 44687 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

Machine Learning系列实验--Logistic function解决分类问题

阅读更多

 

分类问题的值是离散的,区别于之前的线性回归问题。本次采用Logistic回归来解决分类问题,实验还是参考了pennyliang的http://blog.csdn.net/pennyliang/article/details/7045372#comments
Logistic回归问题的Matchine <wbr>Learning系列实验--Logistic <wbr>function解决分类问题,写出likelihood functionMatchine <wbr>Learning系列实验--Logistic <wbr>function解决分类问题


Matchine <wbr>Learning系列实验--Logistic <wbr>function解决分类问题,目标是使得l()最大化。可采用梯度上升方法进行迭代,但不同的是求最大值。


#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;

double h(double* x,double* q)
{
	double temp = q[0] * x[0] + q[1] * x[1] + q[2] * x[2] + q[3] * x[3];
	double e = pow(2.718281828, temp);
	return e / (e + 1);
}

void classifier(double* pre)
{
	double x[6][4]={{1,47,76,24},   
		{1,46,77,23},  
		{1,48,74,22},  
		{1,34,76,21},  
		{1,35,75,24},  
		{1,34,77,25},  
	};  

	double y[]={1,1,1,0,0,0};  
	double theta[]={1,1,1,1};

	int i, j, k;
	double l;
	for (i = 0; i < 10000; i++)
	{
		for (j=0; j<4; j++)
		{
			double sum=0;
			for (k = 0; k < 6; k++)
			{
				sum += (y[k] - h(x[k], theta)) * x[k][j];
			}
			theta[j] += 0.001 * sum;
			cout << theta[j] << " ";
		}
		cout << endl;
		
		l = 0;
		for (j = 0; j < 6; j++)
		{
			l += y[j] * log(h(x[j], theta)) + (1 - y[j]) * log(1 - h(x[j], theta));
		}
		//cout<< l << endl;
	}
	cout << i << endl;
	cout << h(pre, theta) << endl;
	cout << l << endl;

}
int main(void)
{
	double pre[] = {1, 48 ,74, 22};
	classifier(pre);
	return 0;
}
 

试验中选择了一个学习样本进行测试,得到的h(x)=0.999984, 相似的极高,若填入的测试数据为其他,可根据h(x)值的大小进行判断y值是0还是1.
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics