【问题标题】:How can I print the ndarray compelete information?如何打印阵列完整信息?
【发布时间】:2023-03-23 16:23:02
【问题描述】:

我一直在尝试匹配输出,但没有得到我从 df 获得的列名,这些列名是我放入 statmodels 的。

import pandas
import statsmodels.api as statmodel
df = pandas.read_csv('fastfood.csv')

df = df[['total_fat', 'sat_fat', 'cholesterol', 'sodium','calories']]
X = df[['total_fat', 'sat_fat', 'cholesterol', 'sodium']].values
Y = df[['calories']].values
X = statmodel.add_constant(X)
model = statmodel.OLS(Y, X).fit()

print(model.mse_total.round(2))
print(model.rsquared.round(2))
print(model.params.round(2))
print(model.pvalues.round(2))

我得到的输出:

79770.18
0.9
[71.73  9.1   0.6   0.21  0.16]
[0.   0.   0.64 0.07 0.  ]

我需要的输出:

79770.18
0.9 
-{0,}71.73 
total_fat 9.10 
sat_fat . ..0.60 
cholesterol 0.21 
sodium... ...0.16 
dtype: float64 
{0,}0.00 
total_fat 0.00 
sat_fat. ..0.64 
cholesterol...0.07 
sodium .. ..0.00 
dtype: float64

【问题讨论】:

  • 试试model.params.round(2).dtype.names 输出是什么?
  • @kinshukdua 对于model.params.round(2).dtype.names,我得到
  • 如果从 X 和 Y 的定义中删除“.values”会发生什么?
  • @Simone 感谢它现在工作。你是最棒的
  • @AsadKareem 它对我有用,请参阅我的答案。我不知道为什么它不适合你,也许你使用的是旧版本的 statsmodels(我的是 0.12.2)。

标签: python pandas statmodels


【解决方案1】:

我试图删除 X 和 Y 定义中的.values

import pandas
import statsmodels.api as statmodel
df = pandas.DataFrame({'total_fat': np.random.rand(100), 
                       'sat_fat': np.random.rand(100), 
                       'cholesterol': np.random.rand(100), 
                       'sodium': np.random.rand(100),
                       'calories': np.random.rand(100)})

df = df[['total_fat', 'sat_fat', 'cholesterol', 'sodium','calories']]
X = df[['total_fat', 'sat_fat', 'cholesterol', 'sodium']]
Y = df[['calories']]
X = statmodel.add_constant(X)
model = statmodel.OLS(Y, X).fit()

print(model.mse_total.round(2))
print(model.rsquared.round(2))
print(model.params.round(2))
print(model.pvalues.round(2))

它给了我以下输出:

0.09
0.02
const          0.49
total_fat     -0.07
sat_fat        0.14
cholesterol    0.02
sodium        -0.01
dtype: float64
const          0.00
total_fat      0.54
sat_fat        0.21
cholesterol    0.83
sodium         0.89
dtype: float64

【讨论】:

  • 谢谢@Simone。我还想问一件事,我怎样才能得到{0,} 0.49 而不是const 0.49
  • 试试print(model.params.rename(index={'const': '{0,}'}).round(2))
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-06-21
  • 2018-04-26
  • 2020-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多