天天看點

資料挖掘關于一進制線性回歸

一進制線性回歸即隻包括一個自變量和一個因變量,簡單來說這個模型就是可以近似用一條直線來表示。

我們随便取些數值來表示吧!

資料挖掘關于一進制線性回歸

以上數值都是随便輸的。以x為自變量,y為因變量來表示吧。

#首先導入需要的包,numpy和matplotlib
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline#在用jupyter notebook時沒有這個代碼可以會報錯。看運氣吧 我在學校機房操作時出現 figute size 640x480 with 1 axex的報錯,是以在這裡加了這個代碼
from sklearn import linear_model
#擷取資料
def getDate(filename):
    date=pd.read_excel(filename,encoding='utf-8')
    return date['x'],date['y']
x,y=getDate(r'C:\Users\Administrator\Desktop\01.xlsx')
plt.scatter(x,y)
plt.show()
def show_linear_line(X_parameters,Y_parameters):
    regr=linear_model.LinearRegression()#引入線性模型
    regr.fit(X_parameters,Y_parameters)#拟合資料
    plt.scatter(X_parameters,Y_parameters,color='blue',linewidth=5)
    plt.plot(X_parameters,regr.predict(X_parameters),color='red',linewidth=2)
    plt.xticks(())
    plt.yticks(())
    plt.show()
#調用show_linear_line
show_linear_line(x.values.reshape(-1, 1),y.values.reshape(-1, 1))

           

目前隻是了解一進制線性,多元線性還不熟悉。希望下次可以更新多元線性回歸的一些例子!

代碼運作後的結果是

資料挖掘關于一進制線性回歸

繼續閱讀