我需要帮助的代码部分在下面三行,错误消息直接在代码的下面。
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中以单独的代码块运行了此代码,并以怪异的顺序执行了吗?
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/