-
Notifications
You must be signed in to change notification settings - Fork 724
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed inference results when cate_feature_names
is not defined
#225
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
import numpy as np | ||
import unittest | ||
from sklearn.base import clone | ||
from sklearn.preprocessing import PolynomialFeatures | ||
from econml.dml import LinearDMLCateEstimator | ||
|
||
|
||
class TestInference(unittest.TestCase): | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
np.random.seed(123) | ||
# DGP constants | ||
cls.n = 1000 | ||
cls.d_w = 30 | ||
cls.d_x = 5 | ||
# Generate data | ||
cls.X = np.random.uniform(0, 1, size=(cls.n, cls.d_x)) | ||
cls.W = np.random.normal(0, 1, size=(cls.n, cls.d_w)) | ||
cls.T = np.random.normal(0, 1, size=(cls.n, )) | ||
cls.Y = np.random.normal(0, 1, size=(cls.n, )) | ||
|
||
def test_inference_results(self): | ||
"""Tests the inference results summary.""" | ||
# Test inference results when `cate_feature_names` doesn not exist | ||
cate_est = LinearDMLCateEstimator( | ||
featurizer=PolynomialFeatures(degree=1, | ||
include_bias=False) | ||
) | ||
wrapped_est = self._NoFeatNamesEst(cate_est) | ||
wrapped_est.fit( | ||
TestInference.Y, | ||
TestInference.T, | ||
TestInference.X, | ||
TestInference.W, | ||
inference='statsmodels' | ||
) | ||
summary_results = wrapped_est.summary() | ||
coef_rows = np.asarray(summary_results.tables[0].data)[1:, 0] | ||
np.testing.assert_array_equal(coef_rows, ['X{}'.format(i) for i in range(TestInference.d_x)]) | ||
|
||
class _NoFeatNamesEst: | ||
def __init__(self, cate_est): | ||
self.cate_est = clone(cate_est, safe=False) | ||
|
||
def __getattr__(self, name): | ||
if name != 'cate_feature_names': | ||
return getattr(self.cate_est, name) | ||
else: | ||
return self.__getattribute__(name) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rename to something like
test_inference_no_feature_names
or something.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I named it this way so we can add more tests under the same method in the future. And this is just one subtest.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay; I generally prefer to have a larger number of fine grained tests than a smaller number of broader tests where possible because it makes it quicker to track down the nature of the error when there's a failure, but I don't feel super strongly about it.