人工智能—Python實(shí)現(xiàn)線性回歸
1、概述
(1)人工智能學(xué)習(xí)

(2)機(jī)器學(xué)習(xí)

(3)有監(jiān)督學(xué)習(xí)

(4)線性回歸

2、線性回歸
(1)實(shí)現(xiàn)步驟
- 根據(jù)隨機(jī)初始化的 w x b 和 y 來(lái)計(jì)算 loss
- 根據(jù)當(dāng)前的 w x b 和 y 的值來(lái)計(jì)算梯度
- 更新梯度,循環(huán)將新的 w′ 和 b′ 復(fù)賦給 w 和 b ,最終得到一個(gè)最優(yōu)的 w′ 和 b′ 作為方程最終的
(2)數(shù)學(xué)表達(dá)式

3、代碼實(shí)現(xiàn)(Python)
(1)機(jī)器學(xué)習(xí)庫(kù)(sklearn.linear_model)

代碼:
from sklearn import linear_model
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt#用于作圖
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False
import numpy as np#用于創(chuàng)建向量
?
?
reg=linear_model.LinearRegression(fit_intercept=True,normalize=False)
x=[[32.50235],[53.4268],[61.53036],[47.47564],[59.81321],[55.14219],[52.14219],[39.29957],
?[48.10504],[52.55001],[45.41873],[54.35163],[44.16405],[58.16847],[56.72721]]
y=[31.70701,68.7776,62.56238,71.54663,87.23093,78.21152,79.64197,59.17149,75.33124,71.30088,55.16568,82.47885,62.00892
,75.39287,81.43619]
reg.fit(x,y)
k=reg.coef_#獲取斜率w1,w2,w3,...,wn
b=reg.intercept_#獲取截距w0
x0=np.arange(30,60,0.2)
y0=k*x0+b
print("k={0},b={1}".format(k,b))
plt.scatter(x,y)
plt.plot(x0,y0,label='LinearRegression')
plt.xlabel('X')
plt.ylabel('Y')
plt.legend()
plt.show()結(jié)果:
k=[1.36695374],b=0.13079331831460195

(2)Python詳細(xì)實(shí)現(xiàn)(方法1)

代碼:
#方法1
import numpy as np
import matplotlib.pyplot as plt
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False
#數(shù)據(jù)生成
data = []
for i in range(100):
? ? x = np.random.uniform(3., 12.)
? ? # mean=0, std=1
? ? eps = np.random.normal(0., 1)
? ? y = 1.677 * x + 0.039 + eps
? ? data.append([x, y])
?
data = np.array(data)
?
#統(tǒng)計(jì)誤差
# y = wx + b
def compute_error_for_line_given_points(b, w, points):
? ? totalError = 0
? ? for i in range(0, len(points)):
? ? ? ? x = points[i, 0]
? ? ? ? y = points[i, 1]
? ? ? ? # computer mean-squared-error
? ? ? ? totalError += (y - (w * x + b)) ** 2
? ? # average loss for each point
? ? return totalError / float(len(points))
?
?
#計(jì)算梯度
def step_gradient(b_current, w_current, points, learningRate):
? ? b_gradient = 0
? ? w_gradient = 0
? ? N = float(len(points))
? ? for i in range(0, len(points)):
? ? ? ? x = points[i, 0]
? ? ? ? y = points[i, 1]
? ? ? ? # grad_b = 2(wx+b-y)
? ? ? ? b_gradient += (2/N) * ((w_current * x + b_current) - y)
? ? ? ? # grad_w = 2(wx+b-y)*x
? ? ? ? w_gradient += (2/N) * x * ((w_current * x + b_current) - y)
? ? # update w'
? ? new_b = b_current - (learningRate * b_gradient)
? ? new_w = w_current - (learningRate * w_gradient)
? ? return [new_b, new_w]
?
#迭代更新
def gradient_descent_runner(points, starting_b, starting_w, learning_rate, num_iterations):
? ? b = starting_b
? ? w = starting_w
? ? # update for several times
? ? for i in range(num_iterations):
? ? ? ? b, w = step_gradient(b, w, np.array(points), learning_rate)
? ? return [b, w]
?
?
def main():
?
? ? learning_rate = 0.0001
? ? initial_b = 0 ?# initial y-intercept guess
? ? initial_w = 0 ?# initial slope guess
? ? num_iterations = 1000
? ? print("迭代前 b = {0}, w = {1}, error = {2}"
? ? ? ? ? .format(initial_b, initial_w,
? ? ? ? ? ? ? ? ? compute_error_for_line_given_points(initial_b, initial_w, data))
? ? ? ? ? )
? ? print("Running...")
? ? [b, w] = gradient_descent_runner(data, initial_b, initial_w, learning_rate, num_iterations)
? ? print("第 {0} 次迭代結(jié)果 b = {1}, w = {2}, error = {3}".
? ? ? ? ? format(num_iterations, b, w,
? ? ? ? ? ? ? ? ?compute_error_for_line_given_points(b, w, data))
? ? ? ? ? )
? ? plt.plot(data[:,0],data[:,1], color='b', marker='+', linestyle='--',label='true')
? ? plt.plot(data[:,0],w*data[:,0]+b,color='r',label='predict')
? ? plt.xlabel('X')
? ? plt.ylabel('Y')
? ? plt.legend()
? ? plt.show()
?
?
if __name__ == '__main__':
? ? main()
?
?結(jié)果:

