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 ...
随机推荐
- Ubuntu 14.04 DNS 配置
最近得到一个比较好用的DNS,每次重启后都修改DNS配置文件 /etc/resolv.conf 重启就会失效 从网上得知 /etc/resolv.conf中的DNS配置是从/etc/resolvcon ...
- 【SpringBoot整合Elasticsearch】SpringBoot整合ElasticSearch
一.Linux下安装ElasticSearch 1.检测是否安装了Elasticsearch ps aux |grep elasticsearch 2.安装JDK 3.下载Elasticsearch ...
- jQuery的回调管理机制(二)
jQuery.extend({ /* * deferred对象的一大好处,就是它允许你自由添加多个回调函数. * $.ajax("test.html") .done(func ...
- ios-toolchain-based-on-clang-for-linux
https://github.com/tpoechtrager/cctools-port.git https://www.embtoolkit.org
- Jrebel不生效的原因和解决办法
一.问题原因和解决办法 我这里用的是idea,装了jrebel.之前用的好好的. 后边新建了一个project,不知道为啥,感觉总是不生效,虽然显示class reload了,但感觉还是没起作用. 后 ...
- 移动设备 h5屏幕适配
<meta name="HandheldFriendly" content="true"><meta name="MobileOpt ...
- TX大手笔做业务必然失败的原因
首先说一个伪命题: 物体会向下落这是一个基本的定律,一个小小的物理规则会覆盖所有物体的行为准则. 那么,当地球上的所有东西都下落的时候,你指望整个地球,月球,太阳也会下落么? 事实上大家都知道星球在宇 ...
- 原生JS实现ajax 发送post请求
1. [代码]原生JS实现ajax 发送post请求 <script> var oStr = ''; var postData = {}; var oAjax = null; //post ...
- mousedown\mouseup\click事件关系
背景分析: 如果用户在一个元素上点击,那么最少三个事件会被触发,事件发生顺序: 1.mousedown,当用户在这个元素上按下鼠标键的时候 2.mouseup,当用户在这个元素上松开鼠标键的时候 3. ...
- iOS 截屏分享(包含状态栏与不包含状态栏)
iOS8以上的新方法PhotoKit 监听截图相册变化,取最后一张图片:http://www.hangge.com/blog/cache/detail_1515.html PhotoKit 获取本机相 ...