欧美成人午夜免费全部完,亚洲午夜福利精品久久,а√最新版在线天堂,另类亚洲综合区图片小说区,亚洲欧美日韩精品色xxx

扣丁學(xué)堂Python培訓(xùn)簡(jiǎn)述Python實(shí)現(xiàn)的線性回歸算法示例

2019-07-30 16:32:53 5917瀏覽

本篇文章扣丁學(xué)堂Python培訓(xùn)小編給小伙伴們實(shí)例分享一下Python實(shí)現(xiàn)的線性回歸算法,涉及Python使用最小二乘法、梯度下降算法實(shí)現(xiàn)線性回歸相關(guān)算法操作與使用技巧,需要的朋友就隨小編一起來(lái)學(xué)習(xí)了解一下吧。

扣丁學(xué)堂Python培訓(xùn)簡(jiǎn)述Python實(shí)現(xiàn)的線性回歸算法示例

代碼:


#encoding:utf-8
"""
  Project: Using Python to Implement LineRegression Algorithm
"""
import numpy as np
import pandas as pd
from numpy.linalg import inv
from numpy import dot
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from sklearn import linear_model
# 最小二乘法
def lms(x_train,y_train,x_test):
  theta_n = dot(dot(inv(dot(x_train.T, x_train)), x_train.T), y_train) # theta = (X'X)^(-1)X'Y
  #print(theta_n)
  y_pre = dot(x_test,theta_n)
  mse = np.average((y_test-y_pre)**2)
  #print(len(y_pre))
  #print(mse)
  return theta_n,y_pre,mse
#梯度下降算法
def train(x_train, y_train, num, alpha,m, n):
  beta = np.ones(n)
  for i in range(num):
    h = np.dot(x_train, beta)       # 計(jì)算預(yù)測(cè)值
    error = h - y_train.T         # 計(jì)算預(yù)測(cè)值與訓(xùn)練集的差值
    delt = 2*alpha * np.dot(error, x_train)/m # 計(jì)算參數(shù)的梯度變化值
    beta = beta - delt
    #print('error', error)
  return beta
if __name__ == "__main__":
  iris = pd.read_csv('iris.csv')
  iris['Bias'] = float(1)
  x = iris[['Sepal.Width', 'Petal.Length', 'Petal.Width', 'Bias']]
  y = iris['Sepal.Length']
  x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=5)
  t = np.arange(len(x_test))
  m, n = np.shape(x_train)
  # Leastsquare
  theta_n, y_pre, mse = lms(x_train, y_train, x_test)
  # plt.plot(t, y_test, label='Test')
  # plt.plot(t, y_pre, label='Predict')
  # plt.show()
  # GradientDescent
  beta = train(x_train, y_train, 1000, 0.001, m, n)
  y_predict = np.dot(x_test, beta.T)
  # plt.plot(t, y_predict)
  # plt.plot(t, y_test)
  # plt.show()
  # sklearn
  regr = linear_model.LinearRegression()
  regr.fit(x_train, y_train)
  y_p = regr.predict(x_test)
  print(regr.coef_,theta_n,beta)
  l1,=plt.plot(t, y_predict)
  l2,=plt.plot(t, y_p)
  l3,=plt.plot(t, y_pre)
  l4,=plt.plot(t, y_test)
  plt.legend(handles=[l1, l2,l3,l4 ], labels=['GradientDescent', 'sklearn','Leastsquare','True'], loc='best')
  plt.show()

想要了解更多關(guān)于Python和人工智能方面內(nèi)容的小伙伴,請(qǐng)關(guān)注扣丁學(xué)堂Python培訓(xùn)官網(wǎng)、微信等平臺(tái),扣丁學(xué)堂IT職業(yè)在線學(xué)習(xí)教育平臺(tái)為您提供權(quán)威的Python開(kāi)發(fā)環(huán)境搭建視頻,Python培訓(xùn)后的前景無(wú)限,行業(yè)薪資和未來(lái)的發(fā)展會(huì)越來(lái)越好的,扣丁學(xué)堂老師精心推出的Python視頻教程定能讓你快速掌握Python從入門(mén)到精通開(kāi)發(fā)實(shí)戰(zhàn)技能。扣丁學(xué)堂Python技術(shù)交流群:279521237。


扣丁學(xué)堂微信公眾號(hào)                          Python全棧開(kāi)發(fā)爬蟲(chóng)人工智能機(jī)器學(xué)習(xí)數(shù)據(jù)分析免費(fèi)公開(kāi)課直播間


      【關(guān)注微信公眾號(hào)獲取更多學(xué)習(xí)資料】         【掃碼進(jìn)入Python全棧開(kāi)發(fā)免費(fèi)公開(kāi)課】



查看更多關(guān)于"Python開(kāi)發(fā)資訊"的相關(guān)文章>


標(biāo)簽: Python培訓(xùn) Python視頻教程 Python在線視頻 Python學(xué)習(xí)視頻 Python培訓(xùn)班

熱門(mén)專(zhuān)區(qū)

暫無(wú)熱門(mén)資訊

課程推薦

微信
微博
15311698296

全國(guó)免費(fèi)咨詢(xún)熱線

郵箱:codingke@1000phone.com

官方群:148715490

北京千鋒互聯(lián)科技有限公司版權(quán)所有   北京市海淀區(qū)寶盛北里西區(qū)28號(hào)中關(guān)村智誠(chéng)科創(chuàng)大廈4層
京ICP備2021002079號(hào)-2   Copyright ? 2017 - 2022
返回頂部 返回頂部