python實(shí)現(xiàn)梯度下降和邏輯回歸
本文實(shí)例為大家分享了python實(shí)現(xiàn)梯度下降和邏輯回歸的具體代碼,供大家參考,具體內(nèi)容如下
import numpy as np
import pandas as pd
import os
data = pd.read_csv("iris.csv") # 這里的iris數(shù)據(jù)已做過(guò)處理
m, n = data.shape
dataMatIn = np.ones((m, n))
dataMatIn[:, :-1] = data.ix[:, :-1]
classLabels = data.ix[:, -1]
# sigmoid函數(shù)和初始化數(shù)據(jù)
def sigmoid(z):
return 1 / (1 + np.exp(-z))
# 隨機(jī)梯度下降
def Stocgrad_descent(dataMatIn, classLabels):
dataMatrix = np.mat(dataMatIn) # 訓(xùn)練集
labelMat = np.mat(classLabels).transpose() # y值
m, n = np.shape(dataMatrix) # m:dataMatrix的行數(shù),n:dataMatrix的列數(shù)
weights = np.ones((n, 1)) # 初始化回歸系數(shù)(n, 1)
alpha = 0.001 # 步長(zhǎng)
maxCycle = 500 # 最大循環(huán)次數(shù)
epsilon = 0.001
error = np.zeros((n,1))
for i in range(maxCycle):
for j in range(m):
h = sigmoid(dataMatrix * weights) # sigmoid 函數(shù)
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) # 根據(jù)模型預(yù)測(cè)的概率
# 預(yù)測(cè)結(jié)果二值化
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數(shù)據(jù)集 # 第一次運(yùn)行此代碼時(shí)此步驟不要
data.to_csv("data_and_pred.csv", index=False, encoding="utf_8_sig") # 數(shù)據(jù)集保存
pred_result(dataMatIn)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python numpy實(shí)現(xiàn)多次循環(huán)讀取文件 等間隔過(guò)濾數(shù)據(jù)示例
這篇文章主要介紹了python numpy實(shí)現(xiàn)多次循環(huán)讀取文件 等間隔過(guò)濾數(shù)據(jù)示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
使用Python快樂(lè)學(xué)數(shù)學(xué)Github萬(wàn)星神器Manim簡(jiǎn)介
這篇文章主要介紹了使用Python快樂(lè)學(xué)數(shù)學(xué)Github萬(wàn)星神器Manim簡(jiǎn)介,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08
python高并發(fā)異步服務(wù)器核心庫(kù)forkcore使用方法
這篇文章主要介紹了python高并發(fā)異步服務(wù)器核心庫(kù)forkcore的使用方法,大家參考使用吧2013-11-11
Python進(jìn)行數(shù)據(jù)分析的5大常見(jiàn)錯(cuò)誤與解決
數(shù)據(jù)分析已成為現(xiàn)代商業(yè)決策和科學(xué)研究中不可或缺的一部分,而Python憑借其強(qiáng)大的生態(tài)系統(tǒng)和易用性,成為了數(shù)據(jù)分析師的首選工具,本文將深入剖析Python數(shù)據(jù)分析中最危險(xiǎn)的5大禁忌,大家可以參考一下2025-06-06
Python 格式化打印json數(shù)據(jù)方法(展開(kāi)狀態(tài))
今天小編就為大家分享一篇Python 格式化打印json數(shù)據(jù)方法(展開(kāi)狀態(tài)),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02
基于python3實(shí)現(xiàn)socket文件傳輸和校驗(yàn)
這篇文章主要為大家詳細(xì)介紹了基于python3實(shí)現(xiàn)socket文件傳輸和校驗(yàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
Python持續(xù)監(jiān)聽(tīng)文件變化代碼實(shí)例
這篇文章主要介紹了Python持續(xù)監(jiān)聽(tīng)文件變化代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
yolov5調(diào)用usb攝像頭及本地?cái)z像頭的方法實(shí)例
YOLOV5模型從發(fā)布到現(xiàn)在都是炙手可熱的目標(biāo)檢測(cè)模型,被廣泛運(yùn)用于各大場(chǎng)景之中,下面這篇文章主要給大家介紹了關(guān)于yolov5調(diào)用usb攝像頭及本地?cái)z像頭的相關(guān)資料,需要的朋友可以參考下2022-03-03
python 繪圖模塊matplotlib的使用簡(jiǎn)介
這篇文章主要介紹了python 繪圖模塊matplotlib的使用簡(jiǎn)介,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-03-03

