没有正则化项的时候的二分类

#-*-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的更多相关文章

  1. stanford coursera 机器学习编程作业 exercise 3(逻辑回归实现多分类问题)

    本作业使用逻辑回归(logistic regression)和神经网络(neural networks)识别手写的阿拉伯数字(0-9) 关于逻辑回归的一个编程练习,可参考:http://www.cnb ...

  2. Theano3.3-练习之逻辑回归

    是官网上theano的逻辑回归的练习(http://deeplearning.net/tutorial/logreg.html#logreg)的讲解. Classifying MNIST digits ...

  3. DeepLearning之路(一)逻辑回归

    逻辑回归 1.  总述 逻辑回归来源于回归分析,用来解决分类问题,即预测值变为较少数量的离散值. 2.  基本概念 回归分析(Regression Analysis):存在一堆观测资料,希望获得数据内 ...

  4. 斯坦福第六课:逻辑回归(Logistic Regression)

    6.1  分类问题 6.2  假说表示 6.3  判定边界 6.4  代价函数 6.5  简化的成本函数和梯度下降 6.6  高级优化 6.7  多类分类:一个对所有 6.1  分类问题 在分类问题中 ...

  5. Matlab实现线性回归和逻辑回归: Linear Regression & Logistic Regression

    原文:http://blog.csdn.net/abcjennifer/article/details/7732417 本文为Maching Learning 栏目补充内容,为上几章中所提到单参数线性 ...

  6. Stanford机器学习---第三讲. 逻辑回归和过拟合问题的解决 logistic Regression & Regularization

    原文:http://blog.csdn.net/abcjennifer/article/details/7716281 本栏目(Machine learning)包括单参数的线性回归.多参数的线性回归 ...

  7. 深度学习实践系列(1)- 从零搭建notMNIST逻辑回归模型

    MNIST 被喻为深度学习中的Hello World示例,由Yann LeCun等大神组织收集的一个手写数字的数据集,有60000个训练集和10000个验证集,是个非常适合初学者入门的训练集.这个网站 ...

  8. 机器学习系列(3)_逻辑回归应用之Kaggle泰坦尼克之灾

    作者:寒小阳 && 龙心尘 时间:2015年11月. 出处: http://blog.csdn.net/han_xiaoyang/article/details/49797143 ht ...

  9. 【机器学习】Octave 实现逻辑回归 Logistic Regression

    ex2data1.txt ex2data2.txt 本次算法的背景是,假如你是一个大学的管理者,你需要根据学生之前的成绩(两门科目)来预测该学生是否能进入该大学. 根据题意,我们不难分辨出这是一种二分 ...

随机推荐

  1. Android SDK和N多Android开发资源

    开发资源 本文收集整理Android开发需要的Android SDK.工具.Android开发教程.Android设计规范,免费的设计素材等. 欢迎大家推荐自己在Android开发过程中用的好用的工具 ...

  2. Android app 简单的电话拨号器

    实现步骤: 1.画UI 可以用拖拽和文本编辑. 2.根据UI写业务逻辑  在MainActivity中的onCreate中编写 //get editText content et_number = ( ...

  3. kali开启ssh

    Kali 2.0安装之后需要做的事--使用SSH进行远程登录   2015年8月11日,Kali官方推出了新的kali系统2.0版本,此次升级最大的特点就是系统界面的设计理念更加先进,以及系统的升级方 ...

  4. Javascript备忘复习笔记2

    一.函数与形参 1.函数 function abs(x) { if (x >= 0) { return x; } else { return -x; } } alert(abs(-10)); 2 ...

  5. JDK7中的新特性 The try-with-resources Statement

    https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html 类似于这样的代码 try ( By ...

  6. Location of several networks in brain

    Source: Naci, L., et al. (2014). "A common neural code for similar conscious experiences in dif ...

  7. FSL - MELODIC

    Source: http://fsl.fmrib.ox.ac.uk/fsl/fslwiki/MELODIC; https://fsl.fmrib.ox.ac.uk/fsl/fslwiki/MELODI ...

  8. BZOJ 1854 【Scoi2010】 游戏

    Description lxhgww最近迷上了一款游戏,在游戏里,他拥有很多的装备,每种装备都有2个属性,这些属性的值用[1,10000]之间的数表示.当他使用某种装备时,他只能使用该装备的某一个属性 ...

  9. Linux 网络编程详解三(p2p点对点聊天)

    //p2p点对点聊天多进程版--服务器(信号的使用) #include <stdio.h> #include <stdlib.h> #include <string.h& ...

  10. python 测试驱动开发的简单例子

    一.需求分析 需求:一个类 MyClass,有两个功能:add, sub 1.先功能设计 # myclass.py class MyClass(object): # 加法 def add(self): ...