python - 如何定义下面的 python 作业的 'cars_df' 部分?

标签 python pandas

我需要帮助的代码部分是下面三行,错误消息就在该部分代码的正下方。

from statsmodels.formula.api import ols
# create the simple linear regression 
# model with mpg as the response variable
# and weight as the predictor variable
model = ols('mpg ~ wt', data = cars_df).fit()
#print the model summary
print(model.summary())
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-4-375f5286ec63> in <module>
      2 
      3 # create the simple linear regression model with mpg as the response variable and weight as the predictor variable
----> 4 model = ols('mpg ~ wt', data = cars_df).fit()
      5 
      6 #print the model summary

NameError: name 'cars_df' is not defined

cars_df 在之前的脚本行中使用过并且有效。我将在下面附上我以前的代码以获取更多上下文。谢谢你,我很感激任何帮助。

import pandas as pd
from IPython.display import display, HTML

# read data from mtcars.csv data set.
cars_df_orig = pd.read_csv("https://s3-us-west-2.amazonaws.com/data- 
analytics.zybooks.com/mtcars.csv")

# randomly pick 30 observations without
# replacement from mtcars dataset to make the data 
# unique to you.
cars_df = cars_df_orig.sample(n=30, replace=False)

# print only the first five observations in the data set.
print("\nCars data frame (showing only the first five observations)")
display(HTML(cars_df.head().to_html()))

import matplotlib.pyplot as plt

# create scatterplot of variables mpg against wt.
plt.plot(cars_df["wt"], cars_df["mpg"], 'o', color='red')

# set a title for the plot, x-axis, and y-axis.
plt.title('MPG against Weight')
plt.xlabel('Weight (1000s lbs)')
plt.ylabel('MPG')

# show the plot.
plt.show()

# create correlation matrix for mpg and wt. 
# the correlation coefficient between mpg 
# and wt is contained in the cell for mpg row and wt 
# column (or wt row and mpg column) 
mpg_wt_corr = cars_df[['mpg','wt']].corr()
print(mpg_wt_corr)

from statsmodels.formula.api import ols
# create the simple linear regression 
# model with mpg as the response variable
# and weight as the predictor variable
model = ols('mpg ~ wt', data = cars_df).fit()
#print the model summary
print(model.summary())

最佳答案

我刚刚按原样运行了您的整个代码,并且运行良好。您可能在 Jupyter Notebook 中以单独的 block 运行此代码,并以奇怪的顺序执行?


Cars data frame (showing only the first five observations)
Unnamed: 0  mpg cyl disp    hp  drat    wt  qsec    vs  am  gear    carb
29  Ferrari Dino    19.7    6   145.0   175 3.62    2.770   15.5    0   1   5   6
28  Ford Pantera L  15.8    8   351.0   264 4.22    3.170   14.5    0   1   5   4
13  Merc 450SLC 15.2    8   275.8   180 3.07    3.780   18.0    0   0   3   3
7   Merc 240D   24.4    4   146.7   62  3.69    3.190   20.0    1   0   4   2
22  AMC Javelin 15.2    8   304.0   150 3.15    3.435   17.3    0   0   3   2
<Figure size 640x480 with 1 Axes>
          mpg        wt
mpg  1.000000 -0.857965
wt  -0.857965  1.000000
                            OLS Regression Results                            
==============================================================================
Dep. Variable:                    mpg   R-squared:                       0.736
Model:                            OLS   Adj. R-squared:                  0.727
Method:                 Least Squares   F-statistic:                     78.10
Date:                Thu, 06 Feb 2020   Prob (F-statistic):           1.37e-09
Time:                        18:32:25   Log-Likelihood:                -75.042
No. Observations:                  30   AIC:                             154.1
Df Residuals:                      28   BIC:                             156.9
Df Model:                           1                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
Intercept     37.4745      1.989     18.846      0.000      33.401      41.548
wt            -5.3758      0.608     -8.838      0.000      -6.622      -4.130
==============================================================================
Omnibus:                        3.033   Durbin-Watson:                   1.606
Prob(Omnibus):                  0.219   Jarque-Bera (JB):                2.429
Skew:                           0.693   Prob(JB):                        0.297
Kurtosis:                       2.853   Cond. No.                         12.7
==============================================================================

Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.

关于python - 如何定义下面的 python 作业的 'cars_df' 部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60106081/

相关文章:

python - 如何下载和读取带有通用换行符的 URL?

python - 带有 text.usetex 'true' 的轴字体不使用设置字体

python - Google Cloud Dataflow Python,检索作业 ID

python - 带有 Python 请求的 XML POST

python - 'else if' 的正确语法是什么?

python - 有没有什么有效的方法可以用python编写这段代码

python - Pandas /Numpy : remove leading/trailing nan in a pandas series or numpy array

python - 在现有表中创建新列并使用 python 更新列

python - 在 Pandas MultiIndex DataFrame中选择行

python - 将工作表名称添加到excel中自负的最终合并工作表中