逻辑回归&&code
没有正则化项的时候的二分类


#-*-coding=utf-8-*-
from numpy import loadtxt,where, transpose
import matplotlib.pyplot as plt
from ipykernel.pylab.backend_inline import show
import numpy as np
from scipy.optimize import minimize def sigmoid(x): return 1.0/(1+np.e**(-1.0*x)) def cost(theat,x,y):
m=len(x)
J=-(1.0/m)*(transpose(y).dot(np.log(sigmoid(x.dot(theat))))+transpose(1-y).dot(np.log(1-sigmoid(x.dot(theat)))));
if np.isnan(J):
return(np.inf)
return J def gradient(theat,x,y):
m=len(x)
h=sigmoid(x.dot(transpose(theat)))
grad=(1.0/m)*(h-y).dot(x)
return grad def gradient_two(theat,x,y,alpha=0.0001,iteration=40000000):
m=len(x)
for i in xrange(iteration):
h=sigmoid(x.dot(theat))
grad1=theat[0]-alpha*(1.0/m)*transpose(h-y).dot(x[:,0]);
grad2=theat[1]-alpha*(1.0/m)*transpose(h-y).dot(x[:,1]);
grad3=theat[2]-alpha*(1.0/m)*transpose(h-y).dot(x[:,2]);
theat[0],theat[1],theat[2]=grad1,grad2,grad3
print 'cost',cost(theat,x,y)
print 'grad',grad1,grad2,grad3
return theat if __name__=="__main__": data=loadtxt(r'D:/机器学习/【批量下载】data1等/数据挖掘/ml_data/data1.txt',delimiter=',');
x=np.c_[np.ones((len(data),1)),data[:,0:2]];
y=data[:,2]
theat=np.zeros(x.shape[1]);
theat=transpose(theat);
theat=gradient_two(theat,x,y);
#res = minimize(cost, theat, args=(x,y), jac=gradient, options={'maxiter':400})
#print res
'''最后结果
theat[0]=-22.3021297062;
theat[1]=0.183373208731;
theat[2]=0.178329470851;
'''
x1=[20,100]
y1=[-(theat[0]+theat[1]*x1[0])*1.0/theat[2],-(theat[0]+theat[1]*x1[1])*1.0/theat[2]]
plt.plot(x1,y1)
pos=where(y==1)
neg=where(y==0)
plt.scatter(x[pos,1],x[pos,2],marker='o',c='b')
plt.scatter(x[neg,1],x[neg,2],marker='x',c='r')
plt.show()

加上正则化后的损失函数和公式(不想再写代码了,意会就可以了 ,逃。。。。


逻辑回归&&code的更多相关文章
- 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 ...
- DeepLearning之路(一)逻辑回归
逻辑回归 1. 总述 逻辑回归来源于回归分析,用来解决分类问题,即预测值变为较少数量的离散值. 2. 基本概念 回归分析(Regression Analysis):存在一堆观测资料,希望获得数据内 ...
- 斯坦福第六课:逻辑回归(Logistic Regression)
6.1 分类问题 6.2 假说表示 6.3 判定边界 6.4 代价函数 6.5 简化的成本函数和梯度下降 6.6 高级优化 6.7 多类分类:一个对所有 6.1 分类问题 在分类问题中 ...
- Matlab实现线性回归和逻辑回归: Linear Regression & Logistic Regression
原文:http://blog.csdn.net/abcjennifer/article/details/7732417 本文为Maching Learning 栏目补充内容,为上几章中所提到单参数线性 ...
- Stanford机器学习---第三讲. 逻辑回归和过拟合问题的解决 logistic Regression & Regularization
原文:http://blog.csdn.net/abcjennifer/article/details/7716281 本栏目(Machine learning)包括单参数的线性回归.多参数的线性回归 ...
- 深度学习实践系列(1)- 从零搭建notMNIST逻辑回归模型
MNIST 被喻为深度学习中的Hello World示例,由Yann LeCun等大神组织收集的一个手写数字的数据集,有60000个训练集和10000个验证集,是个非常适合初学者入门的训练集.这个网站 ...
- 机器学习系列(3)_逻辑回归应用之Kaggle泰坦尼克之灾
作者:寒小阳 && 龙心尘 时间:2015年11月. 出处: http://blog.csdn.net/han_xiaoyang/article/details/49797143 ht ...
- 【机器学习】Octave 实现逻辑回归 Logistic Regression
ex2data1.txt ex2data2.txt 本次算法的背景是,假如你是一个大学的管理者,你需要根据学生之前的成绩(两门科目)来预测该学生是否能进入该大学. 根据题意,我们不难分辨出这是一种二分 ...
随机推荐
- Codeforces 549A. Face Detection[模拟]
A. Face Detection time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- AC日记——紧急措施 openjudge 1.7 22
22:紧急措施 总时间限制: 1000ms 内存限制: 65536kB 描述 近日,一些热门网站遭受黑客入侵,这些网站的账号.密码及email的数据惨遭泄露.你在这些网站上注册若干账号(使用的用户 ...
- Prefab Assist插件
资料 Prefab文档: http://game.ceeger.com/Manual/Prefabs.html 基础:基于NGUI制作组件的Prefab 前言 在一个游戏的UI中,有一些通用的组件,比 ...
- Android系统自带APP分析——短信app
Android操作系统本身就是一个巨大的开源软件仓库,熟悉它既可以了解到Android系统的设计框架,也可以获得高效的应用程序编写方式.本文所分析的源码来自于Google官方的AOSP源码4.0.1_ ...
- MMDrawerController的使用
1.http://www.jianshu.com/p/9e55cbf7d5ab MMDrawerController的使用
- HTML 学习笔记(块 和 类)
HTML <div> 和 <span> 可以通过<div>和<span>将HTML元素组合起来. HTML块元素 大多数HTML元素被定义为块级元素或者 ...
- Centos5.8 安装 PHP5.5 和 memcached
安装GIT 需要先安装gcc-c++ (sudo yum install gcc-c++) sudo yum install gettext-devel expat-devel cpio perl o ...
- Struts2动态方法调用(DMI)
当structs.xml解析到Action的时候,默认执行的是此action的execute()方法,但是实际开发中,我们的action中含有很多方法,比如说增删改查的方法,那么structs.xml ...
- Java 集合系列07之 Stack详细介绍(源码解析)和使用示例
概要 学完Vector了之后,接下来我们开始学习Stack.Stack很简单,它继承于Vector.学习方式还是和之前一样,先对Stack有个整体认识,然后再学习它的源码:最后再通过实例来学会使用它. ...
- Power of Two
Given an integer, write a function to determine if it is a power of two. bool isPowerOfTwo(int n) { ...