我最近開始機器學習,當我遇到回歸模型時,我發現要訓練模型,我們使用regressor.fit方法,它需要 2 個引數觀察和結果,但觀察陣列是 2d,結果陣列是 1D。誰能告訴我為什么我們不為 fit 方法使用相同的維度陣列?當我嘗試擬合相同維度的陣列時,它給了我一個錯誤,因為類預期的 2d 陣列但給出了 1d
dataset = pd.read_csv("Position_Salaries.csv")
x= dataset.iloc[:,1:-1].values
y = dataset.iloc[:,-1].values
print(y)
print(x)
from sklearn.tree import DecisionTreeRegressor
reg = DecisionTreeRegressor(random_state=0)
reg.fit(x,y)
Output -
[ 45000 50000 60000 80000 110000 150000 200000 300000 500000
1000000]
[[ 1]
[ 2]
[ 3]
[ 4]
[ 5]
[ 6]
[ 7]
[ 8]
[ 9]
[10]]
uj5u.com熱心網友回復:
要使資料符合 sklearn 的首選格式,請嘗試以下操作:
reg.fit(x.reshape(-1,1),y.reshape(-1,1))
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/436243.html
