【问题标题】:"Matrices are not aligned" error in toy example of ols using gneiss使用片麻岩的 ols 玩具示例中的“矩阵未对齐”错误
【发布时间】:2020-11-30 19:39:57
【问题描述】:

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

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。关于如何让它运行的任何想法?

【问题讨论】:

    标签: python linear-regression statsmodels coda


    【解决方案1】:

    您似乎在训练和测试阶段混淆了 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)
    

    【讨论】:

    • 感谢您查看此内容,但您发布的代码会导致相同的错误。
    • 我在gneiss 库中找到了错误弹出的位置,但如果不知道您正在解决的问题的上下文,这对我来说没有意义。您能否简要回顾一下您正在尝试使用上述数据和模型做什么?看来我假设你正在寻找预测 yTest 的值给定输入特征的值 xTest 可能不正确。
    猜你喜欢
    • 1970-01-01
    • 2017-01-12
    • 2021-05-15
    • 2012-02-03
    • 2019-05-11
    • 2012-11-01
    • 1970-01-01
    • 2023-03-16
    相关资源
    最近更新 更多