Search code examples
pythondata-sciencepearson-correlation

Why do i keep gettig an error message o this code


this is an IBM skill lab code. try running it and keeps getting a error

pearson_coef, p_value = stats.pearsonr(df['city-mpg'], df['price'])
print("The Pearson Correlation Coefficient is", pearson_coef, " with a P-value of P = ", p_value)  

error message

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Input In [71], in <cell line: 1>()
----> 1 pearson_coef, p_value = stats.pearsonr(df['city-mpg'], df['price'])
      2 print("The Pearson Correlation Coefficient is", pearson_coef, " with a P-value of P = ", p_value)

NameError: name 'stats' is not defined
​

Solution

  • You have to import stats - which without knowing what package you're trying to use - I would assume is the stats package from scipy

    Add the following line at the top of your cell or file

    # Import statistical package from Scipy
    from scipy import stats