小编给大家分享一下python怎么实现梯度下降和逻辑回归,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
具体内容如下
import numpy as np
import pandas as pd
import os
data = pd.read_csv("iris.csv") # 这里的iris数据已做过处理
m, n = data.shape
dataMatIn = np.ones((m, n))
dataMatIn[:, :-1] = data.ix[:, :-1]
classLabels = data.ix[:, -1]
# sigmoid函数和初始化数据
def sigmoid(z):
return 1 / (1 + np.exp(-z))
# 随机梯度下降
def Stocgrad_descent(dataMatIn, classLabels):
dataMatrix = np.mat(dataMatIn) # 训练集
labelMat = np.mat(classLabels).transpose() # y值
m, n = np.shape(dataMatrix) # m:dataMatrix的行数,n:dataMatrix的列数
weights = np.ones((n, 1)) # 初始化回归系数(n, 1)
alpha = 0.001 # 步长
maxCycle = 500 # 最大循环次数
epsilon = 0.001
error = np.zeros((n,1))
for i in range(maxCycle):
for j in range(m):
h = sigmoid(dataMatrix * weights) # sigmoid 函数
weights = weights + alpha * dataMatrix.transpose() * (labelMat - h) # 梯度
if np.linalg.norm(weights - error) < epsilon:
break
else:
error = weights
return weights
# 逻辑回归
def pred_result(dataMatIn):
dataMatrix = np.mat(dataMatIn)
r = Stocgrad_descent(dataMatIn, classLabels)
p = sigmoid(dataMatrix * r) # 根据模型预测的概率
# 预测结果二值化
pred = []
for i in range(len(data)):
if p[i] > 0.5:
pred.append(1)
else:
pred.append(0)
data["pred"] = pred
os.remove("data_and_pred.csv") # 删除List_lost_customers数据集 # 第一次运行此代码时此步骤不要
data.to_csv("data_and_pred.csv", index=False, encoding="utf_8_sig") # 数据集保存
pred_result(dataMatIn)
以上是“python怎么实现梯度下降和逻辑回归”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。