Last answered:

22 Nov 2023

Posted on:

21 Nov 2023

0

facing error while running following code

y = data['GPA']
x1 = [['SAT', 'Attendance']]

till this statement my prgram runs well but after

x = sm.add_constant(x1)
results = sm.OLS(y,x).fit()
results.summary()

my code gives me an error as per the snap attached

6 answers ( 0 marked as helpful)
Instructor
Posted on:

21 Nov 2023

0

Hey Hazrat,


Thanks for reaching out!


Please, refer to the following thread where a similar question is resolved:

https://365datascience.com/q/2a355307fc


Let me know if you face other issues.


Kind regards,

365 Hristina

Posted on:

21 Nov 2023

0

Nope im still getting the same message

Instructor
Posted on:

21 Nov 2023

0

Hey,


Could you please take a screenshot of the entire error message?


Additionally, could you download and run the notebook provided in the resources and let me know if you encounter issues running it?



Best,

Hristina

Posted on:

21 Nov 2023

0

---------------------------------------------------------------------------
MissingDataError                          Traceback (most recent call last)
Cell In[16], line 2
      1 x = sm.add_constant(x1)
----> 2 results = sm.OLS(y,x).fit
      3 results.summary()

File ~\anaconda3\Lib\site-packages\statsmodels\regression\linear_model.py:922, in OLS.__init__(self, endog, exog, missing, hasconst, **kwargs)
    919     msg = ("Weights are not supported in OLS and will be ignored"
    920            "An exception will be raised in the next version.")
    921     warnings.warn(msg, ValueWarning)
--> 922 super(OLS, self).__init__(endog, exog, missing=missing,
    923                           hasconst=hasconst, **kwargs)
    924 if "weights" in self._init_keys:
    925     self._init_keys.remove("weights")

File ~\anaconda3\Lib\site-packages\statsmodels\regression\linear_model.py:748, in WLS.__init__(self, endog, exog, weights, missing, hasconst, **kwargs)
    746 else:
    747     weights = weights.squeeze()
--> 748 super(WLS, self).__init__(endog, exog, missing=missing,
    749                           weights=weights, hasconst=hasconst, **kwargs)
    750 nobs = self.exog.shape[0]
    751 weights = self.weights

File ~\anaconda3\Lib\site-packages\statsmodels\regression\linear_model.py:202, in RegressionModel.__init__(self, endog, exog, **kwargs)
    201 def __init__(self, endog, exog, **kwargs):
--> 202     super(RegressionModel, self).__init__(endog, exog, **kwargs)
    203     self.pinv_wexog: Float64Array | None = None
    204     self._data_attr.extend(['pinv_wexog', 'wendog', 'wexog', 'weights'])

File ~\anaconda3\Lib\site-packages\statsmodels\base\model.py:270, in LikelihoodModel.__init__(self, endog, exog, **kwargs)
    269 def __init__(self, endog, exog=None, **kwargs):
--> 270     super().__init__(endog, exog, **kwargs)
    271     self.initialize()

File ~\anaconda3\Lib\site-packages\statsmodels\base\model.py:95, in Model.__init__(self, endog, exog, **kwargs)
     93 missing = kwargs.pop('missing', 'none')
     94 hasconst = kwargs.pop('hasconst', None)
---> 95 self.data = self._handle_data(endog, exog, missing, hasconst,
     96                               **kwargs)
     97 self.k_constant = self.data.k_constant
     98 self.exog = self.data.exog

File ~\anaconda3\Lib\site-packages\statsmodels\base\model.py:135, in Model._handle_data(self, endog, exog, missing, hasconst, **kwargs)
    134 def _handle_data(self, endog, exog, missing, hasconst, **kwargs):
--> 135     data = handle_data(endog, exog, missing, hasconst, **kwargs)
    136     # kwargs arrays could have changed, easier to just attach here
    137     for key in kwargs:

File ~\anaconda3\Lib\site-packages\statsmodels\base\data.py:675, in handle_data(endog, exog, missing, hasconst, **kwargs)
    672     exog = np.asarray(exog)
    674 klass = handle_data_class_factory(endog, exog)
--> 675 return klass(endog, exog=exog, missing=missing, hasconst=hasconst,
    676              **kwargs)

File ~\anaconda3\Lib\site-packages\statsmodels\base\data.py:88, in ModelData.__init__(self, endog, exog, missing, hasconst, **kwargs)
     86 self.const_idx = None
     87 self.k_constant = 0
---> 88 self._handle_constant(hasconst)
     89 self._check_integrity()
     90 self._cache = {}

File ~\anaconda3\Lib\site-packages\statsmodels\base\data.py:134, in ModelData._handle_constant(self, hasconst)
    132 exog_max = np.max(self.exog, axis=0)
    133 if not np.isfinite(exog_max).all():
--> 134     raise MissingDataError('exog contains inf or nans')
    135 exog_min = np.min(self.exog, axis=0)
    136 const_idx = np.where(exog_max == exog_min)[0].squeeze()

MissingDataError: exog contains inf or nans

Posted on:

21 Nov 2023

0

this is the error msg i cant take a screen short due to large scale

and secondly after retsarting my jupyter my 0 is printed as 0.0 and 1 as NAN

Instructor
Posted on:

22 Nov 2023

0

Hey again Hazrat,


Regarding the last screenshot, note that in your code, the first letter of the word "Yes" is written in lowercase while it probably needs to be uppercase. Correcting this will likely resolve the error you have.


In case it doesn't resolve it, please download and run the notebook provided in the resources and let me know if you run into an error.


Best,

365 Hristina 

Submit an answer