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

python 牛頓法實(shí)現(xiàn)邏輯回歸(Logistic Regression)

 更新時(shí)間:2020年10月15日 10:42:39   作者:農(nóng)大魯迅  
這篇文章主要介紹了python 牛頓法實(shí)現(xiàn)邏輯回歸(Logistic Regression),幫助大家更好的進(jìn)行機(jī)器學(xué)習(xí),感興趣的朋友可以了解下

本文采用的訓(xùn)練方法是牛頓法(Newton Method)。

代碼

import numpy as np

class LogisticRegression(object):
 """
 Logistic Regression Classifier training by Newton Method
 """

 def __init__(self, error: float = 0.7, max_epoch: int = 100):
  """
  :param error: float, if the distance between new weight and 
      old weight is less than error, the process 
      of traing will break.
  :param max_epoch: if training epoch >= max_epoch the process 
       of traing will break.
  """
  self.error = error
  self.max_epoch = max_epoch
  self.weight = None
  self.sign = np.vectorize(lambda x: 1 if x >= 0.5 else 0)

 def p_func(self, X_):
  """Get P(y=1 | x)
  :param X_: shape = (n_samples + 1, n_features)
  :return: shape = (n_samples)
  """
  tmp = np.exp(self.weight @ X_.T)
  return tmp / (1 + tmp)

 def diff(self, X_, y, p):
  """Get derivative
  :param X_: shape = (n_samples, n_features + 1) 
  :param y: shape = (n_samples)
  :param p: shape = (n_samples) P(y=1 | x)
  :return: shape = (n_features + 1) first derivative
  """
  return -(y - p) @ X_

 def hess_mat(self, X_, p):
  """Get Hessian Matrix
  :param p: shape = (n_samples) P(y=1 | x)
  :return: shape = (n_features + 1, n_features + 1) second derivative
  """
  hess = np.zeros((X_.shape[1], X_.shape[1]))
  for i in range(X_.shape[0]):
   hess += self.X_XT[i] * p[i] * (1 - p[i])
  return hess

 def newton_method(self, X_, y):
  """Newton Method to calculate weight
  :param X_: shape = (n_samples + 1, n_features)
  :param y: shape = (n_samples)
  :return: None
  """
  self.weight = np.ones(X_.shape[1])
  self.X_XT = []
  for i in range(X_.shape[0]):
   t = X_[i, :].reshape((-1, 1))
   self.X_XT.append(t @ t.T)

  for _ in range(self.max_epoch):
   p = self.p_func(X_)
   diff = self.diff(X_, y, p)
   hess = self.hess_mat(X_, p)
   new_weight = self.weight - (np.linalg.inv(hess) @ diff.reshape((-1, 1))).flatten()

   if np.linalg.norm(new_weight - self.weight) <= self.error:
    break
   self.weight = new_weight

 def fit(self, X, y):
  """
  :param X_: shape = (n_samples, n_features)
  :param y: shape = (n_samples)
  :return: self
  """
  X_ = np.c_[np.ones(X.shape[0]), X]
  self.newton_method(X_, y)
  return self

 def predict(self, X) -> np.array:
  """
  :param X: shape = (n_samples, n_features] 
  :return: shape = (n_samples]
  """
  X_ = np.c_[np.ones(X.shape[0]), X]
  return self.sign(self.p_func(X_))

測(cè)試代碼

import matplotlib.pyplot as plt
import sklearn.datasets

def plot_decision_boundary(pred_func, X, y, title=None):
 """分類器畫圖函數(shù),可畫出樣本點(diǎn)和決策邊界
 :param pred_func: predict函數(shù)
 :param X: 訓(xùn)練集X
 :param y: 訓(xùn)練集Y
 :return: None
 """

 # Set min and max values and give it some padding
 x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
 y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
 h = 0.01
 # Generate a grid of points with distance h between them
 xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
 # Predict the function value for the whole gid
 Z = pred_func(np.c_[xx.ravel(), yy.ravel()])
 Z = Z.reshape(xx.shape)
 # Plot the contour and training examples
 plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral)
 plt.scatter(X[:, 0], X[:, 1], s=40, c=y, cmap=plt.cm.Spectral)
 if title:
  plt.title(title)
 plt.show()

效果