迭代前 :b = 0, w = 0, error = 186.61000821356697
Running...
第 1000 次迭代結(jié)果:b = 0.20558501549252192, w = 1.6589067569038516, error = 0.9963685680112963
(3)Python詳細(xì)實(shí)現(xiàn)(方法2)
代碼:
#方法2
?
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams["font.sans-serif"]=["SimHei"]
mpl.rcParams["axes.unicode_minus"]=False
?
?
# y = wx + b
#Import data
file=pd.read_csv("data.csv")
?
def compute_error_for_line_given(b, w):
? ? totalError = np.sum((file['y']-(w*file['x']+b))**2)
? ? return np.mean(totalError)
?
def step_gradient(b_current, w_current, ?learningRate):
? ? b_gradient = 0
? ? w_gradient = 0
? ? N = float(len(file['x']))
? ? for i in range (0,len(file['x'])):
? ? ? ? # grad_b = 2(wx+b-y)
? ? ? ? b_gradient += (2 / N) * ((w_current * file['x'] + b_current) - file['y'])
? ? ? ? # grad_w = 2(wx+b-y)*x
? ? ? ? w_gradient += (2 / N) * file['x'] * ((w_current * file['x'] + b_current) - file['x'])
? ? # update w'
? ? new_b = b_current - (learningRate * b_gradient)
? ? new_w = w_current - (learningRate * w_gradient)
? ? return [new_b, new_w]
?
?
def gradient_descent_runner( starting_b, starting_w, learning_rate, num_iterations):
? ? b = starting_b
? ? w = starting_w
? ? # update for several times
? ? for i in range(num_iterations):
? ? ? ? b, w = step_gradient(b, w, ?learning_rate)
? ? return [b, w]
?
?
def main():
? ? learning_rate = 0.0001
? ? initial_b = 0 ?# initial y-intercept guess
? ? initial_w = 0 ?# initial slope guess
? ? num_iterations = 100
? ? print("Starting gradient descent at b = {0}, w = {1}, error = {2}"
? ? ? ? ? .format(initial_b, initial_w,
? ? ? ? ? ? ? ? ? compute_error_for_line_given(initial_b, initial_w))
? ? ? ? ? )
? ? print("Running...")
? ? [b, w] = gradient_descent_runner(initial_b, initial_w, learning_rate, num_iterations)
? ? print("After {0} iterations b = {1}, w = {2}, error = {3}".
? ? ? ? ? format(num_iterations, b, w,
? ? ? ? ? ? ? ? ?compute_error_for_line_given(b, w))
? ? ? ? ? )
? ? plt.plot(file['x'],file['y'],'ro',label='線性回歸')
? ? plt.xlabel('X')
? ? plt.ylabel('Y')
? ? plt.legend()
? ? plt.show()
?
?
?
?
if __name__ == '__main__':
? ? main()結(jié)果:

