參考:《機器學習實戰》- machine learning in action
一般而言,這幾個包是比較常見的:
• matplotlib,用於繪圖
• numpy,陣列處理庫
• pandas,強大的資料分析庫
• sklearn,用於線性回歸的庫
• scipy, 提供很多有用的科學函式
我一般是用pip安裝,若不熟悉這些庫,可以搜尋一下它們的簡單教程。
典型的例子是房價**,假設我們有以下資料集:
我們需要通過訓練這些資料得到乙個線性模型,以便來**大小為700平方英呎的房價是多少。
詳細**如下:
import結果如圖:matplotlib.pyplot as plt
import
numpy as np
import
pandas as pd
from sklearn import
datasets, linear_model
defget_data(file_name):
data =pd.read_csv(file_name)
x_parameter =
y_parameter =
for single_square_feet ,single_price_value in zip(data['
square_feet
'],data['
price
']):
return
x_parameter,y_parameter
deflinear_model_main(x_parameters,y_parameters,predict_value):
regr =linear_model.linearregression()
regr.fit(x_parameters, y_parameters)
predict_outcome =regr.predict(predict_value)
predictions ={}
predictions[
'intercept
'] =regr.intercept_
predictions[
'coefficient
'] =regr.coef_
predictions[
'predicted_value
'] =predict_outcome
return
predictions
defshow_linear_line(x_parameters,y_parameters):
regr =linear_model.linearregression()
regr.fit(x_parameters, y_parameters)
plt.scatter(x_parameters,y_parameters,color='
blue')
plt.plot(x_parameters,regr.predict(x_parameters),color='
red',linewidth=4)
#plt.xticks(())
#plt.yticks(())
plt.show()
if__name__ == "
__main__":
x,y = get_data('
e:/machine_learning/lr/input_data.csv')
#show_linear_line(x,y)
predictvalue = 700result =linear_model_main(x,y,predictvalue)
"intercept value
" , result['
intercept']
"coefficient
" , result['
coefficient']
"predicted value:
",result['
predicted_value
']
前兩個為公式裡的引數。
簡單的線性模型誤差難免高,於是引入多項式回歸模型,方程式如下:
這次我們用scipy.stats中的norm來生成滿足高斯分布的資料,直接貼**:
#結果如下:encoding:utf-8
import
matplotlib.pyplot as plt
import
numpy as np
from scipy.stats import
norm
from sklearn.pipeline import
pipeline
from sklearn.linear_model import
linearregression, sgdclassifier
from sklearn.preprocessing import
polynomialfeatures, standardscaler
x = np.arange(0, 1, 0.002)
y = norm.rvs(0, size=500, scale=0.1) #
高斯分布資料
y = y + x**2plt.scatter(x, y, s=5)
y_test =
y_test =np.array(y_test)
#clf = linearregression(fit_intercept=false)
clf = pipeline([('
poly
', polynomialfeatures(degree=100)),
('linear
', linearregression(fit_intercept=false))])
clf.fit(x[:, np.newaxis], y)
y_test =clf.predict(x[:, np.newaxis])
plt.plot(x, y_test, linewidth=2)
plt.grid()
#顯示網格
plt.show()
這裡取的最高次為100
參考部落格:
python實現線性回歸
定義 線性回歸在假設特徵滿足線性關係,根據給定的訓練資料訓練乙個模型,並用此模型進行 文中只介紹了簡單的概念,不涉及公式的證明等。從最簡單的一元線性關係介紹,假設有一組資料型態為 y theta x,其中 x y 我們根據 x,y 模擬出近似的 theta 引數值,進而得到 y theta x 模型...
python實現線性回歸
線性回歸模型是最簡單的機器學習模型,基礎可以從線性回歸模型開始入手,慢慢地過渡到非線性回歸以及神經網路模型。1.概念 2.線性回歸 簡單回歸 乙個自變數輸入,y x是一對一的關係,對映到幾何上來說就是二維座標系的直線方程,可表示為y 多元回歸 多個自變數,改變維度的大小。即 3.最小二乘法 通過向量...
python實現線性回歸
線性回歸模型是機器學習中最基礎的演算法,同時也在工業上得到很大應用。編碼實現該方法,可以對其有個透徹理解。回歸模型 目標函式 對目標函式求偏導 更新引數 樣本矩陣表示 python 實現 import numpy as np class linear object def init self sel...