Logistic Regression Using Gradient Descent -- Binary Classification 代码实现
1. 原理
Cost function

Theta

2. Python
# -*- coding:utf8 -*-
import numpy as np
import matplotlib.pyplot as plt def cost_function(input_X, _y, theta):
"""
cost function of binary classification using logistic regression
:param input_X: np.matrix input X
:param _y: np.matrix y
:param theta: np.matrix theta
"""
m = input_X.shape[0]
z = input_X * theta
h = np.asmatrix(1 / np.asarray(1 + np.exp(-z)))
J = 1.0 / m * (np.log(h) * _y.T + np.log(1 - h) * (1 - _y).T)
return J def gradient_descent(input_X, _y, theta, learning_rate=0.1,
iterate_times=3000):
"""
gradient descent of logistic regression
:param input_X: np.matrix input X
:param _y: np.matrix y
:param theta: np.matrix theta
:param learning_rate: float learning rate
:param iterate_times: int max iteration times
:return: tuple
"""
m = input_X.shape[0]
Js = [] for i in range(iterate_times):
z = input_X * theta
h = np.asmatrix(1 / np.asarray(1 + np.exp(-z)))
errors = h - _y
delta = 1.0 / m * (errors.T * input_X).T
theta -= learning_rate * delta
Js.append(cost_function(input_X, _y, theta)) return theta, Js
3. C++
#include <iostream>
#include <vector>
#include <Eigen/Dense> using namespace std;
using namespace Eigen; double cost_function(MatrixXd &input_X, MatrixXd &_y, MatrixXd &theta):
double m = input_X.rows();
ArrayXd _z = - (input_X * theta).array();
ArrayXd h = 1.0 / (1.0 + _z.exp());
double J = h.log().matrix() * _y.transpose() + \
( - h).log().matrix() * ( - _y.array()).matrix().transpose();
return J class GradientDescent{
public:
GradientDescent(MatrixXd &x, MatrixXd &y, MatrixXd &t, double r,
int i): input_X(x), _y(y), theta(t), learning_rate(r),
iterate_times(i) {}
MatrixXd theta;
vector<double> Js;
void run();
private:
MatrixXd input_X;
MatrixXd _y;
double learning_rate;
int iterate_times;
} void GradientDescent::run() {
double rows = input_X.rows();
for(int i=; i<iterate_times; ++i) {
ArrayXd _z = - (input_X * theta).array();
ArrayXd h = 1.0 / (1.0 + _z.exp());
MatrixXd errors = h.matrix() - y;
MatrixXd delta = 1.0 / rows * (errors.transpose() * input_X).transpose();
theta -= learning_rate * delta;
double J = cost_function(input_X, _y, theta);
Js.push_back(J);
}
}
Logistic Regression Using Gradient Descent -- Binary Classification 代码实现的更多相关文章
- Logistic Regression and Gradient Descent
Logistic Regression and Gradient Descent Logistic regression is an excellent tool to know for classi ...
- Linear Regression Using Gradient Descent 代码实现
参考吴恩达<机器学习>, 进行 Octave, Python(Numpy), C++(Eigen) 的原理实现, 同时用 scikit-learn, TensorFlow, dlib 进行 ...
- 斯坦福机器学习视频笔记 Week1 Linear Regression and Gradient Descent
最近开始学习Coursera上的斯坦福机器学习视频,我是刚刚接触机器学习,对此比较感兴趣:准备将我的学习笔记写下来, 作为我每天学习的签到吧,也希望和各位朋友交流学习. 这一系列的博客,我会不定期的更 ...
- 斯坦福机器学习视频笔记 Week1 线性回归和梯度下降 Linear Regression and Gradient Descent
最近开始学习Coursera上的斯坦福机器学习视频,我是刚刚接触机器学习,对此比较感兴趣:准备将我的学习笔记写下来, 作为我每天学习的签到吧,也希望和各位朋友交流学习. 这一系列的博客,我会不定期的更 ...
- 线性回归、梯度下降(Linear Regression、Gradient Descent)
转载请注明出自BYRans博客:http://www.cnblogs.com/BYRans/ 实例 首先举个例子,假设我们有一个二手房交易记录的数据集,已知房屋面积.卧室数量和房屋的交易价格,如下表: ...
- Linear Regression and Gradient Descent
随着所学算法的增多,加之使用次数的增多,不时对之前所学的算法有新的理解.这篇博文是在2018年4月17日再次编辑,将之前的3篇博文合并为一篇. 1.Problem and Loss Function ...
- Linear Regression and Gradient Descent (English version)
1.Problem and Loss Function Linear Regression is a Supervised Learning Algorithm with input matrix ...
- 【Linear Models for Binary Classification】林轩田机器学习基石
首先回顾了几个Linear Model的共性:都是算出来一个score,然后做某种变化处理. 既然Linear Model有各种好处(训练时间,公式简单),那如何把Linear Regression给 ...
- 机器学习技法:05 Kernel Logistic Regression
Roadmap Soft-Margin SVM as Regularized Model SVM versus Logistic Regression SVM for Soft Binary Clas ...
随机推荐
- Qt编写密钥生成器+使用demo(开源)
在很多商业软件中,需要提供一些可以试运行的版本,这样就需要配套密钥机制来控制,纵观大部分的试用版软件,基本上采用以下几种机制来控制.1:远程联网激活,每次启动都联网查看使用时间等,这种方法最完美,缺点 ...
- Qt编写网络调试助手(TCP客户端+TCP服务端+UDP服务端)终极版开源
时隔半年,对网络调试助手工具进行所有代码重写,这次目录结果整齐的一逼,代码整齐的一逼,非常完善了,打死也不再改版了.这次真的打死也不再改版了.旧版本1:http://www.qtcn.org/bbs/ ...
- css3整理--border-radius
1.border-radius 标准: border-top-left-radius: x y // 左上角,x 圆角水平半径, y 圆角垂直半径 border-top-right-radius:x ...
- PKI技术原理
转:http://3layer.blog.51cto.com/57448/20430 对称加密 symmetric cryptographic 非对称加密 asymmetric ...
- sencha touch NavigationView
NavigationView 是官方根据Container控件扩展而来的,由一个导航栏和一个card组成,具备导航和返回时自动销毁当前界面的功能,非常适合新手使用. 其中导航栏的代码如下: Ext.d ...
- JVM常用工具使用之jmap
一.参考链接 https://www.cnblogs.com/yjd_hycf_space/p/7753847.html 二.个人的总结 一般我习惯使用 jmap -dump:live,format= ...
- sprint boot 配置
来源:https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto-configure-to ...
- yii---解决post请求出现500错误
在使用yii框架的时候,在发送数据请求的时候,POST请求会出现500错误,这是因为yii2开启了防御csrf的攻击机制,可去先去掉,在控制器里去掉:public $enableCsrfValidat ...
- 分享10款效果惊艳的HTML5图片特效【转】
先插入一条广告,博主新开了一家淘宝店,经营自己纯手工做的发饰,新店开业,只为信誉!需要的亲们可以光顾一下!谢谢大家的支持!店名: 小鱼尼莫手工饰品店经营: 发饰.头花.发夹.耳环等(手工制作)网店: ...
- TFS二次开发11——标签(Label)
下图是在VS2010里创建Label的界面 可以看出创建Label 需要如下参数:Name.Comment.Path.Version .下面是代码实现: using Microsoft.TeamFou ...