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 代码实现的更多相关文章

  1. Logistic Regression and Gradient Descent

    Logistic Regression and Gradient Descent Logistic regression is an excellent tool to know for classi ...

  2. Linear Regression Using Gradient Descent 代码实现

    参考吴恩达<机器学习>, 进行 Octave, Python(Numpy), C++(Eigen) 的原理实现, 同时用 scikit-learn, TensorFlow, dlib 进行 ...

  3. 斯坦福机器学习视频笔记 Week1 Linear Regression and Gradient Descent

    最近开始学习Coursera上的斯坦福机器学习视频,我是刚刚接触机器学习,对此比较感兴趣:准备将我的学习笔记写下来, 作为我每天学习的签到吧,也希望和各位朋友交流学习. 这一系列的博客,我会不定期的更 ...

  4. 斯坦福机器学习视频笔记 Week1 线性回归和梯度下降 Linear Regression and Gradient Descent

    最近开始学习Coursera上的斯坦福机器学习视频,我是刚刚接触机器学习,对此比较感兴趣:准备将我的学习笔记写下来, 作为我每天学习的签到吧,也希望和各位朋友交流学习. 这一系列的博客,我会不定期的更 ...

  5. 线性回归、梯度下降(Linear Regression、Gradient Descent)

    转载请注明出自BYRans博客:http://www.cnblogs.com/BYRans/ 实例 首先举个例子,假设我们有一个二手房交易记录的数据集,已知房屋面积.卧室数量和房屋的交易价格,如下表: ...

  6. Linear Regression and Gradient Descent

    随着所学算法的增多,加之使用次数的增多,不时对之前所学的算法有新的理解.这篇博文是在2018年4月17日再次编辑,将之前的3篇博文合并为一篇. 1.Problem and Loss Function ...

  7. Linear Regression and Gradient Descent (English version)

    1.Problem and Loss Function   Linear Regression is a Supervised Learning Algorithm with input matrix ...

  8. 【Linear Models for Binary Classification】林轩田机器学习基石

    首先回顾了几个Linear Model的共性:都是算出来一个score,然后做某种变化处理. 既然Linear Model有各种好处(训练时间,公式简单),那如何把Linear Regression给 ...

  9. 机器学习技法:05 Kernel Logistic Regression

    Roadmap Soft-Margin SVM as Regularized Model SVM versus Logistic Regression SVM for Soft Binary Clas ...

随机推荐

  1. vuex - 辅助函数学习

    官网文档: https://vuex.vuejs.org/zh-cn/api.html  最底部 mapState 此函数返回一个对象,生成计算属性 - 当一个组件需要获取多个状态时候,将这些状态都声 ...

  2. vue Element动态设置el-menu导航当前选中项

    1,npm install vuex --save 2,在src下新建vuex文件夹,新建store.js文件: store.js import Vue from 'vue' import Vuex ...

  3. [原]git的使用(六)---远程仓库

    10.远程仓库 -------------------------------------------------------------------------------------------- ...

  4. vue钩子生命周期

    1.beforeCreate        // 组件实例刚刚被创建2.created                 // 实例已经创建完成3.beforeMount        // 模板编译之 ...

  5. EF---延迟加载技术

    延迟加载: 优点:只在需要的时候加载数据,不需要预先计划,避免了各种复杂的外连接.索引.视图操作带来的低效率问题 使用方式:两步 第一:在需要延迟加载的属性前加上virtual ,该属性的类型可以是任 ...

  6. vue--子组件主动获取父组件的数据和方法

    子组件主动获取父组件的数据和方法 简单示例: this.$parent.数组 this.$parent.方法 示例: <template> <div id="Header& ...

  7. linux 安装Swagger(swagger-editor , swagger-ui)

    一.环境要求 1.Nodejs ( 版本6.0x以上 ) 2.npm (npm 3.x) 注: linux 更新nodejs到最新 node -v 4.2.1 sudo npm cache clean ...

  8. jqGrid 中的editrules来自定义colModel验证规则

    editrules    editrules是用来设置一些可用于可编辑列的colModel的额外属性的.大多数的时候是用来在提交到服务器之前验证用户的输入合法性的.比如editrules:{edith ...

  9. Git 使用篇一:初步使用GitHub,下载安装git,并上传项目

    首先在MAC上怎么操作. 在gitHub创立一个账户,在创立一个项目,这就不用我说了对吧. 创建完之后是这样的: 接下来,我们打开https://brew.sh 这是下载homebrew的网站,hom ...

  10. c++中用vector创建多维数组的初始化方法

    最近调试一个程序,在使用vector声明一个二维数组时出现错误.错误的方法如下所示: std::vector<std::vector<double> > sphereGrid; ...