Starting gradient descent at b = 0, w = 0, error = 75104.71822821398 Running... After 100 iterations b = 0 ? ? 0.014845 1 ? ? 0.325621 2 ? ? 0.036883 3 ? ? 0.502265 4 ? ? 0.564917 5 ? ? 0.479366 6 ? ? 0.568968 7 ? ? 0.422619 8 ? ? 0.565073 9 ? ? 0.393907 10 ? ?0.216854 11 ? ?0.580750 12 ? ?0.379350 13 ? ?0.361574 14 ? ?0.511651 dtype: float64, w = 0 ? ? 0.999520 1 ? ? 0.994006 2 ? ? 0.999405 3 ? ? 0.989645 4 ? ? 0.990683 5 ? ? 0.991444 6 ? ? 0.989282 7 ? ? 0.989573 8 ? ? 0.988498 9 ? ? 0.992633 10 ? ?0.995329 11 ? ?0.989490 12 ? ?0.991617 13 ? ?0.993872 14 ? ?0.991116 dtype: float64, error = 6451.5510231710905
數(shù)據(jù):

(4)Python詳細(xì)實(shí)現(xiàn)(方法3)
#方法3
?
import numpy as np
?
points = np.genfromtxt("data.csv", delimiter=",")
#從數(shù)據(jù)讀入到返回需要兩個(gè)迭代循環(huán),第一個(gè)迭代將文件中每一行轉(zhuǎn)化為一個(gè)字符串序列,
#第二個(gè)循環(huán)迭代對(duì)每個(gè)字符串序列指定合適的數(shù)據(jù)類型:
# y = wx + b
def compute_error_for_line_given_points(b, w, points):
? ? totalError = 0
? ? for i in range(0, len(points)):
? ? ? ? x = points[i, 0]
? ? ? ? y = points[i, 1]
? ? ? ? # computer mean-squared-error
? ? ? ? totalError += (y - (w * x + b)) ** 2
? ? # average loss for each point
? ? return totalError / float(len(points))
?
?
def step_gradient(b_current, w_current, points, learningRate):
? ? b_gradient = 0
? ? w_gradient = 0
? ? N = float(len(points))
? ? for i in range(0, len(points)):
? ? ? ? x = points[i, 0]
? ? ? ? y = points[i, 1]
? ? ? ? # grad_b = 2(wx+b-y)
? ? ? ? b_gradient += (2 / N) * ((w_current * x + b_current) - y)
? ? ? ? # grad_w = 2(wx+b-y)*x
? ? ? ? w_gradient += (2 / N) * x * ((w_current * x + b_current) - y)
? ? # update w'
? ? new_b = b_current - (learningRate * b_gradient)
? ? new_w = w_current - (learningRate * w_gradient)
? ? return [new_b, new_w]
?
?
def gradient_descent_runner(points, starting_b, starting_w, learning_rate, num_iterations):
? ? b = starting_b
? ? w = starting_w
? ? # update for several times
? ? for i in range(num_iterations):
? ? ? ? b, w = step_gradient(b, w, np.array(points), learning_rate)
? ? return [b, w]
?
?
def main():
? ? learning_rate = 0.0001
? ? initial_b = 0 ?# initial y-intercept guess
? ? initial_w = 0 ?# initial slope guess
? ? num_iterations = 1000
? ? print("Starting gradient descent at b = {0}, w = {1}, error = {2}"
? ? ? ? ? .format(initial_b, initial_w,
? ? ? ? ? ? ? ? ? compute_error_for_line_given_points(initial_b, initial_w, points))
? ? ? ? ? )
? ? print("Running...")
? ? [b, w] = gradient_descent_runner(points, initial_b, initial_w, learning_rate, num_iterations)
? ? print("After {0} iterations b = {1}, w = {2}, error = {3}".
? ? ? ? ? format(num_iterations, b, w,
? ? ? ? ? ? ? ? ?compute_error_for_line_given_points(b, w, points))
? ? ? ? ? )
?
?
if __name__ == '__main__':
? ? main()4、案例——房屋與價(jià)格、尺寸

