MSE(mean_squared_error):

from sklearn import linear_model,metrics #指標
mse = metrics.mean_squared_error(ytest,ypredict)

判定係數(R-squared):

y^: 預測值
y bar: 平均值
計算判定係數(R-squared):

皮爾森積差相關係數
(Pearson correlation coefficient):

計算皮爾森積差相關係數:

多維度線性迴歸:

from sklearn import linear_model,metrics
import pandas as pd
import sys
fpath = r”C:\Python\P107\doc\BostonHousing.csv”
dataset = pd.read_csv(fpath) #.shape = (506, 14)
headerList = dataset.columns.tolist()
cols = dataset.columns.size
# dataset.columns.size = 14
# ==================================================
# x = dataset.drop(headerList[-1],axis=1).values #(506, 13)
# y = dataset[headerList[-1]].values #(506,)
# ==================================================
x=dataset.iloc[:,0:cols-1].values #(506, 13)
y=dataset.iloc[:,cols-1:].values.ravel() #(506,)
#沒有ravel()的話,(506,1) 多出一個維度,第二維長度僅有1
from sklearn.model_selection import train_test_split
xtrain,xtest,ytrain,ytest =\
train_test_split(x,y,test_size=0.3,
random_state=42,shuffle=True)
# 1. train_test_split()將原始資料切割為
# xtrain, xtest, ytrain, ytest
# =============================================
# print(“The shpae of training data(X axis):”,xtrain.shape)
# print(“The shpae of training data(Y axis):”,ytrain.shape)
# print(“The shpae of testing data(X axis):”,xtest.shape)
# print(“The shpae of testing data(Y axis):”,ytest.shape)

# ==============================================
regr_train = linear_model.LinearRegression()
#2. 建立LinearRegression操作子
regr_train.fit(xtrain,ytrain)
#3. 用. fit(xtrain, ytrain) 影響操作子
ypredict = regr_train.predict(xtest)
#4. 用.predict(xtest) 生出預測值ypredict
print(“The predition data(Y axis):”,ypredict)
print(“The shape of predition data(Y axis):”,ypredict.shape)
mse = metrics.mean_squared_error(ytest,ypredict)
r2score = metrics.r2_score(ytest,ypredict)
# 5. ytest是真實資料,ypredict是預測值,
#有這兩者就可以用來計算mse跟r2_score
print(“mean_squared_error metrics:”,mse)
print(“r-squared metrics:”,r2score)

輸出結果:

p value:

推薦hahow線上學習python: https://igrape.net/30afN



![Python: pandas.read_excel(r”路徑\檔名.副檔名”, header = None), 自動加上0,1…的欄標籤, DataFrame如何取某一直欄或橫列? 如何用 .iloc[bool_list] 取出判斷式為真的那一列? bool_list = list( df[0] == 0 ) ; bool_list = list(df[0].isin([0])) ; DataFrame如何顯示完整的資料? pandas.set_option ( “display.max_rows”, None) Python: pandas.read_excel(r”路徑\檔名.副檔名”, header = None), 自動加上0,1…的欄標籤, DataFrame如何取某一直欄或橫列? 如何用 .iloc[bool_list] 取出判斷式為真的那一列? bool_list = list( df[0] == 0 ) ; bool_list = list(df[0].isin([0])) ; DataFrame如何顯示完整的資料? pandas.set_option ( “display.max_rows”, None)](https://i2.wp.com/savingking.com.tw/wp-content/uploads/2022/11/20221128164005_44.png?quality=90&zoom=2&ssl=1&resize=350%2C233)

![Python: pandas.DataFrame()處理雙維度資料,dict跟2D list轉為DataFrame有何差別?如何用index及columns屬性客製化index跟欄位名稱?df.index = [“一”,”二”,”三”,”四”] ; df.columns = 使用.head(n) ; .tail(m) ;取首n列,尾m列; .at[index,欄位名稱] 取單一資料 ; .iat[index,欄位順序] 取單一資料 ; .loc[index,欄位名稱] 取資料 ; .iloc[index,欄位順序];df.iloc[ [0,1],[0,2]])取資料 ; df.iloc[ 0:3,0:2]切片 Python: pandas.DataFrame()處理雙維度資料,dict跟2D list轉為DataFrame有何差別?如何用index及columns屬性客製化index跟欄位名稱?df.index = [“一”,”二”,”三”,”四”] ; df.columns = 使用.head(n) ; .tail(m) ;取首n列,尾m列; .at[index,欄位名稱] 取單一資料 ; .iat[index,欄位順序] 取單一資料 ; .loc[index,欄位名稱] 取資料 ; .iloc[index,欄位順序];df.iloc[ [0,1],[0,2]])取資料 ; df.iloc[ 0:3,0:2]切片](https://i0.wp.com/savingking.com.tw/wp-content/uploads/2022/11/20221111093547_79.png?quality=90&zoom=2&ssl=1&resize=350%2C233)




近期留言