资源简介
压缩包里含有logistic regression逻辑回归的Python源代码,训练数据集和测试训练集,最后也用Python画了结构示意图。只需要有Numpy和Matplotlib两个包即可。

代码片段和文件信息
import numpy as np
# the function of sigmoid
def sig(x):
return 1.0 / (1 + np.exp(-x))
# train the model by BGD algorithm
def lr_train_bgd(feature label maxCycle alpha):
n = np.shape(feature)[1] # the dimension of the feature
w = np.mat(np.ones((n 1))) # initialize the weights
i =0
while i < maxCycle:
i += 1
h = sig(feature * w)
err = label - h
if i % 100 == 0:
print(‘-----iter=‘ + str(i) + ‘train error rate=‘ + str(error_rate(h label)))
w = w + alpha * feature.T * err # update the weights
return w
# compute the rate of error
def error_rate(h label):
m = np.shape(h)[0]
sum_err = 0.0
for i in range(m):
if h[i 0] > 0 and (1 - h[i 0]) > 0:
sum_err -= (label[i 0] * np.log(h[i 0]) + (1-label[i 0]) * np.log(1-h[i 0]))
else:
sum_err -= 0
return sum_err / m
# load the data for training
def load_data(file_name):
f = open(file_name)
feature_data = []
label_data = []
for line in f.readlines():
feature_tmp = []
label_tmp = []
lines = line.strip().split(“\t“)
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)
# save the modelon the other wordsave the weights
def save_model(file_name w):
m = np.shape(w)[0]
f_w = open(file_name ‘w‘)
w_array = []
for i in range(m):
w_array.append(str(w[i 0]))
f_w.write(“\t“.join(w_array))
f_w.close()
# the main function
if __name__ == “__main__“:
feature label = load_data(‘data.txt‘)
w = lr_train_bgd(feature label 1000 0.01)
save_model(“weights“ w)
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 464 2019-04-04 16:00 classification\.idea\classification.iml
文件 188 2019-04-04 15:46 classification\.idea\misc.xm
文件 287 2019-04-04 15:46 classification\.idea\modules.xm
文件 12256 2019-04-04 17:00 classification\.idea\workspace.xm
文件 7251 2017-12-01 23:19 classification\data.txt
文件 1957 2019-04-04 16:29 classification\logistic-regression.py
文件 1507 2019-04-04 16:55 classification\test.py
文件 6851 2017-12-01 23:19 classification\test_data
文件 0 2019-04-04 16:14 classification\__init__.py
目录 0 2019-04-04 17:00 classification\.idea
目录 0 2019-04-04 17:00 classification
----------- --------- ---------- ----- ----
30761 11
相关资源
- A Byte of Python(简明Python教程)(第
- 机器学习(周志华)配套代码
- Python高級編程源代码
- 《PYTHON QT GUI快速编程 PYQT编程指南》
- 机器学习-岭回归实现
- 012345手势识别神经网络代码
- 猫-非猫图二分类识别
- 机器学习k means算法实现图像分割
- python实现逻辑回归
- python编程:入门到实践 源代码
- 餐厅管理器源代码
- kmeans聚类算法的python实现程序
- Python100经典练习题
- 南瓜书(PumpkinBook)
- Python源代码:以web方式管理自己的常
- 《Python从小白到大牛》源代码
- 机器学习numpy和pandas基础
- python机器学习Sebastian Raschka中文最新完
- Python-DeepMoji模型的pyTorch实现
- python数据分析源代码Ivan Idris
- Python项目案例开发从入门到实战源代
- Python3.x+PyQtChart实现数据可视化界面
- 《机器学习实战》源代码Python3
- 《Python语言程序设计基础第二版PDF+课
- python编程从入门到实践的案例和动手
- Python-使用DeepFakes实现YouTube视频自动换
- Mofan莫烦python全部教程代码
- Python语言程序设计基础 第二版 pdf书籍
- python,pygame开发的太空大战源代码
- Introduction to machine learning with python (
评论
共有 条评论