Issue
I was wondering what the correct way is to pass the columns in a fit function. For example, I have:
# Lin. regression
lin_reg = LinearRegression().fit(data[["X"]],
data[["Y"]])
#Poisson regression
log_reg = PoissonRegressor().fit(data[["X"]],
data.loc[:, "Y"])
So in the linear regression, the lines presented above just work, even though I pass the y-column as data[["Y"]]
. If I were to do that in the poisson regression (e.g. see below).
log_reg = PoissonRegressor().fit(data[["X"]],
data[["Y"]])
It would give a warning "A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().".
Now, I would like to know what the proper way is to pass the y-value to scikit-learn and how it comes that the linear regression does not cause any problems.
I am fully aware that data[["Y"]]
results in a dataframe, so I guess that is the difference, but I am not sure why the linear regression would accept a dataframe while the poisson regression does not.
Therefore, what is the proper way to pass an y-value to scikit learn.
Solution
The proper way to pass y
is to pass an array-like of the specified shape of the corresponding method. So you need to know two things:
1) What is an array-like?
The reference for what constitutes as an array-like can be found here. Any of the specified objects there are valid and can be used. I believe there is no special preference as scikit-learn
will internally convert them to numpy
arrays anyway.
2) What shape is required?
Using your examples, you will see that the fit
methods of LinearRegression
and PoissonRegressor
will accept different shapes.
Here for LinearRegression
:
y: array-like of shape (n_samples,) or (n_samples, n_targets)
And here for PoissonRegressor
:
y: array-like of shape (n_samples,)
Since LinearRegression
does support multioutput regression, it accepts array-likes of shape (n_samples, n_targets)
which could also be (n_samples, 1)
like in your case. So there is no issue.
PoissonRegressor
on the other hand only allows for shapes of (n_samples,)
. That`s why it complains that your input does not comply with the specifications.
However, note that it still works since it can convert the data into the specified shape. But you get a warning so you know that only 1d arrays are meant to be used here as there is no support for (potential) multioutput.
If you want to avoid any issues, follow the error message's suggestion and use ravel
to flatten the input. For example in your case:
log_reg = PoissonRegressor().fit(data[["X"]],
data[["Y"]].values.ravel())
values
returns the corresponding numpy
array and ravel
will convert it into a 1d array.
Answered By - afsharov
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.