【问题标题】:My testcases are not passing. I don't know what the issue is. Since i Cannot see the testcase i'm having difficulties我的测试用例没有通过。我不知道问题是什么。由于我看不到测试用例,我遇到了困难
【发布时间】:2021-05-26 06:57:07
【问题描述】:

我被要求编写一个线性回归程序,步骤如下。

将 R 数据集 mtcars 加载为 pandas 数据框。 通过考虑自变量 wt 的对数和因变量 mpg 的对数建立另一个线性回归模型。 用数据拟合模型,并显示 R 平方值

我尝试了以下 2 个模型,但测试未通过。我的代码有问题吗?

#case 1

import statsmodels.api as sm
import numpy as np
mtcars = sm.datasets.get_rdataset('mtcars')
mtcars_data = mtcars.data
liner_model = sm.formula.ols('np.log(wt) ~ np.log(mpg)',mtcars_data)
liner_result = liner_model.fit()
print(liner_result.rsquared)

#case 2

import statsmodels.api as sa
import statsmodels.formula.api as sfa
import numpy as np
import pandas as pd


mtcars = sa.datasets.get_rdataset('mtcars')
cars_data = mtcars.data

lin_mod2 = pd.DataFrame(cars_data)
lin_mod2['wt'] = np.log(lin_mod2['wt'])
lin_mod2['mpg'] = np.log(lin_mod2['mpg'])

lin_mod1 =  sfa.ols("wt~mpg",lin_mod2)
print(lin_mod1.fit().rsquared)

#或

import statsmodels.api as sm
import numpy as np
import pandas as pd
import statsmodels.formula.api as smf


mtcars = sm.datasets.get_rdataset('mtcars','datasets',cache=True).data
df = pd.DataFrame(mtcars)
model = smf.ols(formula='np.log(wt) ~ np.log(mpg)', data=df).fit()
r = model.rsquared
print(r)

【问题讨论】:

    标签: python statistics


    【解决方案1】:

    好吧,它通过了。这个问题是错误的,我认为我所要做的就是打印。

    model.summary()
    

    不是

    model.rsquared
    

    【讨论】:

      猜你喜欢
      • 2021-01-19
      • 2022-01-16
      • 1970-01-01
      • 2020-10-24
      • 1970-01-01
      • 2020-06-01
      • 1970-01-01
      • 2020-02-10
      • 1970-01-01
      相关资源
      最近更新 更多