python实现逻辑回归的示例
作者:chenxiangzhen 时间:2022-04-05 05:16:59
代码
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets.samples_generator import make_classification
def initialize_params(dims):
w = np.zeros((dims, 1))
b = 0
return w, b
def sigmoid(x):
z = 1 / (1 + np.exp(-x))
return z
def logistic(X, y, w, b):
num_train = X.shape[0]
y_hat = sigmoid(np.dot(X, w) + b)
loss = -1 / num_train * np.sum(y * np.log(y_hat) + (1-y) * np.log(1-y_hat))
cost = -1 / num_train * np.sum(y * np.log(y_hat) + (1 - y) * np.log(1 - y_hat))
dw = np.dot(X.T, (y_hat - y)) / num_train
db = np.sum(y_hat - y) / num_train
return y_hat, cost, dw, db
def linear_train(X, y, learning_rate, epochs):
# 参数初始化
w, b = initialize_params(X.shape[1])
loss_list = []
for i in range(epochs):
# 计算当前的预测值、损失和梯度
y_hat, loss, dw, db = logistic(X, y, w, b)
loss_list.append(loss)
# 基于梯度下降的参数更新
w += -learning_rate * dw
b += -learning_rate * db
# 打印迭代次数和损失
if i % 10000 == 0:
print("epoch %d loss %f" % (i, loss))
# 保存参数
params = {
'w': w,
'b': b
}
# 保存梯度
grads = {
'dw': dw,
'db': db
}
return loss_list, loss, params, grads
def predict(X, params):
w = params['w']
b = params['b']
y_pred = sigmoid(np.dot(X, w) + b)
return y_pred
if __name__ == "__main__":
# 生成数据
X, labels = make_classification(n_samples=100,
n_features=2,
n_informative=2,
n_redundant=0,
random_state=1,
n_clusters_per_class=2)
print(X.shape)
print(labels.shape)
# 生成伪随机数
rng = np.random.RandomState(2)
X += 2 * rng.uniform(size=X.shape)
# 划分训练集和测试集
offset = int(X.shape[0] * 0.9)
X_train, y_train = X[:offset], labels[:offset]
X_test, y_test = X[offset:], labels[offset:]
y_train = y_train.reshape((-1, 1))
y_test = y_test.reshape((-1, 1))
print('X_train=', X_train.shape)
print('y_train=', y_train.shape)
print('X_test=', X_test.shape)
print('y_test=', y_test.shape)
# 训练
loss_list, loss, params, grads = linear_train(X_train, y_train, 0.01, 100000)
print(params)
# 预测
y_pred = predict(X_test, params)
print(y_pred[:10])
来源:https://www.cnblogs.com/chenxiangzhen/p/10395231.html
标签:python,逻辑回归,机器学习
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Dreamweaver4探谜系列(1)
2010-09-05 21:12:00
双屏显示提升前端开发10%工作效率
2009-03-16 18:22:00
![](https://img.aspxhome.com/file/UploadPic/20093/16/screen-34s.jpg)
WML初级教程之从实际应用中了解WML
2008-09-04 11:24:00
python 检测nginx服务邮件报警的脚本
2023-08-04 17:34:10
利用django+wechat-python-sdk 创建微信服务器接入的方法
2023-06-04 21:28:55
![](https://img.aspxhome.com/file/2023/3/81853_0s.jpg)
Sql Server 索引使用情况及优化的相关Sql语句分享
2012-06-06 19:49:36
Python解释器以及PyCharm的安装教程图文详解
2021-04-09 11:56:32
![](https://img.aspxhome.com/file/2023/6/71386_0s.jpg)
使用 TRUNCATE TABLE 删除所有行
2008-04-24 19:20:00
对python 树状嵌套结构的实现思路详解
2022-02-04 15:45:06
![](https://img.aspxhome.com/file/2023/7/89287_0s.jpg)
MySQL数据库安全解决方案
2009-10-17 21:36:00
数字人组件反写[asp组件开发实例5]
2009-06-09 13:23:00
![](https://img.aspxhome.com/file/UploadPic/20093/9/20093918286639s.gif)
python列表生成器迭代器实例解析
2022-01-14 15:09:20
pytorch_pretrained_bert如何将tensorflow模型转化为pytorch模型
2022-04-18 18:07:58
![](https://img.aspxhome.com/file/2023/0/88850_0s.png)
httpwatch 的页面元素加载时间表
2008-02-13 08:28:00
![](https://img.aspxhome.com/file/UploadPic/20082/13/200821383430675s.gif)
重构Python代码的六个实例
2023-08-07 02:10:14
Python3 hashlib密码散列算法原理详解
2021-07-06 12:59:55
![](https://img.aspxhome.com/file/2023/9/65869_0s.png)
Python函数和模块的使用详情
2023-10-11 13:51:20
Python 去除字符串中指定字符串
2023-04-20 23:44:53
![](https://img.aspxhome.com/file/2023/2/70742_0s.png)
在Django的URLconf中进行函数导入的方法
2023-07-10 10:46:26
ASP访问带多个参数的存储过程
2008-10-14 16:45:00