我正在尝试为成分数据建立一个简单的线性回归的例子。我使用了以下代码:
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。有没有办法让它运行起来?
发布于 2020-08-11 15:46:34
看起来您在训练和测试阶段混淆了x和y矩阵。您的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)https://stackoverflow.com/questions/63353096
复制相似问题