gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 19:46:29 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com