亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

使用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)
? ? #進行邏輯回歸預(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):
? ? ? ? #隨機獲取一個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]
? ? ? ? #進行一次梯度更新(優(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')
? ? #獲取樣本屬性和標簽
? ? 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')
? ? #對測試集進行預(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é)習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 徹底搞懂Python字符編碼

    徹底搞懂Python字符編碼

    本篇文章帶領(lǐng)大家徹底搞懂Python字符編碼的一些知識,及python字符編碼的一些基礎(chǔ)概念,需要的朋友可以參考下
    2018-01-01
  • 解決python3中自定義wsgi函數(shù),make_server函數(shù)報錯的問題

    解決python3中自定義wsgi函數(shù),make_server函數(shù)報錯的問題

    下面小編就為大家分享一篇解決python3中自定義wsgi函數(shù),make_server函數(shù)報錯的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-11-11
  • python計算兩個數(shù)的百分比方法

    python計算兩個數(shù)的百分比方法

    今天小編就為大家分享一篇python計算兩個數(shù)的百分比方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • 淺析PHP與Python進行數(shù)據(jù)交互

    淺析PHP與Python進行數(shù)據(jù)交互

    本篇文章給大家分享了PHP與Python進行數(shù)據(jù)交互的詳細方法以及重點點撥,有興趣的朋友可以學(xué)習下。
    2018-05-05
  • python繪制多個子圖的實例

    python繪制多個子圖的實例

    今天小編就為大家分享一篇python繪制多個子圖的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • python定義類的簡單用法

    python定義類的簡單用法

    在本篇文章里小編給大家分享的是一篇關(guān)于python定義類的簡單用法,需要的朋友們可以參考下。
    2020-07-07
  • Python selenium爬取微信公眾號文章代碼詳解

    Python selenium爬取微信公眾號文章代碼詳解

    這篇文章主要介紹了Python selenium爬取微信公眾號歷史文章代碼詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友可以參考下
    2020-08-08
  • python創(chuàng)建和使用字典實例詳解

    python創(chuàng)建和使用字典實例詳解

    字典是python中唯一內(nèi)建的映射類型。字典中的值并沒有特殊的順序,但是都存儲在一個特定的鍵(key)里。
    2013-11-11
  • 連接Python程序與MySQL的教程

    連接Python程序與MySQL的教程

    這篇文章主要介紹了連接Python程序與MySQL的教程,MySQL作為最具人氣的數(shù)據(jù)庫,與程序之間的連接也成為了如今Python學(xué)習中近乎必備的知識,需要的朋友可以參考下
    2015-04-04
  • python計算機視覺OpenCV入門講解

    python計算機視覺OpenCV入門講解

    這篇文章主要介紹了python計算機視覺OpenCV入門講解,關(guān)于圖像處理的相關(guān)簡單操作,包括讀入圖像、顯示圖像及圖像相關(guān)理論知識
    2022-06-06

最新評論