網站首頁 編程語言 正文
使用python的numpy模塊實現邏輯回歸模型的代碼,供大家參考,具體內容如下
使用了numpy模塊,pandas模塊,matplotlib模塊
1.初始化參數
def initial_para(nums_feature):
? ? """initial the weights and bias which is zero"""
? ? #nums_feature是輸入數據的屬性數目,因此權重w是[1, nums_feature]維
? ? #且w和b均初始化為0
? ? w = np.zeros((1, nums_feature))
? ? b = 0
? ? return w, b
2.邏輯回歸方程
def activation(x, w , b):
? ? """a linear function and then sigmoid activation function:?
? ? x_ = w*x +b,y = 1/(1+exp(-x_))"""
? ? #線性方程,輸入的x是[batch, 2]維,輸出是[1, batch]維,batch是模型優化迭代一次輸入數據的數目
? ? #[1, 2] * [2, batch] = [1, batch], 所以是w * x.T(x的轉置)
? ? #np.dot是矩陣乘法
? ? x_ = np.dot(w, x.T) + b
? ? #np.exp是實現e的x次冪
? ? sigmoid = 1 / (1 + np.exp(-x_))
? ? return sigmoid
3.梯度下降
def gradient_descent_batch(x, w, b, label, learning_rate):
? ? #獲取輸入數據的數目,即batch大小
? ? n = len(label)
? ? #進行邏輯回歸預測
? ? sigmoid = activation(x, w, b)
? ? #損失函數,np.sum是將矩陣求和
? ? cost = -np.sum(label.T * np.log(sigmoid) + (1-label).T * np.log(1-sigmoid)) / n
? ? #求對w和b的偏導(即梯度值)
? ? g_w = np.dot(x.T, (sigmoid - label.T).T) / n
? ? g_b = np.sum((sigmoid - label.T)) / n
? ? #根據梯度更新參數
? ? w = w - learning_rate * g_w.T
? ? b = b - learning_rate * g_b
? ? return w, b, cost
4.模型優化
def optimal_model_batch(x, label, nums_feature, step=10000, batch_size=1):
? ? """train the model with batch"""
? ? length = len(x)
? ? w, b = initial_para(nums_feature)
? ? for i in range(step):
? ? ? ? #隨機獲取一個batch數目的數據
? ? ? ? num = randint(0, length - 1 - batch_size)
? ? ? ? x_batch = x[num:(num+batch_size), :]
? ? ? ? label_batch = label[num:num+batch_size]
? ? ? ? #進行一次梯度更新(優化)
? ? ? ? w, b, cost = gradient_descent_batch(x_batch, w, b, label_batch, 0.0001)
? ? ? ? #每1000次打印一下損失值
? ? ? ? if i%1000 == 0:
? ? ? ? ? ? print('step is : ', i, ', cost is: ', cost)
? ? return w, b
5.讀取數據,數據預處理,訓練模型,評估精度
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from random import randint
from sklearn.preprocessing import StandardScaler
?
def _main():
? ? #讀取csv格式的數據data_path是數據的路徑
? ? data = pd.read_csv('data_path')
? ? #獲取樣本屬性和標簽
? ? x = data.iloc[:, 2:4].values
? ? y = data.iloc[:, 4].values
? ? #將數據集分為測試集和訓練集
? ? x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2, random_state=0)
? ? #數據預處理,去均值化
? ? standardscaler = StandardScaler()
? ? x_train = standardscaler.fit_transform(x_train)
? ? x_test = standardscaler.transform(x_test)
? ? #w, b = optimal_model(x_train, y_train, 2, 50000)
? ? #訓練模型
? ? w, b = optimal_model_batch(x_train, y_train, 2, 50000, 64)
? ? print('trian is over')
? ? #對測試集進行預測,并計算精度
? ? predict = activation(x_test, w, b).T
? ? n = 0
? ? for i, p in enumerate(predict):
? ? ? ? if p >=0.5:
? ? ? ? ? ? if y_test[i] == 1:
? ? ? ? ? ? ? ? n += 1
? ? ? ? else:
? ? ? ? ? ? if y_test[i] == 0:
? ? ? ? ? ? ? ? n += 1
? ? print('accuracy is : ', n / len(y_test))
6.結果可視化
predict = np.reshape(np.int32(predict), [len(predict)])
? ? #將預測結果以散點圖的形式可視化
? ? for i, j in enumerate(np.unique(predict)):
? ? ? ? plt.scatter(x_test[predict == j, 0], x_test[predict == j, 1],?
? ? ? ? c = ListedColormap(('red', 'blue'))(i), label=j)
? ? plt.show()
原文鏈接:https://blog.csdn.net/qq_35153620/article/details/95763896
相關推薦
- 2022-07-06 Flutter?DateTime日期轉換的詳細使用_Android
- 2023-10-30 解決docker拉取鏡像時報錯Error response from daemon: Get ““:
- 2022-09-13 GO語言包管理工具go?mod以及包詳解_Golang
- 2022-07-24 基于python實現雙向鏈表_python
- 2022-03-23 C++函數模板的使用詳解_C 語言
- 2022-12-09 python中為main方法傳參問題_python
- 2022-08-02 C#如何Task執行任務,等待任務完成_C#教程
- 2023-03-19 教你如何實現在react項目中嵌入Blazor_React
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支