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

Python 實(shí)現(xiàn)3種回歸模型(Linear Regression,Lasso,Ridge)的示例

 更新時(shí)間:2020年10月15日 10:43:27   作者:農(nóng)大魯迅  
這篇文章主要介紹了Python 實(shí)現(xiàn) 3 種回歸模型(Linear Regression,Lasso,Ridge)的示例,幫助大家更好的進(jìn)行機(jī)器學(xué)習(xí),感興趣的朋友可以了解下

公共的抽象基類(lèi)

import numpy as np
from abc import ABCMeta, abstractmethod


class LinearModel(metaclass=ABCMeta):
 """
 Abstract base class of Linear Model.
 """

 def __init__(self):
  # Before fit or predict, please transform samples' mean to 0, var to 1.
  self.scaler = StandardScaler()

 @abstractmethod
 def fit(self, X, y):
  """fit func"""

 def predict(self, X):
  # before predict, you must run fit func.
  if not hasattr(self, 'coef_'):
   raise Exception('Please run `fit` before predict')

  X = self.scaler.transform(X)
  X = np.c_[np.ones(X.shape[0]), X]

  # `x @ y` == `np.dot(x, y)`
  return X @ self.coef_

Linear Regression

class LinearRegression(LinearModel):
 """
 Linear Regression.
 """

 def __init__(self):
  super().__init__()

 def fit(self, X, y):
  """
  :param X_: shape = (n_samples + 1, n_features)
  :param y: shape = (n_samples])
  :return: self
  """
  self.scaler.fit(X)
  X = self.scaler.transform(X)
  X = np.c_[np.ones(X.shape[0]), X]
  self.coef_ = np.linalg.inv(X.T @ X) @ X.T @ y
  return self

Lasso

class Lasso(LinearModel):
 """
 Lasso Regression, training by Coordinate Descent.
 cost = ||X @ coef_||^2 + alpha * ||coef_||_1
 """
 def __init__(self, alpha=1.0, n_iter=1000, e=0.1):
  self.alpha = alpha
  self.n_iter = n_iter
  self.e = e
  super().__init__()

 def fit(self, X, y):
  self.scaler.fit(X)
  X = self.scaler.transform(X)
  X = np.c_[np.ones(X.shape[0]), X]
  self.coef_ = np.zeros(X.shape[1])
  for _ in range(self.n_iter):
   z = np.sum(X * X, axis=0)
   tmp = np.zeros(X.shape[1])
   for k in range(X.shape[1]):
    wk = self.coef_[k]
    self.coef_[k] = 0
    p_k = X[:, k] @ (y - X @ self.coef_)
    if p_k < -self.alpha / 2:
     w_k = (p_k + self.alpha / 2) / z[k]
    elif p_k > self.alpha / 2:
     w_k = (p_k - self.alpha / 2) / z[k]
    else:
     w_k = 0
    tmp[k] = w_k
    self.coef_[k] = wk
   if np.linalg.norm(self.coef_ - tmp) < self.e:
    break
   self.coef_ = tmp
  return self

Ridge

class Ridge(LinearModel):
 """
 Ridge Regression.
 """

 def __init__(self, alpha=1.0):
  self.alpha = alpha
  super().__init__()

 def fit(self, X, y):
  """
  :param X_: shape = (n_samples + 1, n_features)
  :param y: shape = (n_samples])
  :return: self
  """
  self.scaler.fit(X)
  X = self.scaler.transform(X)
  X = np.c_[np.ones(X.shape[0]), X]
  self.coef_ = np.linalg.inv(
   X.T @ X + self.alpha * np.eye(X.shape[1])) @ X.T @ y
  return self

測(cè)試代碼

import matplotlib.pyplot as plt
import numpy as np

def gen_reg_data():
 X = np.arange(0, 45, 0.1)
 X = X + np.random.random(size=X.shape[0]) * 20
 y = 2 * X + np.random.random(size=X.shape[0]) * 20 + 10
 return X, y

def test_linear_regression():
 clf = LinearRegression()
 X, y = gen_reg_data()
 clf.fit(X, y)
 plt.plot(X, y, '.')
 X_axis = np.arange(-5, 75, 0.1)
 plt.plot(X_axis, clf.predict(X_axis))
 plt.title("Linear Regression")
 plt.show()

def test_lasso():
 clf = Lasso()
 X, y = gen_reg_data()
 clf.fit(X, y)
 plt.plot(X, y, '.')
 X_axis = np.arange(-5, 75, 0.1)
 plt.plot(X_axis, clf.predict(X_axis))
 plt.title("Lasso")
 plt.show()

def test_ridge():
 clf = Ridge()
 X, y = gen_reg_data()
 clf.fit(X, y)
 plt.plot(X, y, '.')
 X_axis = np.arange(-5, 75, 0.1)
 plt.plot(X_axis, clf.predict(X_axis))
 plt.title("Ridge")
 plt.show()

