Create TargetMeanPredictor class to enhance SelectByTargetMeanPerformance - #358
Create TargetMeanPredictor class to enhance SelectByTargetMeanPerformance #358Morgan-Sell wants to merge 157 commits into
Conversation
|
Hi @solegalli, I've made some progress on the TargetMeanPredictor class. In PR #334, we discussed creating the codebase so the Issues:
I suspect it is cumbersome to revise all the inheritance rules/checks from the parent classes, e.g., BaseEncoder, and the encoders/discretisers that are instantiated w/in the TargetMeanPredictor class. One solution could be to require that the user inputs a dataframe w/ the same dimensions and variable names as the dataframe that is used to fit the TargetMeanPredictor. The Do you have any thoughts? If so, please share! Also, the codebase is a WIP, so it's not in the best shape ;) |
solegalli
left a comment
There was a problem hiding this comment.
Hi @Morgan-Sell
Thank you for the code start. I made a few comments. Could you take a look?
Thank you!
There was a problem hiding this comment.
Hi @Morgan-Sell
Thank you so much for the quick turnaround with the code.
I made some suggestions on how to re-write the transform() method that should answer the issues you raised in your last comment.
If you still encounter errors, I would say, write a couple of tests, it doesn't matter that they are failing, because tests normally helps us (or at least me :p) understand where the problem is coming from.
transform(X) should take in the same dataframe that was used in fit(). Fit can take a df with multiple columns or a df with 1 column. sklearn classes do not take in pandas series, so if we have a series, we need to add .to_frame() for it to work. Please add a test using a series.to_frame() to test the functionality as well.
If something does not work or you feel stuck, don't worry, just draft some tests and we take it from there.
Thank you!
| the values will be sorted. | ||
|
|
||
| strategy: str, default='equal_width' | ||
| Whether to create the bins for discretization of numerical variables using |
There was a problem hiding this comment.
Replace
Whether the bins should be of equal width ('equal_width') or equal frequency ('equal_frequency').
There was a problem hiding this comment.
Should we use an underscore or hyphen, e.g., equal_width vs equal-width? I feel like I've seen both uses or I may be confusing feature-engine and sklearn semantics. I'm adopting your pedantic tendencies ;)
There was a problem hiding this comment.
trailing underscores are only added to attributes that learn parameters during the fit() method as per sklearn conventions.
There was a problem hiding this comment.
I'm referring to the potential values for the self.strategy parameter. The parameter can either be equal-width or equal-distance.
Should I use (1) equal_distance and equal_width or (2) equal-distance and equal-width?
Option 1 uses an underscore. Option 2 uses a hyphen.
I suspect we want to be consistent w/ sklearn and within the feature-engine library.
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def df_pred(): |
There was a problem hiding this comment.
lets more this df into a conftest inside the test_prediction module.
There was a problem hiding this comment.
Done. Will these dataframes only be used for predictor classes? Therefore we create a separate conftest file.
|
Muchas gracias por el feedback, @solegalli! I'm still refactoring the codebase. It's a long list ;) In the meantime, please poke and prod where you see fit! |
|
Hi @solegalli, A few questions:
|
|
Hi @Morgan-Sell Before I review, could you please try and review the files committed to this PR? I think something went wrong with rebase. Check out main, pull upstream main to your main branch, then check out your feature branch, rebase main onto it, and then force push. thank you! |
…sts w/in df. sucessfully printed sliced df using column names
|
Hola @solegalli, I implemented all the variable-manipulation unit tests except for I'm stuck! What am I missing? :( Also, like PR #360, the code is returning |
solegalli
left a comment
There was a problem hiding this comment.
Hi @Morgan-Sell
Well done with the code changes and the tests. This is looking really good.
I wonder if you could experiment with a few functions from sklearn that are used for classifiers and predictors and expand the tests a bit more.
See my comments below.
Thank you!
| assert _find_all_variables(df_vartypes, non_existing_vars) | ||
|
|
||
|
|
||
| def test_find_categorical_and_numeric_variables_one_categorical_variables( |
There was a problem hiding this comment.
could we put all these tests together (into 1) and use parametrize?
also, can we use a df that already exists? like df_vartypes?
There was a problem hiding this comment.
It seems that the conftest dataframes are either (1) not recognized if they are not imported or (2) stay as a function object.
Is there an example of passing a conftest dataframe to @pytest.mark.parametrize?
| """ | ||
| # check if 'X' is a dataframe | ||
| _is_dataframe(X) | ||
|
|
There was a problem hiding this comment.
Hi @Morgan-Sell
would you be so kind to experiment with adding this check and see if the tests still pass:
# Check that X and y have correct shape
X, y = check_X_y(X, y, dtype=None)
check_X_y is from sklearn and supposedly checks that X and y fulfill some criteria.
There was a problem hiding this comment.
I played w/ check_X_y. The function returns numpy arrays which lack a dtype and cause errors downstream.
I tried transforming X and y into pandas dataframe and series after check_X_y was executed. This caused ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). to occur for various unit tests.
I think the root of both errors exists when the MeanEncoder is applied.
|
|
||
| # check inf | ||
| _check_contains_inf(X, self.variables_numerical_) | ||
|
|
There was a problem hiding this comment.
if we use Check_X-y we do not need to check for na and inf ourselves. In theory. But I have not tried.
I was wondering if you could try commenting this out and see if the tests still pass?
There was a problem hiding this comment.
See prior comment.
…to @pytest.mark.parameterize?
…to @pytest.mark.parameterize?
…b/c fcn returns numpy arrays which don't have dtypes
… test_attributes_upon_fitting()
|
Hola @solegalli, Shoudl we delete the tests |
|
Hola @solegalli, I haven't heard from you in a while. I hope you're busy having fun! Do you have any thoughts on my above questions or the classes in general? Abrazo! |
Yep. Sorry. Actually I was working on a different PR. This one is a bit out of my comfort zone so I need to do some research to get more familiar with sklearn classifiers and regressors mixins. It'll take me a little longer. |
SelectByTargetMeanPerformance class will call TargetMeanPredictor allowing the use of cross-validation to return the important features.
Important links:
https://scikit-learn.org/stable/glossary.html#term-predictors
https://scikit-learn.org/stable/glossary.html#term-classifier
Important: in a classifier predict() should output the real label, not 1, 0:
https://scikit-learn.org/stable/glossary.html#term-predict
check example classifier here:
https://scikit-learn.org/stable/developers/develop.html#rolling-your-own-estimator