0
$\begingroup$
# adding constant to the train data
x_train1 = sm.add_constant(x_train) # adding constant to the test data
x_test1 = sm.add_constant(x_test) # Complete the code to add constant to the test data

olsmodel1 = sm.OLS(x_train, y_train).fit()
olsmodel1
# checking for missing values
df.isnull().sum()
## Complete the code to check missing values in all the columns
​
brand_name            0
os                    0
screen_size           0
4g                    0
5g                    0
main_camera_mp      179
selfie_camera_mp      2
int_memory            4
ram                   4
battery               6
weight                7
release_year          0
days_used             0
new_price             0
used_price            0
used_price_log        0
new_price_log         0
device_category       0
dtype: int64
print(olsmodel1.summary())
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/var/folders/6j/zjjqp3y160ddq0qjnph_7r0w0000gp/T/ipykernel_29575/2464932037.py in <module>
----> 1 print(olsmodel1.summary())

/opt/anaconda3/lib/python3.9/site-packages/statsmodels/regression/linear_model.py in summary(self, yname, xname, title, alpha)
   2638         rsquared_type = '' if self.k_constant else ' (uncentered)'
   2639         top_right = [('R-squared' + rsquared_type + ':',
-> 2640                       ["%#8.3f" % self.rsquared]),
   2641                      ('Adj. R-squared' + rsquared_type + ':',
   2642                       ["%#8.3f" % self.rsquared_adj]),

/opt/anaconda3/lib/python3.9/site-packages/pandas/_libs/properties.pyx in pandas._libs.properties.CachedProperty.__get__()

/opt/anaconda3/lib/python3.9/site-packages/statsmodels/regression/linear_model.py in rsquared(self)
   1715             return 1 - self.ssr/self.centered_tss
   1716         else:
-> 1717             return 1 - self.ssr/self.uncentered_tss
   1718 
   1719     @cache_readonly

/opt/anaconda3/lib/python3.9/site-packages/pandas/_libs/properties.pyx in pandas._libs.properties.CachedProperty.__get__()

/opt/anaconda3/lib/python3.9/site-packages/statsmodels/regression/linear_model.py in ssr(self)
   1654         """Sum of squared (whitened) residuals."""
   1655         wresid = self.wresid
-> 1656         return np.dot(wresid, wresid)
   1657 
   1658     @cache_readonly

<__array_function__ internals> in dot(*args, **kwargs)

ValueError: shapes (2417,47) and (2417,47) not aligned: 47 (dim 1) != 2417 (dim 0)
$\endgroup$

1 Answer 1

0
$\begingroup$

The OLS model from statsmodels uses arguments for the data in a different order than is used for scikit-learn, meaning that the exogenous variables come second after the endogenous variable (see also the statsmodels documentation.

olsmodel1 = sm.OLS(y_train, x_train).fit()
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.