Resolved: Problem with the attached code
The attached code does not work.
It seems to be a problem related to the version of the sci-kit learn package. I have just re-downloaded it, and the problem remains. It appears with the lines:
reg_with_pvalues = LinearRegression()
reg_with_pvalues.fit(x,y)
The error message is:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-29-fa0b9ff40e4e> in <module>
1 # When we create the regression everything is the same
2 reg_with_pvalues = LinearRegression()
----> 3 reg_with_pvalues.fit(x,y)
<ipython-input-28-9291cc26caba> in fit(self, X, y, n_jobs)
29
30 def fit(self, X, y, n_jobs=1):
---> 31 self = super(LinearRegression, self).fit(X, y, n_jobs)
32
33 # Calculate SSE (sum of squared errors)
~\Anaconda3\lib\site-packages\sklearn\linear_model\_base.py in fit(self, X, y, sample_weight)
514 n_jobs_ = self.n_jobs
515
--> 516 accept_sparse = False if self.positive else ['csr', 'csc', 'coo']
517
518 X, y = self._validate_data(X, y, accept_sparse=accept_sparse,
AttributeError: 'LinearRegression' object has no attribute 'positive'
Hey Omar,
Thank you for reaching out!
As of version 0.24 of sklearn, the parameter positive
is added to the constructor. Therefore, you should change the __init__
function in the following way:
# nothing changes in __init__
def __init__(self, fit_intercept=True, normalize=False, copy_X=True,
n_jobs=1, positive = False):
self.fit_intercept = fit_intercept
self.normalize = normalize
self.copy_X = copy_X
self.n_jobs = n_jobs
self.positive = positive
Here, I have added positive = False
as the default value and have added the following line at the end:
self.positive = positive
This should help you solve the error.
Kind regards,
365 Hristina
Thank you, Hristina. It worked, but it gives slightly different results as with the StatsModels package:
p-values for SAT and Rand1,2,3 respectively for each run:
- sklearn.LinearRegression before fix: 0.000 and 0.676
- sklearn.LinearRegression before fix: 0.000 and 0.757
- StastsModels: 0.000 and 0.762
The difference is minuscule. Is it supposed to be? Or is there a way to make them coincide exactly?
Kind regards,
Hi, is there any course of 365 that can help me improve my Object-Oriented Programming skill? Thanks for your help !
Hey,
Thank you for reaching out!
Python and R are the two object-oriented programming languages that we offer courses on. You can find the introductory and intermediate courses in the Programming for Data Science module:
Introduction to Python
Python Programmer Bootcamp
Intermediate Python Programming
Introduction to R Programming
In the same module, you can find other Python courses dealing with more specific topics such as preprocessing with the NumPy
and pandas
libraries, using the matplotlib
library for visualizations, working with dates, times, text files, etc.
Hope this helps!
Kind regards,
365 Hristina