逻辑回归 C++
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <cmath>
template <typename DataType>
double sigmoid(DataType z) {
return 1.0/(1+exp((-1)*z));
}
template <typename DataType, typename WeightType>
double getMatResult(typename::std::vector<DataType> &data, typename::std::vector<WeightType> &weights) {
double result=0.0;
for(size_t i=0;i<data.size();++i) {
result+=data.at(i)*weights.at(i);
}
return result;
}
template <typename DataType>
void DisplayData(typename::std::vector<std::vector<DataType> > &vv) {
std::cout<<"the number of data: "<<vv.size()<<std::endl;
for(size_t i=0;i<vv.size();++i) {
for(typename::std::vector<DataType>::iterator it=vv[i].begin();it!=vv[i].end();++it) {
std::cout<<*it<<" ";
}
std::cout<<std::endl;
}
}
template <typename DataType, typename WeightType>
double CostFun(typename::std::vector<std::vector<DataType> > &vv, typename::std::vector<WeightType> &v_weights) {
double J=0.0;
typename::std::vector<DataType> v_x;
for(size_t i=0;i<vv.size();++i) {
v_x.clear();
v_x.push_back(vv[i][0]);
v_x.push_back(vv[i][1]);
v_x.push_back(vv[i][2]);
double z=getMatResult(v_x,v_weights);
J=J+vv[i][3]*log2(sigmoid(z))+(1-vv[i][3])*log2(1-sigmoid(z));
}
J=-J/vv.size();
return J;
}
int main() {
std::ifstream infile_feat("train.data");
std::string feature;
float feat_onePoint;
std::vector<float> lines;
std::vector<double> v_weights;
std::vector<std::vector<float> > lines_feat;
lines_feat.clear();
v_weights.clear();
for(size_t i=0;i<3;++i) {
v_weights.push_back(1.0);
}
while(!infile_feat.eof()) {
getline(infile_feat, feature);
if(feature.empty())
break;
std::stringstream stringin(feature);
lines.clear();
lines.push_back(1.0);
while(stringin >> feat_onePoint) {
lines.push_back(feat_onePoint);
}
lines_feat.push_back(lines);
}
infile_feat.close();
std::cout<<"display train data: "<<std::endl;
DisplayData(lines_feat);
double res=CostFun(lines_feat, v_weights);
std::cout<<"the value of cost function: "<<res<<std::endl;
std::vector<double> v_x;
while(true) {
double grad0=0.0,grad1=0.0,grad2=0.0;
for(size_t i=0;i<lines_feat.size();++i) {
v_x.clear();
v_x.push_back(lines_feat[i][0]);
v_x.push_back(lines_feat[i][1]);
v_x.push_back(lines_feat[i][2]);
grad0+=(lines_feat[i][3]-sigmoid(getMatResult(v_x,v_weights)))*lines_feat[i][0];
grad1+=(lines_feat[i][3]-sigmoid(getMatResult(v_x,v_weights)))*lines_feat[i][1];
grad2+=(lines_feat[i][3]-sigmoid(getMatResult(v_x,v_weights)))*lines_feat[i][2];
}
grad0=grad0/lines_feat.size();
grad1=grad1/lines_feat.size();
grad2=grad2/lines_feat.size();
//0.03为学习率阿尔法
v_weights[0]=v_weights[0]+0.03*grad0;
v_weights[1]=v_weights[1]+0.03*grad1;
v_weights[2]=v_weights[2]+0.03*grad2;
double res_new;
res_new=CostFun(lines_feat,v_weights);
if(std::abs(res_new-res)<0.0000000001)
break;
res=res_new;
}
for(size_t i=0;i<3;++i) {
std::cout<<v_weights.at(i)<<" ";
}
std::cout<<std::endl;
lines_feat.clear();
infile_feat.open("test.data");
while(!infile_feat.eof()) {
getline(infile_feat, feature);
if(feature.empty())
break;
std::stringstream stringin(feature);
lines.clear();
lines.push_back(1.0);
while(stringin >> feat_onePoint) {
lines.push_back(feat_onePoint);
}
lines_feat.push_back(lines);
}
infile_feat.close();
std::cout<<"display test data: "<<std::endl;
DisplayData(lines_feat);
for(size_t i=0;i<lines_feat.size();++i) {
v_x.clear();
v_x.push_back(lines_feat[i][0]);
v_x.push_back(lines_feat[i][1]);
v_x.push_back(lines_feat[i][2]);
res=getMatResult(v_x,v_weights);
double lable=sigmoid(res);
for(size_t j=0;j<4;++j) {
std::cout<<lines_feat[i][j]<<" ";
}
if(lable>0.5)
std::cout<<" 1"<<std::endl;
else
std::cout<<" 0"<<std::endl;
}
return 0;
}
逻辑回归 C++的更多相关文章
- 逻辑回归 Logistic Regression
逻辑回归(Logistic Regression)是广义线性回归的一种.逻辑回归是用来做分类任务的常用算法.分类任务的目标是找一个函数,把观测值匹配到相关的类和标签上.比如一个人有没有病,又因为噪声的 ...
- 用R做逻辑回归之汽车贷款违约模型
数据说明 本数据是一份汽车贷款违约数据 application_id 申请者ID account_number 账户号 bad_ind 是否违约 vehicle_year ...
- 逻辑回归(LR)总结复习
摘要: 1.算法概述 2.算法推导 3.算法特性及优缺点 4.注意事项 5.实现和具体例子 6.适用场合 内容: 1.算法概述 最基本的LR分类器适合于对两分类(类0,类1)目标进行分类:这个模型以样 ...
- scikit-learn 逻辑回归类库使用小结
之前在逻辑回归原理小结这篇文章中,对逻辑回归的原理做了小结.这里接着对scikit-learn中逻辑回归类库的我的使用经验做一个总结.重点讲述调参中要注意的事项. 1. 概述 在scikit-lear ...
- 逻辑回归LR
逻辑回归算法相信很多人都很熟悉,也算是我比较熟悉的算法之一了,毕业论文当时的项目就是用的这个算法.这个算法可能不想随机森林.SVM.神经网络.GBDT等分类算法那么复杂那么高深的样子,可是绝对不能小看 ...
- 逻辑回归(Logistic Regression)
转载请注明出自BYRans博客:http://www.cnblogs.com/BYRans/ 本文主要讲解分类问题中的逻辑回归.逻辑回归是一个二分类问题. 二分类问题 二分类问题是指预测的y值只有两个 ...
- 逻辑回归算法的原理及实现(LR)
Logistic回归虽然名字叫"回归" ,但却是一种分类学习方法.使用场景大概有两个:第一用来预测,第二寻找因变量的影响因素.逻辑回归(Logistic Regression, L ...
- 感知器、逻辑回归和SVM的求解
这篇文章将介绍感知器.逻辑回归的求解和SVM的部分求解,包含部分的证明.本文章涉及的一些基础知识,已经在<梯度下降.牛顿法和拉格朗日对偶性>中指出,而这里要解决的问题,来自<从感知器 ...
- stanford coursera 机器学习编程作业 exercise 3(逻辑回归实现多分类问题)
本作业使用逻辑回归(logistic regression)和神经网络(neural networks)识别手写的阿拉伯数字(0-9) 关于逻辑回归的一个编程练习,可参考:http://www.cnb ...
- Theano3.3-练习之逻辑回归
是官网上theano的逻辑回归的练习(http://deeplearning.net/tutorial/logreg.html#logreg)的讲解. Classifying MNIST digits ...
随机推荐
- dotnetnuke 添加用户属性 Profile
if (DotNetNuke.Entities.Profile.ProfileController.GetPropertyDefinitionByName(this.PortalId, "Q ...
- cookie和sessionStorage 、localStorage 对比
相同点:都存储在客户端 不同点:1.存储大小 cookie数据大小不能超过4k. sessionStorage和localStorage 虽然也有存储大小的限制,但比cookie大得多,可以达到5M或 ...
- jquery from使用
jquery form是一个基于jquery的表单异步提交的插件,通过它能快速简便的提交表单. html <div> <form id="ajaxForm" me ...
- GFS分布式文件系统脚本
#!/bin/bashfor i in $(fdisk -l | grep -wo "/dev/sd[b-z]" | sort)dodd if=/dev/zero of=$i bs ...
- APIshop精选接口助力双十一电商业务
距离2018年双11的购物盛典已经不到一个月了,各大电商之间的战役已经悄然打响,今年的双11仍会是一场电商鏖战,想必又会打破2017年双11近2540亿的全网成交总额记录. 据统计,去年双11全天共产 ...
- Web 常用
System.Web.Hosting.HostingEnvironment.MapPath(); HttpUtility.UrlEncode();
- c/c++排坑(5) -- c语言中的申明
C语言的申明总是令人头大,对于这块内容也一直让我头疼.希望通过这篇博客能够稍微梳理一下.材料和例子来源于<C专家编程> 一.C语言的申明的优先级规则 先来个例子,看看下面这行C代码到底是个 ...
- [Luogu] P3413 萌数
题目背景 本题由世界上最蒟蒻最辣鸡最撒比的SOL提供. 寂月城网站是完美信息教室的官网.地址:已和谐 . 题目描述 辣鸡蒟蒻SOL是一个傻逼,他居然觉得数很萌! 好在在他眼里,并不是所有数都是萌的.只 ...
- Windows编译PHP7.2拓展
转载请注明文章出处:https://tlanyan.me/windows-co... 准备工作 https://github.com/Microsoft/...下载PHP-SDK(在右边的" ...
- 联想小新Air 15 安装黑苹果macOS High Sierra 10.13.6过程
联想小新Air 15 安装黑苹果全过程 本文参考:https://blog.csdn.net/qq_28735663/article/details/80634300 本人是联想小新AIr 15 , ...