簡體   English   中英

使用片麻岩的 ols 玩具示例中的“矩陣未對齊”錯誤

[英]“Matrices are not aligned” error in toy example of ols using gneiss

我正在嘗試為成分數據構建一個線性回歸的簡單示例。 我正在使用以下代碼:

from pandas import DataFrame
import numpy as np
from skbio import TreeNode
from gneiss.regression import ols
from IPython.display import display

#define table of compositions
yTrain = DataFrame({'y1': [0.8, 0.3, 0.5], 'y2': [0.2, 0.7, 0.5]})

#define predictors for compositions
xTrain = DataFrame({'x1': [1,3,2]})

#Once these variables are defined, a regression can be performed. These proportions will be converted to balances according to the tree specified. And the regression formula is specified to run temp and ph against the proportions in a single model.
model = ols('x1', yTrain, xTrain)
model.fit()
xTest = DataFrame({'x1': [1,3]})
yTest = model.predict(xTest)
display(yTest)

我得到錯誤matrices are not aligned 關於如何讓它運行的任何想法?

看起來您在訓練和測試階段之間混淆了xy矩陣。 您的xTest的結構可能與yTrain相同。 在您的代碼中, xTest看起來像xTrain ,它似乎與標簽相對應。

ML 中的一般約定是使用x作為輸入,使用y作為輸出。 在您的情況下,您在訓練期間將y用於輸入,將x用於標簽,而在測試期間則相反。

例如,嘗試將 xTest 設置為以下內容:

xTest = DataFrame({'y1': [0.1, 0.4, 0.6], 'y2': [0.4, 0.2, 0.8]})

那應該擺脫錯誤。 理想情況下,您會按照以下方式做一些事情:

from pandas import DataFrame
import numpy as np
from skbio import TreeNode
from gneiss.regression import ols
from IPython.display import display

#define table of compositions
xTrain = DataFrame({'x1': [0.8, 0.3, 0.5], 'x2': [0.2, 0.7, 0.5]})

#define predictors for compositions
yTrain = DataFrame({'y1': [1,3,2]})

model = ols('y1', xTrain, yTrain)
model.fit()
xTest = DataFrame({'x1': [0.1, 0.4, 0.6], 'x2': [0.4, 0.2, 0.8]})
yTest = model.predict(xTest)
display(yTest)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM