Logistics Regression

二分类问题。

模型 线性模型
响应 sigmoid
损失函数(显示) 最小均方
优化方法 BGD

例子:

#coding utf-8
import numpy as np def load_data(file_name):
feature_data = []
label_data = [] f = open(file_name) # 打开文件
for line in f.readlines():
# @ strip() 去除字符串首尾的空格
# @ split("\t") 以“\t”分割字符串
lines = line.strip().split("\t") feature_tmp = []
label_tmp = []
feature_tmp.append(1) # 偏置项 for i in range(len(lines)-1):
feature_tmp.append(float(lines[i]))
label_tmp.append(float(lines[-1])) feature_data.append(feature_tmp)
label_data.append(label_tmp) f.close() # 关闭文件 return np.mat(feature_data), np.mat(label_data) def sig(x):
return 1.0/(1+np.exp(-x)) def compute_error(h, label):
# @ shape() 获得特征的长度,[0]行数,[1]列数
n = np.shape(h)[0] err = 0
for i in range(n):
if h[i,0]>0 and (1-h[i,0])>0:
err -= (label[i,0]*np.log(h[i,0])
+ (1-label[i,0])*np.log(1-h[i,0]))
else:
err -= 0 return err def lr_train_bgd(feature, label, maxCycle, alpha):
n = np.shape(feature)[1]
W = np.mat(np.ones((n,1))) for i in range(maxCycle):
h = sig(feature*W)
err = label - h
if i % 100 == 0:
print(compute_error(h, label)) W = W + alpha * feature.T * err return W def save_model(file_name, W):
f = open(file_name, "w")
w_array = []
n = np.shape(W)[0]
for i in range(n):
w_array.append(str(W[i,0])) f.write("\t".join(w_array))
f.close() if __name__ == "__main__":
print("load data")
feature, label = load_data("data.txt")
print("train")
w = lr_train_bgd(feature, label, 1000, 0.1)
print("save")
save_model("weights2018", w)

  

参考:

https://blog.csdn.net/google19890102/article/details/77996085

https://blog.csdn.net/google19890102?viewmode=contents

https://github.com/zhaozhiyong19890102/Python-Machine-Learning-Algorithm

01-赵志勇机器学习-Logistics_Regression-train的更多相关文章

  1. 12-赵志勇机器学习-Label_Propagation

    (草稿) 过程: 1. 初始化所有节点的 labels 成唯一的值: 2. 对每个节点,将 label 更新为和其相连的所有节点中,标签最多的 节点的label: 2. 初始化情况下,假如所有相连的节 ...

  2. 11-赵志勇机器学习-DBSCAN聚类

    (草稿) 两点关系的三种定义: 1. 直接密度可达:A在B的邻域内: 2. 密度可达:AB之间存在,直接密度可达的点串: 3. 密度连接:AB之间存在点k,使得Ak和Bk都密度可达: 过程: 1. 对 ...

  3. 09-赵志勇机器学习-k-means

    (草稿) k-means: 1. 随机选取n个中心 2. 计算每个点到各个中心的距离 3. 距离小于阈值的归成一类. 4. 计算新类的质心,作为下一次循环的n个中心 5. 直到新类的质心和对应本次循环 ...

  4. 10-赵志勇机器学习-meanshift

    (草稿) meanshift 也是一种聚类方法. 优点在于:不需要提前指定类型数. 缺点就是计算量大 过程:(最一般的做法,没有使用核函数) 1. 逐点迭代,设置为位置中心 2. 计算所有点到位置中心 ...

  5. 02-赵志勇机器学习-Logistics_Regression-test(转载)

    # coding:UTF-8 ''' Date:20160901 @author: zhaozhiyong ''' import numpy as np from lr_train import si ...

  6. 00-赵志勇机器学习-Logistics_Regression-data.txt(转载)

    4.45925637575900 8.22541838354701 0 0.0432761720122110 6.30740040001402 0 6.99716180262699 9.3133933 ...

  7. 周志华-机器学习西瓜书-第三章习题3.5 LDA

    本文为周志华机器学习西瓜书第三章课后习题3.5答案,编程实现线性判别分析LDA,数据集为书本第89页的数据 首先介绍LDA算法流程: LDA的一个手工计算数学实例: 课后习题的代码: # coding ...

  8. 机器学习周志华 pdf统计学习人工智能资料下载

    周志华-机器学习 pdf,下载地址: https://u12230716.pipipan.com/fs/12230716-239561959 统计学习方法-李航,  下载地址: https://u12 ...

  9. 机器学习系统设计(Building Machine Learning Systems with Python)- Willi Richert Luis Pedro Coelho

    机器学习系统设计(Building Machine Learning Systems with Python)- Willi Richert Luis Pedro Coelho 总述 本书是 2014 ...

随机推荐

  1. Metasploit 常用命令手册

    Installation curl https://raw.githubusercontent.com/rapid7/metasploit-omnibus/master/config/template ...

  2. leetcode 903. DI序列的有效排列

    题目描述: 我们给出 S,一个源于 {'D', 'I'} 的长度为 n 的字符串 .(这些字母代表 “减少” 和 “增加”.)有效排列 是对整数 {0, 1, ..., n} 的一个排列 P[0], ...

  3. 第31课 std::atomic原子变量

    一. std::atomic_flag和std::atomic (一)std::atomic_flag 1. std::atomic_flag是一个bool类型的原子变量,它有两个状态set和clea ...

  4. C#用mouse_event模拟鼠标点击的问题

    1.首先添加using System.Runtime.InteropServices; 2.为鼠标添加模拟点击的各种参数 //鼠标事件  因为我用的不多,所以其他参数没有写 1 2 3 4 5 6 7 ...

  5. Navicat MYSQL 建立关联表 保存时遇到 cannot add foreign key constraint

    首先建立user表,如下图 然后建立message表,userid用作外键,关联user表的id 点击上面的外键按钮,添加外键如下 结果保存时报错: cannot add foreign key co ...

  6. MySQL子查询结果集是否有记录

    Mark SELECT tu.id userId, tu.avatar_url avatarUrl, tu.wx_nick_name wxNickName, tu.city city, (select ...

  7. 关于stm32f10x_conf.h文件

    简介 stm32f10x_conf.h文件有2个作用:①提供对assert_param运行时参数检查宏函数的定义.②将开发者用到的标准外设头文件集中在这个文件里面,而stm32f10x_conf.h又 ...

  8. POSIX 正则表达式 BRE与ERE的差异

    BRE,标准正则表达式,basic regular expressions ERE,扩展正则表达式,Extended Regular Expressions POSIX 正则表达式 传统上,POSIX ...

  9. 深入V8引擎-初始化之InitPlatform

    上一篇其实想讲初始化的第二步,但是内容比较无聊,所以换了一个话题,谈了谈v8的命名空间和宏,稍微轻松一下. 在这里还是接着说说初始化过程,毕竟写博客的初衷是对自己努力的记录,不是为了吸粉,这篇没图,对 ...

  10. excel批量添加超链接

    使用 Hyperlink(Link-location,Friendly-name)