更多機(jī)器學(xué)習(xí)代碼,請(qǐng)?jiān)L問 https://github.com/WiseDoge/plume

以上就是python 牛頓法實(shí)現(xiàn)邏輯回歸(Logistic Regression)的詳細(xì)內(nèi)容,更多關(guān)于python 邏輯回歸的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python獲取時(shí)間的操作示例詳解

    Python獲取時(shí)間的操作示例詳解

    這篇文章主要為大家詳細(xì)介紹了一些Python中獲取時(shí)間的操作,例如:獲取時(shí)間戳、獲取當(dāng)前時(shí)間、獲取昨天日期等,感興趣的可以參考一下
    2022-07-07
  • Python+Selenium實(shí)現(xiàn)網(wǎng)站滑塊拖動(dòng)操作

    Python+Selenium實(shí)現(xiàn)網(wǎng)站滑塊拖動(dòng)操作

    這篇文章主要為大家詳細(xì)介紹了如何利用Python+Selenium模擬實(shí)現(xiàn)登錄某網(wǎng)站的滑塊拖動(dòng)操作,文中的示例代碼講解詳細(xì),需要的可以參考一下
    2022-09-09
  • Python異常處理操作實(shí)例詳解

    Python異常處理操作實(shí)例詳解

    這篇文章主要介紹了Python異常處理操作,結(jié)合實(shí)例形式分析了Python常見的異常處理類型、相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下
    2018-08-08
  • Python正則表達(dá)式高級(jí)使用方法匯總

    Python正則表達(dá)式高級(jí)使用方法匯總

    這篇文章主要介紹了Python正則表達(dá)式高級(jí)使用方法解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • anaconda中更改python版本的方法步驟

    anaconda中更改python版本的方法步驟

    這篇文章主要介紹了anaconda中更改python版本的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • python實(shí)現(xiàn)簡(jiǎn)單的飛機(jī)大戰(zhàn)

    python實(shí)現(xiàn)簡(jiǎn)單的飛機(jī)大戰(zhàn)

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡(jiǎn)單的飛機(jī)大戰(zhàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Python使用pip安裝Matplotlib的方法詳解

    Python使用pip安裝Matplotlib的方法詳解

    在網(wǎng)上看見許多matplotlib的安裝教程都是比較復(fù)雜,需要配置許多環(huán)境,對(duì)于電腦基礎(chǔ)不好的人來(lái)說可是一件頭疼的事情,今天我介紹一個(gè)簡(jiǎn)單的安裝方法,下面這篇文章主要給大家介紹了關(guān)于Python使用pip安裝Matplotlib的相關(guān)資料,需要的朋友可以參考下
    2022-07-07
  • Python paramiko模塊使用解析(實(shí)現(xiàn)ssh)

    Python paramiko模塊使用解析(實(shí)現(xiàn)ssh)

    這篇文章主要介紹了Python paramiko模塊使用解析(實(shí)現(xiàn)ssh),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • 通過python掃描二維碼/條形碼并打印數(shù)據(jù)

    通過python掃描二維碼/條形碼并打印數(shù)據(jù)

    這篇文章主要介紹了通過python掃描二維碼/條形碼并打印數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • python 爬蟲一鍵爬取 淘寶天貓寶貝頁(yè)面主圖顏色圖和詳情圖的教程

    python 爬蟲一鍵爬取 淘寶天貓寶貝頁(yè)面主圖顏色圖和詳情圖的教程

    今天小編就為大家分享一篇python 爬蟲一鍵爬取 淘寶天貓寶貝頁(yè)面主圖顏色圖和詳情圖的教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧
    2018-05-05

最新評(píng)論

光山县| 武安市| 威海市| 连南| 郎溪县| 台南县| 富蕴县| 巫山县| 山西省| 扎鲁特旗| 武平县| 新巴尔虎右旗| 河西区| 宁南县| 北辰区| 尚义县| 肥城市| 平乡县| 焦作市| 马关县| 清徐县| 东源县| 泗阳县| 云和县| 东台市| 凤台县| 高要市| 巨鹿县| 滦南县| 洱源县| 曲靖市| 剑川县| 林西县| 高唐县| 浏阳市| 东乡县| 民丰县| 淮滨县| 乌拉特中旗| 沙雅县| 讷河市|