(1)代碼
#1.導(dǎo)入包
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn import linear_model
?
#2.加載訓(xùn)練數(shù)據(jù),建立回歸方程
# 取數(shù)據(jù)集(1)
datasets_X = [] ? ? #存放房屋面積
datasets_Y = [] ? ? #存放交易價(jià)格
fr = open('房?jī)r(jià)與房屋尺寸.csv','r') ? ?#讀取文件,r: 以只讀方式打開(kāi)文件,w: 打開(kāi)一個(gè)文件只用于寫(xiě)入。
lines = fr.readlines() ? ? ? ? ? ? ?#一次讀取整個(gè)文件。
for line in lines: ? ? ? ? ? ? ? ? ?#逐行進(jìn)行操作,循環(huán)遍歷所有數(shù)據(jù)
? ? items = line.strip().split(',') ? ?#去除數(shù)據(jù)文件中的逗號(hào),strip()用于移除字符串頭尾指定的字符(默認(rèn)為空格或換行符)或字符序列。
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#split(‘ '): 通過(guò)指定分隔符對(duì)字符串進(jìn)行切片,如果參數(shù) num 有指定值,則分隔 num+1 個(gè)子字符串。
? ? datasets_X.append(int(items[0])) ? #將讀取的數(shù)據(jù)轉(zhuǎn)換為int型,并分別寫(xiě)入
? ? datasets_Y.append(int(items[1]))
?
length = len(datasets_X) ? ? ? ? ? ? ?#求得datasets_X的長(zhǎng)度,即為數(shù)據(jù)的總數(shù)
datasets_X = np.array(datasets_X).reshape([length,1]) ? #將datasets_X轉(zhuǎn)化為數(shù)組,并變?yōu)?維,以符合線性回歸擬合函數(shù)輸入?yún)?shù)要求
datasets_Y = np.array(datasets_Y) ? ? ? ? ? ? ? ? ? ?#將datasets_Y轉(zhuǎn)化為數(shù)組
?
#取數(shù)據(jù)集(2)
'''fr = pd.read_csv('房?jī)r(jià)與房屋尺寸.csv',encoding='utf-8')
datasets_X=fr['房屋面積']
datasets_Y=fr['交易價(jià)格']'''
?
minX = min(datasets_X)
maxX = max(datasets_X)
X = np.arange(minX,maxX).reshape([-1,1]) ? ? ? ?#以數(shù)據(jù)datasets_X的最大值和最小值為范圍,建立等差數(shù)列,方便后續(xù)畫(huà)圖。
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #reshape([-1,1]),轉(zhuǎn)換成1列,reshape([2,-1]):轉(zhuǎn)換成兩行
linear = linear_model.LinearRegression() ? ? ?#調(diào)用線性回歸模塊,建立回歸方程,擬合數(shù)據(jù)
linear.fit(datasets_X, datasets_Y)
?
#3.斜率及截距
print('Coefficients:', linear.coef_) ? ? ?#查看回歸方程系數(shù)(k)
print('intercept:', linear.intercept_) ? ?##查看回歸方程截距(b)
print('y={0}x+{1}'.format(linear.coef_,linear.intercept_)) #擬合線
?
# 4.圖像中顯示
plt.scatter(datasets_X, datasets_Y, color = 'red')
plt.plot(X, linear.predict(X), color = 'blue')
plt.xlabel('Area')
plt.ylabel('Price')
plt.show()(2)結(jié)果
Coefficients: [0.14198749] intercept: 53.43633899175563 y=[0.14198749]x+53.43633899175563

(3)數(shù)據(jù)
第一列是房屋面積,第二列是交易價(jià)格



到此這篇關(guān)于人工智能—Python實(shí)現(xiàn)線性回歸的文章就介紹到這了,更多相關(guān) Python實(shí)現(xiàn)線性回歸內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Python實(shí)現(xiàn)圖像顏色量化的方法
這篇文章主要介紹了使用Python進(jìn)行圖像顏色量化,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
Python調(diào)用OpenCV實(shí)現(xiàn)圖像平滑代碼實(shí)例
這篇文章主要介紹了Python調(diào)用OpenCV實(shí)現(xiàn)圖像平滑代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
python OpenCV學(xué)習(xí)筆記直方圖反向投影的實(shí)現(xiàn)
這篇文章主要介紹了python OpenCV學(xué)習(xí)筆記直方圖反向投影的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
Python?matplotlib?seaborn繪圖教程詳解
Seaborn是在matplotlib的基礎(chǔ)上進(jìn)行了更高級(jí)的API封裝,從而使得作圖更加容易,在大多數(shù)情況下使用seaborn就能做出很具有吸引力的圖。本文將詳細(xì)講解如何利用Seaborn繪制圖表,需要的可以參考一下2022-03-03
django開(kāi)發(fā)post接口簡(jiǎn)單案例,獲取參數(shù)值的方法
今天小編就為大家分享一篇django開(kāi)發(fā)post接口簡(jiǎn)單案例,獲取參數(shù)值的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12

