热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Logistic回归模型(C++代码实现)

Logistic回归主要针对输入的数据是多个,输出则是有限的数值型,多为2个分类。涉及到以下方面:1.输出yw0+w1*x1+w2*x2+..(x1,x2,是样本的

Logistic回归主要针对输入的数据是多个,输出则是有限的数值型,多为2个分类。

涉及到以下方面:

1. 输出y = w0+w1*x1+w2*x2+..... (x1,x2,...是样本的属性值,为连续型的变量,w0,w1,w2,...为所要求的参数,y为有限的数值型变量,表示样本所属类别)。

2. logistic模型: 1/(1+exp(-z)),其中z= w0+w1*x1+w2*x2+..... 。

3.算法实现:

    w初始化为1;

    alph = 0.1; //设置步长,需根据情况逐步调整

    i = 0;

    while( i<样本数量)

          zi = w0+w1*xi1+w2*xi2+..... 

         h = 1/(1+exp(-zi));

         error = yi-h;

         while(...)

               wj = wj+alph *error*xij; // j表示第j个属性

          end

    end

以上算法过程在样本量比较小的时候可以实现,在样本量非常大的时候,需要考虑采用随机梯度下降法,即随机从总的样本的选出小的样本集来用于迭代过程(可以百度相关资料)。

本文主要采用了梯度下降法完成了参数值优化过程。以下程序主要将3中算法实现。主要包含main.h 和 main.cpp两个文件

测试结果发现预测的准确率可以到80%左右。但感觉这和参数的调整有很大关系,样本量还是太小(总样本量198,训练集:150,测试集:48),这里比较简便,不包含校准数据集,另外结果存在一些欠拟合的现象。


main.h

/*************
Logistic Regression( logistic 回归 )using newton gradient descent

CopyRight 2016/8/21 xukaiwen
All Rights Reserved

**************/

#ifndef MAIN_H
#define MAIN_H

#include "stdio.h"
#include "stdlib.h"
#include "iostream"
#include "string"
#include "string.h"
#include
#include

#include "math.h"

using namespace std;

#define maxClassLabelNum 10;
int curLabelNum = 0;


const double alph = 0.3; //set the newton gradient algorithm fixed step
const int attriNum = 33;
const int sampleNum = 198;
int trainNum = 140;

struct DataSample
{
double attriValue[attriNum];
bool classLabel;
};

double StringTodouble(char * src)
{
double a;
stringstream str;
str< str>>a;
str.clear();
return a;
}



int ReadData( DataSample* data, char *file)
{
FILE *pFile;
char buf[1024];
pFile = fopen(file,"rt");
if(pFile==NULL)
{
printf("the data file is not existing: %s\n", file);
return -1;
}

int row = 0; //data line
int cloumn = 0; //data attribute
char delim[] = ",";//data delimiter
char *tmpdata = NULL;//data cache

while(!feof(pFile)&&row {
buf[0] = '\0';
fgets(buf,1024,pFile);

if( buf[strlen(buf)-1]=='\n' )
{
buf[strlen(buf)-1]='\0';
}

//the first column is non-used,and second column is class label;
for( int column = 0;column<(attriNum+2);++column )
{
if( column==0 )
{
tmpdata = strtok(buf,delim);
continue;
}
else if( column==1 )
{
tmpdata = strtok(NULL,delim);


if( tmpdata[0]=='R' )
data[row].classLabel = 1; //R:1; N:0
else
data[row].classLabel = 0;

}
else
{
tmpdata = strtok(NULL,delim);

if(tmpdata[0]!='?')// '?' mean the loss attribute value
data[row].attriValue[column-2] = StringTodouble(tmpdata);
else
data[row].attriValue[column-2] = -1000;
}
}
++row;

}

return 1;
}

void Normalize( DataSample* data )
{
double atrriMinValue[attriNum];
double atrriMaxValue[attriNum];//for normalization (x-xmin)/(xmax-xmin)

//think about the first sample is none-loss
//get the min and max value of each attribute without thinking about the loss atrribute
for( int i=0;i {
atrriMinValue[i] = data[0].attriValue[i];
atrriMaxValue[i] = data[0].attriValue[i];
}

for( int row = 1; row for( int column = 0; column {
if( data[row].attriValue[column] > atrriMaxValue[column] && (data[row].attriValue[column]+1000)>0.0001 )
atrriMaxValue[column] = data[row].attriValue[column];

if( data[row].attriValue[column] 0.0001 )
atrriMinValue[column] = data[row].attriValue[column];
}

for( int row = 1; row for( int column = 0; column {
if( (data[row].attriValue[column]+1000)>0.0001)
data[row].attriValue[column] = (data[row].attriValue[column]-atrriMinValue[column])/(atrriMaxValue[column]-atrriMinValue[column]);
else
data[row].attriValue[column] = 0;//set loss value 0;
}
}

//use newton gradient descent algorithm to get the w
//logistic model: 1/(1+exp(-z))
//class label
void Logistic( DataSample* data, double *logisW )
{

//memset( logisW,1.0,(attriNum+1)*sizeof(double) );//initial

for( int i=0;i<(attriNum+1);++i )
{
logisW[i] = 1.0;
}


Normalize( data );

double h = 0.0;
double error = 0.0;
for( int row=0; row {
h = 0.0;
for( int column=0; column {
h += data[row].attriValue[column]*logisW[column];
}
h += logisW[attriNum]*1;
h = 1/(1+exp(-h));

error = data[row].classLabel-h;

for( int column=0; column {
logisW[column] += error*alph*data[row].attriValue[column];
}
logisW[attriNum] = error*alph*1;

}
}

bool Predict( DataSample sample, double *logisW )
{
double h = 0.0;
bool label = 0;
for( int column=0; column {
h += sample.attriValue[column]*logisW[column];
}
h += logisW[attriNum];

if( h>0.5 )
label = 1;
else
label = 0;

if( label==sample.classLabel )
return 1;
else
return 0;
}


#endif
main.cpp

/*************
Logistic Regression( logistic 回归 )using newton gradient descent

the Data:from UCI datalib named "wpbc.data"(that is about cancer )

CopyRight 2016/8/21 xukaiwen
All Rights Reserved

**************/

#include "main.h"

int main()
{
char *file = "C:\\Users\\Administrator\\Desktop\\machine_learnning\\wpbc.data";
DataSample *data = new DataSample[sampleNum];
double *logisW = new double[attriNum+1];

if( -1!=ReadData( data,file ) )
{
Logistic( data,logisW );
}

for(int i=0;i<(attriNum+1);++i)
{
printf("%f\t",logisW[i]);
}
printf("\n\n");

int correct = 0;
int sum = 0;
for(int i=trainNum;i {
++sum;
bool eva = Predict(data[i],logisW);
if(eva)
++correct;
}

double rp = double(correct)/sum;
printf("the right correction: %f\n",rp);

delete []data;
delete []logisW;

return 0;
}



推荐阅读
  • 毕业设计:基于机器学习与深度学习的垃圾邮件(短信)分类算法实现
    本文详细介绍了如何使用机器学习和深度学习技术对垃圾邮件和短信进行分类。内容涵盖从数据集介绍、预处理、特征提取到模型训练与评估的完整流程,并提供了具体的代码示例和实验结果。 ... [详细]
  • 题目描述:给定n个半开区间[a, b),要求使用两个互不重叠的记录器,求最多可以记录多少个区间。解决方案采用贪心算法,通过排序和遍历实现最优解。 ... [详细]
  • 本文详细探讨了KMP算法中next数组的构建及其应用,重点分析了未改良和改良后的next数组在字符串匹配中的作用。通过具体实例和代码实现,帮助读者更好地理解KMP算法的核心原理。 ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • 深入解析:手把手教你构建决策树算法
    本文详细介绍了机器学习中广泛应用的决策树算法,通过天气数据集的实例演示了ID3和CART算法的手动推导过程。文章长度约2000字,建议阅读时间5分钟。 ... [详细]
  • 机器学习中的相似度度量与模型优化
    本文探讨了机器学习中常见的相似度度量方法,包括余弦相似度、欧氏距离和马氏距离,并详细介绍了如何通过选择合适的模型复杂度和正则化来提高模型的泛化能力。此外,文章还涵盖了模型评估的各种方法和指标,以及不同分类器的工作原理和应用场景。 ... [详细]
  • 本实验主要探讨了二叉排序树(BST)的基本操作,包括创建、查找和删除节点。通过具体实例和代码实现,详细介绍了如何使用递归和非递归方法进行关键字查找,并展示了删除特定节点后的树结构变化。 ... [详细]
  • Coursera ML 机器学习
    2019独角兽企业重金招聘Python工程师标准线性回归算法计算过程CostFunction梯度下降算法多变量回归![选择特征](https:static.oschina.n ... [详细]
  • Java 中 Writer flush()方法,示例 ... [详细]
  • 本文将介绍如何使用 Go 语言编写和运行一个简单的“Hello, World!”程序。内容涵盖开发环境配置、代码结构解析及执行步骤。 ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 主要用了2个类来实现的,话不多说,直接看运行结果,然后在奉上源代码1.Index.javaimportjava.awt.Color;im ... [详细]
  • 尽管深度学习带来了广泛的应用前景,其训练通常需要强大的计算资源。然而,并非所有开发者都能负担得起高性能服务器或专用硬件。本文探讨了如何在有限的硬件条件下(如ARM CPU)高效运行深度神经网络,特别是通过选择合适的工具和框架来加速模型推理。 ... [详细]
  • 机器学习核心概念与技术
    本文系统梳理了机器学习的关键知识点,涵盖模型评估、正则化、线性模型、支持向量机、决策树及集成学习等内容,并深入探讨了各算法的原理和应用场景。 ... [详细]
  • 深入浅出TensorFlow数据读写机制
    本文详细介绍TensorFlow中的数据读写操作,包括TFRecord文件的创建与读取,以及数据集(dataset)的相关概念和使用方法。 ... [详细]
author-avatar
隔岸观火2502884207
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有