Python實(shí)現(xiàn)的邏輯回歸算法示例【附測試csv文件下載】
本文實(shí)例講述了Python實(shí)現(xiàn)的邏輯回歸算法。分享給大家供大家參考,具體如下:
使用python實(shí)現(xiàn)邏輯回歸
Using Python to Implement Logistic Regression Algorithm
菜鳥寫的邏輯回歸,記錄一下學(xué)習(xí)過程
代碼:
#encoding:utf-8 """ Author: njulpy Version: 1.0 Data: 2018/04/10 Project: Using Python to Implement LogisticRegression Algorithm """ import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.model_selection import train_test_split #建立sigmoid函數(shù) def sigmoid(x): x = x.astype(float) return 1./(1+np.exp(-x)) #訓(xùn)練模型,采用梯度下降算法 def train(x_train,y_train,num,alpha,m,n): beta = np.ones(n) for i in range(num): h=sigmoid(np.dot(x_train,beta)) #計(jì)算預(yù)測值 error = h-y_train.T #計(jì)算預(yù)測值與訓(xùn)練集的差值 delt=alpha*(np.dot(error,x_train))/m #計(jì)算參數(shù)的梯度變化值 beta = beta - delt #print('error',error) return beta def predict(x_test,beta): y_predict=np.zeros(len(y_test))+0.5 s=sigmoid(np.dot(beta,x_test.T)) y_predict[s < 0.34] = 0 y_predict[s > 0.67] = 1 return y_predict def accurancy(y_predict,y_test): acc=1-np.sum(np.absolute(y_predict-y_test))/len(y_test) return acc if __name__ == "__main__": data = pd.read_csv('iris.csv') x = data.iloc[:,1:5] y = data.iloc[:,5].copy() y.loc[y== 'setosa'] = 0 y.loc[y== 'versicolor'] = 0.5 y.loc[y== 'virginica'] = 1 x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.3,random_state=15) m,n=np.shape(x_train) alpha = 0.01 beta=train(x_train,y_train,1000,alpha,m,n) pre=predict(x_test,beta) t = np.arange(len(x_test)) plt.figure() p1 = plt.plot(t,pre) p2 = plt.plot(t,y_test,label='test') label = ['prediction', 'true'] plt.legend(label, loc=1) plt.show() acc=accurancy(pre,y_test) print('The predicted value is ',pre) print('The true value is ',np.array(y_test)) print('The accuracy rate is ',acc)
輸出結(jié)果:
The predicted value is [ 0. 0.5 1. 0. 0. 1. 1. 0.5 1. 1. 1. 0.5 0.5 0.5 1.
0. 0.5 1. 0. 1. 0.5 0. 0.5 0.5 0. 0. 1. 1. 1. 1.
0. 1. 1. 1. 0. 0. 1. 0. 0. 0.5 1. 0. 0. 0.5 1. ]
The true value is [0 0.5 0.5 0 0 0.5 1 0.5 0.5 1 1 0.5 0.5 0.5 1 0 0.5 1 0 1 0.5 0 0.5 0.5 0
0 1 1 1 0.5 0 1 0.5 1 0 0 1 0 0 0.5 1 0 0 0.5 1]
The accuracy rate is 0.9444444444444444
附:上述示例中的iris.csv文件點(diǎn)擊此處本站下載。
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計(jì)有所幫助。
- 基于Pytorch實(shí)現(xiàn)邏輯回歸
- PyTorch實(shí)現(xiàn)多維度特征輸入邏輯回歸
- pytorch實(shí)現(xiàn)邏輯回歸
- PyTorch線性回歸和邏輯回歸實(shí)戰(zhàn)示例
- python sklearn庫實(shí)現(xiàn)簡單邏輯回歸的實(shí)例代碼
- python實(shí)現(xiàn)邏輯回歸的方法示例
- python編寫Logistic邏輯回歸
- python代碼實(shí)現(xiàn)邏輯回歸logistic原理
- Python利用邏輯回歸模型解決MNIST手寫數(shù)字識別問題詳解
- python 牛頓法實(shí)現(xiàn)邏輯回歸(Logistic Regression)
- Python利用邏輯回歸分類實(shí)現(xiàn)模板
- python機(jī)器學(xué)習(xí)理論與實(shí)戰(zhàn)(四)邏輯回歸
- pytorch使用nn.Moudle實(shí)現(xiàn)邏輯回歸
相關(guān)文章
python保留格式匯總各部門excel內(nèi)容的實(shí)現(xiàn)思路
這篇文章主要介紹了python保留格式匯總各部門excel內(nèi)容,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06Django之使用celery和NGINX生成靜態(tài)頁面實(shí)現(xiàn)性能優(yōu)化
這篇文章主要介紹了Django之使用celery和NGINX生成靜態(tài)頁面實(shí)現(xiàn)性能優(yōu)化,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10python使用xpath中遇到:<Element a at 0x39a9a80>到底是什么?
這篇文章主要給大家詳細(xì)介紹了關(guān)于python使用xpath中遇到:<Element a at 0x39a9a80>的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01Python+Tableau廣東省人口普查可視化的實(shí)現(xiàn)
本文將結(jié)合實(shí)例代碼,介紹Python+Tableau廣東省人口普查可視化,第七次人口普查數(shù)據(jù)分析,繪制歷次人口普查人口數(shù)量變化圖,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06python 監(jiān)控服務(wù)器是否有人遠(yuǎn)程登錄(詳細(xì)思路+代碼)
這篇文章主要介紹了python 監(jiān)控服務(wù)器是否有人遠(yuǎn)程登錄的方法,幫助大家利用python 監(jiān)控服務(wù)器,感興趣的朋友可以了解下2020-12-12python文字和unicode/ascll相互轉(zhuǎn)換函數(shù)及簡單加密解密實(shí)現(xiàn)代碼
這篇文章主要介紹了python文字和unicode/ascll相互轉(zhuǎn)換函數(shù)及簡單加密解密實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-08-08Python 數(shù)據(jù)處理庫 pandas 入門教程基本操作
pandas是一個Python語言的軟件包,在我們使用Python語言進(jìn)行機(jī)器學(xué)習(xí)編程的時候,這是一個非常常用的基礎(chǔ)編程庫。本文是對Python 數(shù)據(jù)處理庫 pandas 入門教程,非常不錯,感興趣的朋友一起看看吧2018-04-04python判斷鏈表是否有環(huán)的實(shí)例代碼
在本篇文章里小編給大家整理的是關(guān)于python判斷鏈表是否有環(huán)的知識點(diǎn)及實(shí)例代碼,需要的朋友們參考下。2020-01-01Python使用jsonpath-rw模塊處理Json對象操作示例
這篇文章主要介紹了Python使用jsonpath-rw模塊處理Json對象操作,結(jié)合實(shí)例形式分析了Python使用requests與response處理json的方法,并給出了jsonpath_rw模塊操作json對象的基本示例,需要的朋友可以參考下2018-07-07