Python-迴歸分析 | Python 回歸 結果

線性迴歸也被稱為最小二乘法回歸(Linear Regression, also called Ordinary Least-Squares (OLS) ... sklearn,statsmodels計算之結果是相同的.2017-06-18Python相關係數,線性迴歸,statsmodels,sklearn,itertools相關係數rets.corr()CtripLionCtrip1.0000000.059657Lion0.0596571.000000簡單線性迴歸(LinearRegression)123456789101112fromsklearn.linear_modelimportLinearRegressionX1=X.iloc[0:len(X)].to_frame()#ConvertSeriestoDataFrameY1=Y.iloc[0:len(Y)].to_frame()regr=LinearRegression()regr.fit(X1,Y1)regr.coef_#係數regr.intercept_#截距regr.predict(X1)#依線性迴歸公式計算Y值%pylabinline#繪製散佈圖plt.scatter(X1,Y1,color="black")plt.xlabel('戶數',fontproperties='SimHei')plt.ylabel('人口數',fontproperties='SimHei')$$Y=\beta_0+\beta_1X$$錯誤訊息:執行regr.fit時,產生異常訊息Foundinputvariableswithinconsistentnumbersofsamples:[1,368]處理方式:錯誤原因是sklearn要求使用DataFrame,但原資料為Series,所以將資料重新reshape即可使用X.iloc[0:len(X)].to_frame()series是一維的數據類型,dataframe是一個二維的、表格型的數據結構。

多變量線性迴歸`線性迴歸也被稱為最小二乘法回歸(LinearRegression,alsocalledOrdinaryLeast-Squares(OLS)Regression)sklearn12345678910house=pd.read_csv('python_for_data_science-master/Data/house-prices.csv')house1=pd.concat([house,pd.get_dummies(house['Brick']),pd.get_dummies(house['Neighborhood'])],axis=1)delhouse1['Home'],house1['No'],house1['Brick'],house1['Neighborhood']X=house1[['SqFt','Bedrooms','Bathrooms','Offers','Yes','East','North','West']]Y=house1['Price'].valuesfromsklearn.linear_modelimportLinearRegressionregr=LinearRegression()regr.fit(X,Y)house1['Price_Predict']=regr.predict(X).astype(int)house1.head()PriceSqFtBedroomsBathroomsOffersYesEastNorthWestPrice_Predict01143001790222010010318211142002030423010011612721148001740321010011304739470019803230100109230411980021303330100125063statsmodels參數估計:矩估計(MethodofMoment、MOM)最小平方法(OrdinaryLeastSquareEstimation,OLSE)最大似然估計(MaximumLikelihoodEstimation,MLE)12345importstatsmodels.apiassmX2=sm.add_constant(X)#為模型增加常數項,即迴歸線在y軸上的截距est=sm.OLS(Y,X2)est2=est.fit()print(est2.summary())sklearn,statsmodels計算之結果是相同的regr.coef_array([52.99374081,4246.79389165,7883.27849293,-8267.48831831,17297.34952752


常見投資理財問答


延伸文章資訊