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 ...
随机推荐
- 【BZOJ4036】[HAOI2015]按位或 FWT
[BZOJ4036][HAOI2015]按位或 Description 刚开始你有一个数字0,每一秒钟你会随机选择一个[0,2^n-1]的数字,与你手上的数字进行或(c++,c的|,pascal的or ...
- 【CF875E】Delivery Club 二分+线段树
[CF875E]Delivery Club 题意:有n个快递需要依次接收,这n个快递分部在x轴上,第i个快递的位置是xi.有两个快递员,一开始分别在s0,s1,你可以任意安排哪个人收哪个快递,前提是一 ...
- Sql Server 统计当天数据
方法一. ),日期字段名,)),) 方法二. Access: * FROM 表名 WHERE DAY(日期字段名)=DAY(NOW()) 查询当天之前一天的数据
- 通过JS模拟select表单,达到美化效果[demo][转]
转自: http://www.cnblogs.com/dreamback/p/SelectorJS.html 通过JS模拟select表单,达到美化效果 Demo ------------------ ...
- iOS ViewControllers 瘦身
https://objccn.io/issue-1-1/ https://juejin.im/user/57ddfba4128fe10064cbb93a 把 Data Source 和其他 Proto ...
- snowflake and uuid
分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的. 有些时候我们希望能使用一种简单一 ...
- es fielddata理解
在es中,text类型的字段使用一种叫做fielddata的查询时内存数据结构.当字段被排序,聚合或者通过脚本访问时这种数据结构会被创建.它是通过从磁盘读取每个段的整个反向索引来构建的,然后存存储在j ...
- Roadblocks--poj3255(次短路)
题目链接 求次短路的问题: dist[i][0]和dist[i][1]表示从起点1到i的距离和从起点n到i的距离: 次短路要比最短路大但小于其他路: 每条路1--n的距离都可以用dist[i][0] ...
- Python开发【算法】:斐波那契数列两种时间复杂度
斐波那契数列 概述: 斐波那契数列,又称黄金分割数列,指的是这样一个数列:0.1.1.2.3.5.8.13.21.34.……在数学上,斐波纳契数列以如下被以递归的方法定义:F(0)=0,F(1)=1, ...
- 发送html内容的email(转)
html中无法使用css, js.你发送一个<div>片断就好了,不用写整个html页面,因为即使写了,邮件客户端也会删除body之外(包括<body>这个标签)的内容,只留下 ...