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

#-*-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. Stanford机器学习笔记-7. Machine Learning System Design

    7 Machine Learning System Design Content 7 Machine Learning System Design 7.1 Prioritizing What to W ...

  2. 线程实现方式以及序列化 反序列化.java

    一.序列化与反序列化       把对象转换为字节序列的过程称为对象的序列化. 把字节序列恢复为对象的过程称为对象的反序列化. 对象的序列化主要有两种用途: 1) 把对象的字节序列永久地保存到硬盘上, ...

  3. AC日记——单词的长度 openjudge 1.7 24

    24:单词的长度 总时间限制:  1000ms 内存限制:  65536kB 描述 输入一行单词序列,相邻单词之间由1个或多个空格间隔,请对应地计算各个单词的长度. 注意,如果有标点符号(如连字符,逗 ...

  4. Linux下mysql新建账号及权限设置

    http://www.cnblogs.com/eczhou/archive/2012/07/12/2588187.html 1.权限赋予 说明:mysql部署在服务器A上,内网上主机B通过客户端工具连 ...

  5. Android测试之Monkey

    自己用的测试 C:\Users\Star>adb shell monkey -p com.cmstop.android --monitor-native-crashes -- pct-touch ...

  6. JS中NULL和Undefined的区别

    NULL是表示一个”无“的对象,转换成数值为0:undefined是一个“无”的原始值,转为数值为NaN: 当声明的变量还未被初始化时,变量的默认值为undefined: null用来表示尚未存在的对 ...

  7. SQL SERVER的连接方式

    最近在做项目的时候,遇到了SQLSERVER的连接,以前是很模糊的,现在做一个简单的总结. 针对SQL_SERVER,连接指定的方式有两种,一种是Where条件指定方式,另外一种是采用On连指定连接条 ...

  8. IE 和Firefox的js兼容性总结

    IE 和Firefox的js兼容性总结 12 August 2010 11:39 Thursday by 小屋 标签: 浏览器 方法 属性 IT 写法 一.函数和方法差异 1 . getYear()方 ...

  9. iOS APNS配置(转)

    Introduction To send Push notification to an application/device couple you need an unique device tok ...

  10. iOS视频录制、压缩导出、取帧等http://www.jianshu.com/p/6f23f608048e

    原文网址请参考:http://www.jianshu.com/p/6f23f608048e