0

我是 Python 编程的绝对新手,目前正在学习它的基本统计数据。

我面临着一个

“PatsyError:错误评估因素:NameError:”

在代码上pred = model.predict(pd.DataFrame(calo['wt'])

下面是我的代码:

# For reading data set
# importing necessary libraries
import pandas as pd 
import numpy as np
import matplotlib.pyplot as plt

# reading a csv file using pandas library
calo=pd.read_csv("/Users/Sanjeev/Desktop/Excel R Assignments/Simple Linear Regression/calories_consumed.csv")
calo.columns = ['wt','cal']

np.corrcoef(calo.wt,calo.cal)

plt.plot(calo.wt,calo.cal,"bo");plt.xlabel("WEIGHT");plt.ylabel("CALORIES")

# For preparing linear regression model we need to import the statsmodels.formula.api
import statsmodels.formula.api as smf
model = smf.ols("wt~cal",data=calo).fit()

# For getting coefficients of the varibles used in equation
model.params

# P-values for the variables and R-squared value for prepared model
model.summary()

model.conf_int(0.05) # 95% confidence interval

pred = model.predict(pd.DataFrame(calo['wt']))

这会引发一个错误:

Traceback (most recent call last):

  File "<ipython-input-43-4fcbf1ee1921>", line 1, in <module>
    pred = model.predict(pd.DataFrame(calo['wt']))

  File "/anaconda3/lib/python3.7/site-packages/statsmodels/base/model.py", line 837, in predict
    exog = dmatrix(design_info, exog, return_type="dataframe")

  File "/anaconda3/lib/python3.7/site-packages/patsy/highlevel.py", line 291, in dmatrix
    NA_action, return_type)

  File "/anaconda3/lib/python3.7/site-packages/patsy/highlevel.py", line 169, in _do_highlevel_design
    return_type=return_type)

  File "/anaconda3/lib/python3.7/site-packages/patsy/build.py", line 888, in build_design_matrices
    value, is_NA = _eval_factor(factor_info, data, NA_action)

  File "/anaconda3/lib/python3.7/site-packages/patsy/build.py", line 63, in _eval_factor
    result = factor.eval(factor_info.state, data)

  File "/anaconda3/lib/python3.7/site-packages/patsy/eval.py", line 566, in eval
    data)

  File "/anaconda3/lib/python3.7/site-packages/patsy/eval.py", line 551, in _eval
    inner_namespace=inner_namespace)

  File "/anaconda3/lib/python3.7/site-packages/patsy/compat.py", line 43, in call_and_wrap_exc
    exec("raise new_exc from e")

  File "<string>", line 1, in <module>

PatsyError: Error evaluating factor: NameError: name 'cal' is not defined
    wt~cal
       ^^^

需要您的帮助来解决这个问题。

提前致谢。:)

4

3 回答 3

1

查看此处的 statsmodels API ,看起来他们期望参数作为输入,而不是协变量。

所以你可能想要的是

pred = model.predict(model.params)
于 2019-03-20T02:17:30.090 回答
0

您需要放置一个变量,您将根据该变量来决定因变量(y)

model = statsmodels.formula.api.ols('y ~x ',data=df)
model.predict(pd.DataFrame(df['x']))
于 2021-12-07T06:20:43.157 回答
0

我遇到了这个问题。我正在做这样的事情:

for _, i in frame.iterrows()
    model.predict(i)

这没有为它提供必要的标题。你必须这样做:

for _, i in frame.iterrows()
    model.predict(pd.DataFrame([i]))
于 2022-01-09T05:11:11.503 回答