Python實現(xiàn)的邏輯回歸算法示例【附測試csv文件下載】
本文實例講述了Python實現(xiàn)的邏輯回歸算法。分享給大家供大家參考,具體如下:
使用python實現(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)) #計算預(yù)測值
error = h-y_train.T #計算預(yù)測值與訓(xùn)練集的差值
delt=alpha*(np.dot(error,x_train))/m #計算參數(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文件點擊此處本站下載。
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)學(xué)運算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計有所幫助。
- 基于Pytorch實現(xiàn)邏輯回歸
- PyTorch實現(xiàn)多維度特征輸入邏輯回歸
- pytorch實現(xiàn)邏輯回歸
- PyTorch線性回歸和邏輯回歸實戰(zhàn)示例
- python sklearn庫實現(xiàn)簡單邏輯回歸的實例代碼
- python實現(xiàn)邏輯回歸的方法示例
- python編寫Logistic邏輯回歸
- python代碼實現(xiàn)邏輯回歸logistic原理
- Python利用邏輯回歸模型解決MNIST手寫數(shù)字識別問題詳解
- python 牛頓法實現(xiàn)邏輯回歸(Logistic Regression)
- Python利用邏輯回歸分類實現(xiàn)模板
- python機器學(xué)習(xí)理論與實戰(zhàn)(四)邏輯回歸
- pytorch使用nn.Moudle實現(xiàn)邏輯回歸
相關(guān)文章
python列表添加元素append(),extend(),insert(),+list的區(qū)別及說明
這篇文章主要介紹了python列表添加元素append(),extend(), insert(),+list的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
Python實現(xiàn)將mp3音頻格式轉(zhuǎn)換為wav格式
這篇文章主要介紹了利用python寫了這個小工具,可以批量進行mp3音頻格式轉(zhuǎn)換為wav格式,文中的示例代碼講解詳細(xì),感興趣的可以學(xué)習(xí)一下2022-01-01
python之文件的讀寫和文件目錄以及文件夾的操作實現(xiàn)代碼
這篇文章主要介紹了python之文件的讀寫和文件目錄以及文件夾的操作實現(xiàn)代碼,需要的朋友可以參考下2016-08-08

