2、Logistic Regression求解classification问题 c代码
2016-02-27 10:56
330 查看
#include "stdio.h"
#include "math.h"
double x[6][4]={{1.0,47.0,76.0,24.0}, //include x0=1
{1.0,46.0,77.0,23.0},
{1.0,48.0,74.0,22.0},
{1.0,34.0,76.0,21.0},
{1.0,35.0,75.0,24.0},
{1.0,34.0,77.0,25.0},
};
double y[]={1.0,1.0,1.0,0.0,0.0,0.0};
double theta[]={1.0,1.0,1.0,1.0}; // include theta0
int i,j,k;
double ez,gz,gh;
double fun_g(double z){//重点是定义函数g(z),之后可以调用
ez= pow(2.718,z);
gz=ez/(1+ez);
return gz;
}
int main(){
double fun_g(double z);
for(i=1;i<1000;i++){
for(j=0;j<6;j++){
double h=0.0;
for(k=0;k<4;k++){//记住j,k都从0开始
h=h+theta[k]*x[j][k];
}
gh=fun_g(h) ;
double error=0.0;
error=y[j]-gh;
for(k=0;k<4;k++){
theta[k]=theta[k]+0.01*error*x[j][k];
}
printf("%lf %lf %lf %lf\t",theta[0],theta[1],theta[2],theta[3]);
}
}
}
logistics回归和线性回归的参数迭代学习规则形式完全一样,但意义不同,logistics本质上是梯度上升,logistics回归是凹函数,找局部最大值,一定是全局最大值
logistics回归用来处理分类问题,处理离散数据
http://blog.csdn.net/pennyliang/article/details/7045372
【这是别人Logistic Regression求解classification问题 的代码】
Given Summed Input:
x =
Instead of threshold, and fire/not fire,
we could have continuous output y according to the sigmoid function:设定了一个sigmoid函数
Note e and its properties.
As x goes to minus infinity, y goes to 0 (tends not to fire).
As x goes to infinity, y goes to 1 (tends to fire):
At x=0, y=1/2
#include "math.h"
double x[6][4]={{1.0,47.0,76.0,24.0}, //include x0=1
{1.0,46.0,77.0,23.0},
{1.0,48.0,74.0,22.0},
{1.0,34.0,76.0,21.0},
{1.0,35.0,75.0,24.0},
{1.0,34.0,77.0,25.0},
};
double y[]={1.0,1.0,1.0,0.0,0.0,0.0};
double theta[]={1.0,1.0,1.0,1.0}; // include theta0
int i,j,k;
double ez,gz,gh;
double fun_g(double z){//重点是定义函数g(z),之后可以调用
ez= pow(2.718,z);
gz=ez/(1+ez);
return gz;
}
int main(){
double fun_g(double z);
for(i=1;i<1000;i++){
for(j=0;j<6;j++){
double h=0.0;
for(k=0;k<4;k++){//记住j,k都从0开始
h=h+theta[k]*x[j][k];
}
gh=fun_g(h) ;
double error=0.0;
error=y[j]-gh;
for(k=0;k<4;k++){
theta[k]=theta[k]+0.01*error*x[j][k];
}
printf("%lf %lf %lf %lf\t",theta[0],theta[1],theta[2],theta[3]);
}
}
}
logistics回归和线性回归的参数迭代学习规则形式完全一样,但意义不同,logistics本质上是梯度上升,logistics回归是凹函数,找局部最大值,一定是全局最大值
logistics回归用来处理分类问题,处理离散数据
http://blog.csdn.net/pennyliang/article/details/7045372
【这是别人Logistic Regression求解classification问题 的代码】
Given Summed Input:
x =
Instead of threshold, and fire/not fire,
we could have continuous output y according to the sigmoid function:设定了一个sigmoid函数
Note e and its properties.
As x goes to minus infinity, y goes to 0 (tends not to fire).
As x goes to infinity, y goes to 1 (tends to fire):
At x=0, y=1/2
相关文章推荐
- Java中的向前引用
- C语言实现logistic回归
- springmvc4 mybatis 整合 框架源码
- java 小概念
- [国嵌攻略][074][系统调用方式文件编程]
- C++基础总结(1)--基本数据类型范围
- C++基础总结(1)--基本数据类型范围
- 多线程socket编程--聊天程序
- 【leetcode】【9】Palindrome Number
- [2016/02/27] PAT(Basic Level) Problem 1001-1050 JAVA略解
- Java类文件结构(Class文件)
- 1、梯度下降c代码
- 当项目使用SpringMvc时拦截器的注意事项
- Python实现简单多线程任务队列
- Java应用场景的解决框架资源大全
- java一个类获取另一个类的变量值
- 第一行代码-9.5 服务的更多技巧
- Thinking in Java---多线程仿真:银行出纳员仿真+饭店仿真+汽车装配工厂仿真
- 搞笑代码注释
- Spring Security 3.x完整入门配置教程及其代码下载