測(cè)試效果

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

以上就是Python 實(shí)現(xiàn) 3 種回歸模型(Linear Regression,Lasso,Ridge)的示例的詳細(xì)內(nèi)容,更多關(guān)于Python 實(shí)現(xiàn) 回歸模型的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python locust工具使用詳解

    Python locust工具使用詳解

    這篇文章主要介紹了Python locust工具使用詳解,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下
    2021-03-03
  • 國(guó)產(chǎn)麒麟系統(tǒng)kylin部署python項(xiàng)目詳細(xì)步驟

    國(guó)產(chǎn)麒麟系統(tǒng)kylin部署python項(xiàng)目詳細(xì)步驟

    這篇文章主要給大家介紹了關(guān)于國(guó)產(chǎn)麒麟系統(tǒng)kylin部署python項(xiàng)目的相關(guān)資料,文中通過(guò)代碼示例介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-09-09
  • tensorflow2.0的函數(shù)簽名與圖結(jié)構(gòu)(推薦)

    tensorflow2.0的函數(shù)簽名與圖結(jié)構(gòu)(推薦)

    這篇文章主要介紹了tensorflow2.0的函數(shù)簽名與圖結(jié)構(gòu),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-04-04
  • 將python文件打包成EXE應(yīng)用程序的方法

    將python文件打包成EXE應(yīng)用程序的方法

    相信大家都想把自己完成的項(xiàng)目打包成EXE應(yīng)用文件,然后就可以放在桌面隨時(shí)都能運(yùn)行了,下面來(lái)分享利用pytinstaller這個(gè)第三方庫(kù)來(lái)打包程序,感興趣的朋友跟隨小編一起看看吧
    2019-05-05
  • python使用datetime模塊處理日期時(shí)間及常用功能詳解

    python使用datetime模塊處理日期時(shí)間及常用功能詳解

    datetime模塊是Python標(biāo)準(zhǔn)庫(kù)中用于處理日期和時(shí)間的模塊,在本節(jié)中,我們將介紹datetime模塊的一些常用功能,并通過(guò)實(shí)例代碼詳細(xì)講解每個(gè)知識(shí)點(diǎn),有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2023-06-06
  • Python基礎(chǔ)中的的if-else語(yǔ)句詳解

    Python基礎(chǔ)中的的if-else語(yǔ)句詳解

    這篇文章主要為大家詳細(xì)介紹了Python基礎(chǔ)中的的if-else語(yǔ)句,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-02-02
  • 深入解析python中的實(shí)例方法、類(lèi)方法和靜態(tài)方法

    深入解析python中的實(shí)例方法、類(lèi)方法和靜態(tài)方法

    這篇文章主要介紹了python中的實(shí)例方法、類(lèi)方法和靜態(tài)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • 解決tensorflow訓(xùn)練時(shí)內(nèi)存持續(xù)增加并占滿(mǎn)的問(wèn)題

    解決tensorflow訓(xùn)練時(shí)內(nèi)存持續(xù)增加并占滿(mǎn)的問(wèn)題

    今天小編就為大家分享一篇解決tensorflow訓(xùn)練時(shí)內(nèi)存持續(xù)增加并占滿(mǎn)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-01-01
  • Python畫(huà)圖練習(xí)案例分享

    Python畫(huà)圖練習(xí)案例分享

    這篇文章主要介紹了Python畫(huà)圖練習(xí)案例分享,文章基于Python實(shí)現(xiàn)各種畫(huà)圖,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下
    2022-07-07
  • 使用C語(yǔ)言來(lái)擴(kuò)展Python程序和Zope服務(wù)器的教程

    使用C語(yǔ)言來(lái)擴(kuò)展Python程序和Zope服務(wù)器的教程

    這篇文章主要介紹了使用C語(yǔ)言來(lái)擴(kuò)展Python程序和Zope服務(wù)器的教程,本文來(lái)自于IBM官方網(wǎng)站技術(shù)文檔,需要的朋友可以參考下
    2015-04-04

最新評(píng)論

法库县| 永宁县| 那曲县| 通城县| 海盐县| 双城市| 石泉县| 六枝特区| 岚皋县| 抚顺县| 依安县| 宜章县| 井冈山市| 乐平市| 萨嘎县| 肥东县| 芦溪县| 苍山县| 津南区| 定陶县| 特克斯县| 兰州市| 常熟市| 海安县| 茌平县| 古交市| 黑水县| 庆元县| 德清县| 泰安市| 汉沽区| 图们市| 博乐市| 灵川县| 苍溪县| 皮山县| 思南县| 民乐县| 永昌县| 崇礼县| 玉田县|