最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python的numpy模塊實現(xiàn)邏輯回歸模型

 更新時間:2022年07月30日 10:02:26   作者:上進(jìn)的小菜鳥  
這篇文章主要為大家詳細(xì)介紹了python的numpy模塊實現(xiàn)邏輯回歸模型,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

使用python的numpy模塊實現(xiàn)邏輯回歸模型的代碼,供大家參考,具體內(nèi)容如下

使用了numpy模塊,pandas模塊,matplotlib模塊

1.初始化參數(shù)

def initial_para(nums_feature):
? ? """initial the weights and bias which is zero"""
? ? #nums_feature是輸入數(shù)據(jù)的屬性數(shù)目,因此權(quán)重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是模型優(yōu)化迭代一次輸入數(shù)據(jù)的數(shù)目
? ? #[1, 2] * [2, batch] = [1, batch], 所以是w * x.T(x的轉(zhuǎn)置)
? ? #np.dot是矩陣乘法
? ? x_ = np.dot(w, x.T) + b
? ? #np.exp是實現(xiàn)e的x次冪
? ? sigmoid = 1 / (1 + np.exp(-x_))
? ? return sigmoid

3.梯度下降

def gradient_descent_batch(x, w, b, label, learning_rate):
? ? #獲取輸入數(shù)據(jù)的數(shù)目,即batch大小
? ? n = len(label)
? ? #進(jìn)行邏輯回歸預(yù)測
? ? sigmoid = activation(x, w, b)
? ? #損失函數(shù),np.sum是將矩陣求和
? ? cost = -np.sum(label.T * np.log(sigmoid) + (1-label).T * np.log(1-sigmoid)) / n
? ? #求對w和b的偏導(dǎo)(即梯度值)
? ? g_w = np.dot(x.T, (sigmoid - label.T).T) / n
? ? g_b = np.sum((sigmoid - label.T)) / n
? ? #根據(jù)梯度更新參數(shù)
? ? w = w - learning_rate * g_w.T
? ? b = b - learning_rate * g_b
? ? return w, b, cost

4.模型優(yōu)化

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):
? ? ? ? #隨機(jī)獲取一個batch數(shù)目的數(shù)據(jù)
? ? ? ? num = randint(0, length - 1 - batch_size)
? ? ? ? x_batch = x[num:(num+batch_size), :]
? ? ? ? label_batch = label[num:num+batch_size]
? ? ? ? #進(jìn)行一次梯度更新(優(yōu)化)
? ? ? ? w, b, cost = gradient_descent_batch(x_batch, w, b, label_batch, 0.0001)
? ? ? ? #每1000次打印一下?lián)p失值
? ? ? ? if i%1000 == 0:
? ? ? ? ? ? print('step is : ', i, ', cost is: ', cost)
? ? return w, b

5.讀取數(shù)據(jù),數(shù)據(jù)預(yù)處理,訓(xùn)練模型,評估精度

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格式的數(shù)據(jù)data_path是數(shù)據(jù)的路徑
? ? data = pd.read_csv('data_path')
? ? #獲取樣本屬性和標(biāo)簽
? ? x = data.iloc[:, 2:4].values
? ? y = data.iloc[:, 4].values
? ? #將數(shù)據(jù)集分為測試集和訓(xùn)練集
? ? x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2, random_state=0)
? ? #數(shù)據(jù)預(yù)處理,去均值化
? ? 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)
? ? #訓(xùn)練模型
? ? w, b = optimal_model_batch(x_train, y_train, 2, 50000, 64)
? ? print('trian is over')
? ? #對測試集進(jìn)行預(yù)測,并計算精度
? ? 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.結(jié)果可視化

predict = np.reshape(np.int32(predict), [len(predict)])
? ? #將預(yù)測結(jié)果以散點圖的形式可視化
? ? 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()

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • python打印當(dāng)前文件的絕對路徑并解決打印為空的問題

    python打印當(dāng)前文件的絕對路徑并解決打印為空的問題

    這篇文章主要介紹了python打印當(dāng)前文件的絕對路徑并解決打印為空的問題,文中補(bǔ)充介紹了python中對文件路徑的獲取方法,需要的朋友可以參考下
    2023-03-03
  • Python實現(xiàn)普通圖片轉(zhuǎn)ico圖標(biāo)的方法詳解

    Python實現(xiàn)普通圖片轉(zhuǎn)ico圖標(biāo)的方法詳解

    ICO是一種圖標(biāo)文件格式,圖標(biāo)文件可以存儲單個圖案、多尺寸、多色板的圖標(biāo)文件。本文將利用Python實現(xiàn)普通圖片轉(zhuǎn)ico圖標(biāo),感興趣的小伙伴可以了解一下
    2022-11-11
  • python 多進(jìn)程和協(xié)程配合使用寫入數(shù)據(jù)

    python 多進(jìn)程和協(xié)程配合使用寫入數(shù)據(jù)

    這篇文章主要介紹了python 多進(jìn)程和協(xié)程配合使用寫入數(shù)據(jù),幫助大家利用python高效辦公,感興趣的朋友可以了解下
    2020-10-10
  • python讀取和保存圖片5種方法對比

    python讀取和保存圖片5種方法對比

    為大家分享一下python讀取和保存圖片5種方法與比較,python中對象之間的賦值是按引用傳遞的,如果需要拷貝對象,需要用到標(biāo)準(zhǔn)庫中的copy模塊
    2018-09-09
  • Django?CSRF驗證失敗請求被中斷的問題

    Django?CSRF驗證失敗請求被中斷的問題

    這篇文章主要介紹了Django?CSRF驗證失敗請求被中斷的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Python入門教程(二十四)Python的迭代器

    Python入門教程(二十四)Python的迭代器

    這篇文章主要介紹了Python入門教程(二十四)Python的迭代器,Python是一門非常強(qiáng)大好用的語言,也有著易上手的特性,本文為入門教程,需要的朋友可以參考下
    2023-04-04
  • PyTorch中torch.load()的用法和應(yīng)用

    PyTorch中torch.load()的用法和應(yīng)用

    torch.load()它用于加載由torch.save()保存的模型或張量,本文主要介紹了PyTorch中torch.load()的用法和應(yīng)用,具有一定的參考價值,感興趣的可以了解一下
    2024-03-03
  • 利用pandas合并多個excel的方法示例

    利用pandas合并多個excel的方法示例

    這篇文章主要介紹了利用pandas合并多個excel的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • python讀取圖片并修改格式與大小的方法

    python讀取圖片并修改格式與大小的方法

    這篇文章主要為大家詳細(xì)介紹了python讀取圖片并修改格式與大小的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • python 將字符串中的數(shù)字相加求和的實現(xiàn)

    python 將字符串中的數(shù)字相加求和的實現(xiàn)

    這篇文章主要介紹了python 將字符串中的數(shù)字相加求和的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07

最新評論

土默特左旗| 白玉县| 平乐县| 远安县| 九龙坡区| 汉阴县| 边坝县| 泾源县| 安塞县| 阿图什市| 读书| 改则县| 平凉市| 贺州市| 浮梁县| 体育| 连平县| 桓台县| 山西省| 定日县| 南康市| 宝山区| 仲巴县| 汤原县| 韶关市| 星座| 信丰县| 沐川县| 海淀区| 佳木斯市| 武山县| 讷河市| 舒城县| 石林| 金华市| 恩施市| 灵武市| 扶风县| 通州区| 盐池县| 仲巴县|