使用片麻岩的OLS玩具示例中的"Matrices are not aligned"错误



我正试图为组成数据构建一个简单的线性回归示例。我使用以下代码:

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)

相关内容

最新更新