diff --git a/econml/_cate_estimator.py b/econml/_cate_estimator.py index e22965e1f..172a01507 100644 --- a/econml/_cate_estimator.py +++ b/econml/_cate_estimator.py @@ -10,10 +10,10 @@ from copy import deepcopy from warnings import warn from .inference import BootstrapInference -from .utilities import (tensordot, ndim, reshape, shape, parse_final_model_params, get_feature_names_or_default, - inverse_onehot, Summary, get_input_columns, check_input_arrays, jacify_featurizer) +from .utilities import (ndim, shape, parse_final_model_params, get_feature_names_or_default, + inverse_onehot, Summary, get_input_columns, check_input_arrays) from .inference import StatsModelsInference, StatsModelsInferenceDiscrete, LinearModelFinalInference, \ - LinearModelFinalInferenceDiscrete, NormalInferenceResults, GenericSingleTreatmentModelFinalInference, \ + LinearModelFinalInferenceDiscrete, GenericSingleTreatmentModelFinalInference, \ GenericModelFinalInferenceDiscrete from ._shap import _shap_explain_cme, _shap_explain_joint_linear_model_cate from .dowhy import DoWhyWrapper diff --git a/econml/_ensemble/_ensemble.py b/econml/_ensemble/_ensemble.py index 9412b8737..345b5e93d 100644 --- a/econml/_ensemble/_ensemble.py +++ b/econml/_ensemble/_ensemble.py @@ -13,7 +13,6 @@ import numpy as np from abc import ABCMeta, abstractmethod from sklearn.base import BaseEstimator, clone -from sklearn.utils import check_random_state from joblib import effective_n_jobs from packaging.version import parse import sklearn diff --git a/econml/_ortho_learner.py b/econml/_ortho_learner.py index 3c14df2ee..b411314da 100644 --- a/econml/_ortho_learner.py +++ b/econml/_ortho_learner.py @@ -24,28 +24,22 @@ class in this module implements the general logic in a very versatile way """ -import copy from collections import namedtuple -from warnings import warn from abc import abstractmethod from typing import List, Union -import inspect -from collections import defaultdict -import re import numpy as np from sklearn.base import clone from sklearn.model_selection import KFold, StratifiedKFold, GroupKFold, StratifiedGroupKFold, check_cv -from sklearn.preprocessing import (FunctionTransformer, LabelEncoder, - OneHotEncoder) +from sklearn.preprocessing import (LabelEncoder) from sklearn.utils import check_random_state from ._cate_estimator import (BaseCateEstimator, LinearCateEstimator, TreatmentExpansionMixin) from .inference import BootstrapInference -from .utilities import (_deprecate_positional, check_input_arrays, - cross_product, filter_none_kwargs, one_hot_encoder, strata_from_discrete_arrays, - inverse_onehot, jacify_featurizer, ndim, reshape, shape, transpose) +from .utilities import (check_input_arrays, + filter_none_kwargs, one_hot_encoder, strata_from_discrete_arrays, + jacify_featurizer, reshape, shape) from .sklearn_extensions.model_selection import ModelSelector try: diff --git a/econml/cate_interpreter/_interpreters.py b/econml/cate_interpreter/_interpreters.py index bd4443e58..5806979a2 100644 --- a/econml/cate_interpreter/_interpreters.py +++ b/econml/cate_interpreter/_interpreters.py @@ -4,8 +4,7 @@ import abc import numbers import numpy as np -import sklearn -from sklearn.tree import DecisionTreeRegressor, DecisionTreeClassifier +from sklearn.tree import DecisionTreeRegressor from sklearn.utils import check_array from ..policy import PolicyTree from .._tree_exporter import (_SingleTreeExporterMixin, diff --git a/econml/data/dynamic_panel_dgp.py b/econml/data/dynamic_panel_dgp.py index ee4e84333..8ae72588f 100644 --- a/econml/data/dynamic_panel_dgp.py +++ b/econml/data/dynamic_panel_dgp.py @@ -372,7 +372,7 @@ class SemiSynthetic: def create_instance(self): # get new covariance matrix - self.cov_new = joblib.load(os.path.join(dir, f"input_dynamicdgp/cov_new.jbl")) + self.cov_new = joblib.load(os.path.join(dir, "input_dynamicdgp/cov_new.jbl")) # get coefs self.index = ["proxy1", "proxy2", "proxy3", "proxy4", diff --git a/econml/dml/_rlearner.py b/econml/dml/_rlearner.py index 010c83567..25621699a 100644 --- a/econml/dml/_rlearner.py +++ b/econml/dml/_rlearner.py @@ -27,13 +27,9 @@ from abc import abstractmethod import numpy as np -import copy -from warnings import warn from ..sklearn_extensions.model_selection import ModelSelector -from ..utilities import (shape, reshape, ndim, hstack, filter_none_kwargs, _deprecate_positional) -from sklearn.linear_model import LinearRegression -from sklearn.base import clone +from ..utilities import (filter_none_kwargs) from .._ortho_learner import _OrthoLearner diff --git a/econml/dml/causal_forest.py b/econml/dml/causal_forest.py index d2cf7269a..1437b72ca 100644 --- a/econml/dml/causal_forest.py +++ b/econml/dml/causal_forest.py @@ -4,20 +4,15 @@ from warnings import warn import numpy as np -from sklearn.linear_model import LogisticRegressionCV -from sklearn.base import clone, BaseEstimator -from sklearn.preprocessing import FunctionTransformer -from sklearn.pipeline import Pipeline +from sklearn.base import clone from sklearn.model_selection import train_test_split from itertools import product from .dml import _BaseDML from .dml import _make_first_stage_selector -from ..sklearn_extensions.linear_model import WeightedLassoCVWrapper -from ..sklearn_extensions.model_selection import WeightedStratifiedKFold from ..inference import NormalInferenceResults from ..inference._inference import Inference -from ..utilities import (add_intercept, shape, check_inputs, check_input_arrays, - _deprecate_positional, cross_product, Summary) +from ..utilities import (shape, check_input_arrays, + cross_product, Summary) from ..grf import CausalForest, MultiOutputGRF from .._cate_estimator import LinearCateEstimator from .._shap import _shap_explain_multitask_model_cate diff --git a/econml/dml/dml.py b/econml/dml/dml.py index 8a1f88236..fd3a0dc35 100644 --- a/econml/dml/dml.py +++ b/econml/dml/dml.py @@ -5,36 +5,25 @@ import numpy as np from sklearn.base import TransformerMixin, clone -from sklearn.exceptions import NotFittedError -from sklearn.linear_model import (ElasticNetCV, LassoCV, LogisticRegressionCV) -from sklearn.ensemble import RandomForestRegressor -from sklearn.model_selection import KFold, StratifiedKFold, check_cv +from sklearn.linear_model import (ElasticNetCV) from sklearn.pipeline import Pipeline -from sklearn.preprocessing import (FunctionTransformer, LabelEncoder, - OneHotEncoder) +from sklearn.preprocessing import (FunctionTransformer) from sklearn.utils import check_random_state -import copy from .._ortho_learner import _OrthoLearner from ._rlearner import _RLearner from .._cate_estimator import (DebiasedLassoCateEstimatorMixin, - ForestModelFinalCateEstimatorMixin, LinearModelFinalCateEstimatorMixin, StatsModelsCateEstimatorMixin, LinearCateEstimator) -from ..inference import StatsModelsInference, GenericSingleTreatmentModelFinalInference +from ..inference import GenericSingleTreatmentModelFinalInference from ..sklearn_extensions.linear_model import (MultiOutputDebiasedLasso, - StatsModelsLinearRegression, - WeightedLassoCVWrapper) -from ..sklearn_extensions.model_selection import WeightedStratifiedKFold -from ..utilities import (_deprecate_positional, add_intercept, + StatsModelsLinearRegression) +from ..utilities import (add_intercept, broadcast_unit_treatments, check_high_dimensional, - cross_product, deprecated, - hstack, inverse_onehot, ndim, reshape, - reshape_treatmentwise_effects, shape, transpose, - get_feature_names_or_default, filter_none_kwargs) + cross_product, hstack, inverse_onehot, reshape_treatmentwise_effects, shape, get_feature_names_or_default, filter_none_kwargs) from .._shap import _shap_explain_model_cate -from ..sklearn_extensions.model_selection import get_selector, ModelSelector, SingleModelSelector +from ..sklearn_extensions.model_selection import get_selector, SingleModelSelector def _combine(X, W, n_samples): diff --git a/econml/dr/_drlearner.py b/econml/dr/_drlearner.py index b9a770e67..4bdd1a352 100644 --- a/econml/dr/_drlearner.py +++ b/econml/dr/_drlearner.py @@ -36,25 +36,20 @@ """ from warnings import warn -from copy import deepcopy import numpy as np from sklearn.base import clone -from sklearn.linear_model import (LassoCV, LinearRegression, - LogisticRegressionCV) -from sklearn.ensemble import RandomForestRegressor from .._ortho_learner import _OrthoLearner -from .._cate_estimator import (DebiasedLassoCateEstimatorDiscreteMixin, BaseCateEstimator, - ForestModelFinalCateEstimatorDiscreteMixin, +from .._cate_estimator import (DebiasedLassoCateEstimatorDiscreteMixin, ForestModelFinalCateEstimatorDiscreteMixin, StatsModelsCateEstimatorDiscreteMixin, LinearCateEstimator) from ..inference import GenericModelFinalInferenceDiscrete from ..grf import RegressionForest from ..sklearn_extensions.linear_model import ( - DebiasedLasso, StatsModelsLinearRegression, WeightedLassoCVWrapper) + DebiasedLasso, StatsModelsLinearRegression) from ..sklearn_extensions.model_selection import ModelSelector, SingleModelSelector, get_selector -from ..utilities import (_deprecate_positional, check_high_dimensional, +from ..utilities import (check_high_dimensional, filter_none_kwargs, inverse_onehot, get_feature_names_or_default) from .._shap import _shap_explain_multitask_model_cate, _shap_explain_model_cate diff --git a/econml/federated_learning.py b/econml/federated_learning.py index fb2068e51..0c28d9497 100644 --- a/econml/federated_learning.py +++ b/econml/federated_learning.py @@ -1,7 +1,6 @@ # Copyright (c) PyWhy contributors. All rights reserved. # Licensed under the MIT License. -import numpy as np from sklearn import clone from econml.utilities import check_input_arrays diff --git a/econml/grf/_base_grf.py b/econml/grf/_base_grf.py index 8255ba243..dac530fa6 100644 --- a/econml/grf/_base_grf.py +++ b/econml/grf/_base_grf.py @@ -10,19 +10,19 @@ # All rights reserved. import numbers -from warnings import catch_warnings, simplefilter, warn +from warnings import warn from abc import ABCMeta, abstractmethod import numpy as np import threading from .._ensemble import (BaseEnsemble, _partition_estimators, _get_n_samples_subsample, - _accumulate_prediction, _accumulate_prediction_var, _accumulate_prediction_and_var, + _accumulate_prediction, _accumulate_prediction_and_var, _accumulate_oob_preds) -from ..utilities import check_inputs, cross_product +from ..utilities import check_inputs from ..tree._tree import DTYPE, DOUBLE from ._base_grftree import GRFTree from joblib import Parallel, delayed from scipy.sparse import hstack as sparse_hstack -from sklearn.utils import check_random_state, compute_sample_weight +from sklearn.utils import check_random_state from sklearn.utils.validation import _check_sample_weight, check_is_fitted from sklearn.utils import check_X_y import scipy.stats diff --git a/econml/grf/_base_grftree.py b/econml/grf/_base_grftree.py index 44359f57a..7635dd59f 100644 --- a/econml/grf/_base_grftree.py +++ b/econml/grf/_base_grftree.py @@ -12,11 +12,8 @@ import numpy as np from ._criterion import LinearMomentGRFCriterionMSE, LinearMomentGRFCriterion from ..tree import BaseTree -from sklearn.model_selection import train_test_split -from sklearn.utils import check_array from sklearn.utils import check_random_state from sklearn.utils.validation import check_is_fitted -import copy # ============================================================================= # Types and constants diff --git a/econml/grf/classes.py b/econml/grf/classes.py index 6318f4cf3..46d0a22ab 100644 --- a/econml/grf/classes.py +++ b/econml/grf/classes.py @@ -2,7 +2,6 @@ # Licensed under the MIT License. import numpy as np -from warnings import warn from ..utilities import cross_product from ._base_grf import BaseGRF from ..utilities import check_inputs diff --git a/econml/inference/_bootstrap.py b/econml/inference/_bootstrap.py index 717d2d716..7993a9c74 100644 --- a/econml/inference/_bootstrap.py +++ b/econml/inference/_bootstrap.py @@ -6,8 +6,6 @@ from joblib import Parallel, delayed from sklearn.base import clone from scipy.stats import norm -from collections import OrderedDict -import pandas as pd class BootstrapEstimator: diff --git a/econml/inference/_inference.py b/econml/inference/_inference.py index be2e0f681..140756da7 100644 --- a/econml/inference/_inference.py +++ b/econml/inference/_inference.py @@ -9,14 +9,11 @@ import pandas as pd import scipy from scipy.stats import norm -from statsmodels.iolib.table import SimpleTable from ._bootstrap import BootstrapEstimator -from ..sklearn_extensions.linear_model import StatsModelsLinearRegression from ..utilities import (Summary, _safe_norm_ppf, broadcast_unit_treatments, cross_product, inverse_onehot, ndim, - parse_final_model_params, jacify_featurizer, - reshape_treatmentwise_effects, shape, filter_none_kwargs) + parse_final_model_params, reshape_treatmentwise_effects, shape, filter_none_kwargs) """Options for performing inference in estimators.""" diff --git a/econml/iv/dml/_dml.py b/econml/iv/dml/_dml.py index 3a3a3e361..741162147 100644 --- a/econml/iv/dml/_dml.py +++ b/econml/iv/dml/_dml.py @@ -15,20 +15,17 @@ import numpy as np from sklearn.base import clone -from sklearn.linear_model import LinearRegression, LogisticRegressionCV from sklearn.pipeline import Pipeline from sklearn.preprocessing import FunctionTransformer from itertools import product from ..._ortho_learner import _OrthoLearner -from ..._cate_estimator import LinearModelFinalCateEstimatorMixin, StatsModelsCateEstimatorMixin, LinearCateEstimator -from ...inference import StatsModelsInference, GenericSingleTreatmentModelFinalInference -from ...sklearn_extensions.linear_model import StatsModels2SLS, StatsModelsLinearRegression, WeightedLassoCVWrapper -from ...sklearn_extensions.model_selection import (ModelSelector, SingleModelSelector, - WeightedStratifiedKFold, get_selector) -from ...utilities import (_deprecate_positional, get_feature_names_or_default, filter_none_kwargs, add_intercept, +from ..._cate_estimator import LinearModelFinalCateEstimatorMixin, LinearCateEstimator +from ...sklearn_extensions.linear_model import StatsModels2SLS, StatsModelsLinearRegression +from ...sklearn_extensions.model_selection import (ModelSelector, SingleModelSelector) +from ...utilities import (get_feature_names_or_default, filter_none_kwargs, add_intercept, cross_product, broadcast_unit_treatments, reshape_treatmentwise_effects, shape, - parse_final_model_params, deprecated, Summary) + parse_final_model_params, Summary) from ...dml.dml import _make_first_stage_selector, _FinalWrapper from ...dml._rlearner import _ModelFinal from ..._shap import _shap_explain_joint_linear_model_cate, _shap_explain_model_cate diff --git a/econml/iv/dr/_dr.py b/econml/iv/dr/_dr.py index b7d62b4c2..d9e5b2e31 100644 --- a/econml/iv/dr/_dr.py +++ b/econml/iv/dr/_dr.py @@ -15,7 +15,6 @@ import numpy as np from sklearn.base import clone -from sklearn.linear_model import LinearRegression, LogisticRegressionCV from sklearn.pipeline import Pipeline from sklearn.preprocessing import FunctionTransformer from sklearn.dummy import DummyClassifier @@ -25,13 +24,12 @@ from ..._cate_estimator import (StatsModelsCateEstimatorMixin, DebiasedLassoCateEstimatorMixin, ForestModelFinalCateEstimatorMixin, GenericSingleTreatmentModelFinalInference, LinearCateEstimator) -from ...inference import StatsModelsInference -from ...sklearn_extensions.linear_model import StatsModelsLinearRegression, DebiasedLasso, WeightedLassoCVWrapper -from ...sklearn_extensions.model_selection import ModelSelector, SingleModelSelector, WeightedStratifiedKFold -from ...utilities import (_deprecate_positional, add_intercept, filter_none_kwargs, +from ...sklearn_extensions.linear_model import StatsModelsLinearRegression, DebiasedLasso +from ...sklearn_extensions.model_selection import ModelSelector, SingleModelSelector +from ...utilities import (add_intercept, filter_none_kwargs, inverse_onehot, get_feature_names_or_default, check_high_dimensional, check_input_arrays) from ...grf import RegressionForest -from ...dml.dml import _make_first_stage_selector, _FinalWrapper +from ...dml.dml import _make_first_stage_selector from ...iv.dml import NonParamDMLIV from ..._shap import _shap_explain_model_cate diff --git a/econml/iv/nnet/_deepiv.py b/econml/iv/nnet/_deepiv.py index 4a1b8f059..1f2b7ac9b 100644 --- a/econml/iv/nnet/_deepiv.py +++ b/econml/iv/nnet/_deepiv.py @@ -5,7 +5,7 @@ import numpy as np from ..._cate_estimator import BaseCateEstimator -from ...utilities import check_input_arrays, _deprecate_positional, deprecated, MissingModule +from ...utilities import check_input_arrays, MissingModule try: import keras from keras import backend as K diff --git a/econml/iv/sieve/_tsls.py b/econml/iv/sieve/_tsls.py index 38b22ac87..ffed3350d 100644 --- a/econml/iv/sieve/_tsls.py +++ b/econml/iv/sieve/_tsls.py @@ -4,12 +4,11 @@ """Provides a non-parametric two-stage least squares instrumental variable estimator.""" import numpy as np -from copy import deepcopy from sklearn import clone from sklearn.linear_model import LinearRegression from ...utilities import (shape, transpose, reshape, cross_product, ndim, size, - _deprecate_positional, check_input_arrays) -from ..._cate_estimator import BaseCateEstimator, LinearCateEstimator + check_input_arrays) +from ..._cate_estimator import BaseCateEstimator from numpy.polynomial.hermite_e import hermeval from sklearn.base import TransformerMixin from sklearn.preprocessing import PolynomialFeatures diff --git a/econml/metalearners/_metalearners.py b/econml/metalearners/_metalearners.py index d90b804b9..92ba4a337 100644 --- a/econml/metalearners/_metalearners.py +++ b/econml/metalearners/_metalearners.py @@ -9,15 +9,12 @@ """ import numpy as np -import warnings from .._cate_estimator import BaseCateEstimator, LinearCateEstimator, TreatmentExpansionMixin from sklearn import clone from sklearn.linear_model import LogisticRegression from sklearn.pipeline import Pipeline -from sklearn.utils import check_array, check_X_y -from sklearn.preprocessing import OneHotEncoder, FunctionTransformer -from ..utilities import (check_inputs, check_models, broadcast_unit_treatments, reshape_treatmentwise_effects, - one_hot_encoder, inverse_onehot, transpose, _deprecate_positional) +from sklearn.utils import check_array +from ..utilities import (check_inputs, check_models, broadcast_unit_treatments, one_hot_encoder, inverse_onehot, transpose) from .._shap import _shap_explain_model_cate diff --git a/econml/orf/_ortho_forest.py b/econml/orf/_ortho_forest.py index 71361b2dd..578c41336 100644 --- a/econml/orf/_ortho_forest.py +++ b/econml/orf/_ortho_forest.py @@ -20,28 +20,24 @@ For more details on these methods, see our paper [Oprescu2019]_. """ -import abc -import inspect import numpy as np import warnings from joblib import Parallel, delayed from sklearn import clone from scipy.stats import norm from sklearn.exceptions import NotFittedError -from sklearn.linear_model import LassoCV, Lasso, LinearRegression, LogisticRegression, \ - LogisticRegressionCV, ElasticNet +from sklearn.linear_model import LinearRegression, LogisticRegression, \ + LogisticRegressionCV from sklearn.model_selection import KFold, StratifiedKFold from sklearn.pipeline import Pipeline -from sklearn.preprocessing import OneHotEncoder, LabelEncoder, PolynomialFeatures, FunctionTransformer from sklearn.utils import check_random_state, check_array, column_or_1d from ..sklearn_extensions.linear_model import WeightedLassoCVWrapper from .._cate_estimator import BaseCateEstimator, LinearCateEstimator, TreatmentExpansionMixin from ._causal_tree import CausalTree from ..inference import NormalInferenceResults from ..inference._inference import Inference -from ..utilities import (one_hot_encoder, reshape, reshape_Y_T, MAX_RAND_SEED, check_inputs, _deprecate_positional, - cross_product, inverse_onehot, check_input_arrays, jacify_featurizer, - _RegressionWrapper, deprecated, ndim) +from ..utilities import (one_hot_encoder, reshape_Y_T, MAX_RAND_SEED, check_inputs, cross_product, check_input_arrays, jacify_featurizer, + _RegressionWrapper, ndim) from sklearn.model_selection import check_cv # TODO: consider working around relying on sklearn implementation details from ..sklearn_extensions.model_selection import _cross_val_predict diff --git a/econml/panel/dml/_dml.py b/econml/panel/dml/_dml.py index 63869580e..790b5a14a 100644 --- a/econml/panel/dml/_dml.py +++ b/econml/panel/dml/_dml.py @@ -1,23 +1,16 @@ # Copyright (c) PyWhy contributors. All rights reserved. # Licensed under the MIT License. -import abc import numpy as np from warnings import warn from sklearn.base import clone from sklearn.model_selection import GroupKFold -from scipy.stats import norm -from sklearn.linear_model import (ElasticNetCV, LassoCV, LogisticRegressionCV) -from ...sklearn_extensions.linear_model import (StatsModelsLinearRegression, WeightedLassoCVWrapper) -from ...sklearn_extensions.model_selection import ModelSelector, WeightedStratifiedKFold +from ...sklearn_extensions.linear_model import (StatsModelsLinearRegression) +from ...sklearn_extensions.model_selection import ModelSelector from ...dml.dml import _make_first_stage_selector, _FinalWrapper -from ..._cate_estimator import TreatmentExpansionMixin, LinearModelFinalCateEstimatorMixin +from ..._cate_estimator import LinearModelFinalCateEstimatorMixin from ..._ortho_learner import _OrthoLearner -from ...utilities import (_deprecate_positional, add_intercept, - broadcast_unit_treatments, check_high_dimensional, - cross_product, deprecated, - hstack, inverse_onehot, ndim, reshape, - reshape_treatmentwise_effects, shape, transpose, +from ...utilities import (ndim, reshape, get_feature_names_or_default, check_input_arrays, filter_none_kwargs) diff --git a/econml/policy/_base.py b/econml/policy/_base.py index 42546b1dd..844cff254 100644 --- a/econml/policy/_base.py +++ b/econml/policy/_base.py @@ -4,7 +4,6 @@ """Base classes for all Policy estimators.""" import abc -import numpy as np class PolicyLearner(metaclass=abc.ABCMeta): diff --git a/econml/policy/_drlearner.py b/econml/policy/_drlearner.py index 3a1333647..e9263cce8 100644 --- a/econml/policy/_drlearner.py +++ b/econml/policy/_drlearner.py @@ -4,10 +4,9 @@ from warnings import warn import numpy as np from sklearn.base import clone -from ..utilities import check_inputs, filter_none_kwargs, check_input_arrays +from ..utilities import filter_none_kwargs, check_input_arrays from ..dr import DRLearner from ..dr._drlearner import _ModelFinal -from .._tree_exporter import _SingleTreeExporterMixin from ._base import PolicyLearner from . import PolicyTree, PolicyForest diff --git a/econml/policy/_forest/_forest.py b/econml/policy/_forest/_forest.py index 474bc71a4..92c4bdfb5 100644 --- a/econml/policy/_forest/_forest.py +++ b/econml/policy/_forest/_forest.py @@ -9,18 +9,16 @@ # Copyright (c) 2007-2020 The scikit-learn developers. # All rights reserved. -import numbers -from warnings import catch_warnings, simplefilter, warn -from abc import ABCMeta, abstractmethod +from warnings import warn +from abc import ABCMeta import numpy as np import threading from ..._ensemble import (BaseEnsemble, _partition_estimators, _get_n_samples_subsample, _accumulate_prediction) -from ...utilities import check_inputs, cross_product from ...tree._tree import DTYPE, DOUBLE from ._tree import PolicyTree from joblib import Parallel, delayed from scipy.sparse import hstack as sparse_hstack -from sklearn.utils import check_random_state, compute_sample_weight +from sklearn.utils import check_random_state from sklearn.utils.validation import _check_sample_weight, check_is_fitted from sklearn.utils import check_X_y diff --git a/econml/policy/_forest/_tree.py b/econml/policy/_forest/_tree.py index 3b02c374a..46c6f4fa9 100644 --- a/econml/policy/_forest/_tree.py +++ b/econml/policy/_forest/_tree.py @@ -10,23 +10,12 @@ # All rights reserved. import numpy as np -import numbers -from math import ceil -from ...tree import Tree -from ...tree._criterion import Criterion -from ...tree._splitter import Splitter, BestSplitter -from ...tree import DepthFirstTreeBuilder -from ...tree import _tree from ..._tree_exporter import _SingleTreeExporterMixin, _PolicyTreeDOTExporter, _PolicyTreeMPLExporter from ._criterion import LinearPolicyCriterion -from . import _criterion from ...tree import BaseTree -from sklearn.model_selection import train_test_split -from sklearn.utils import check_array, check_X_y +from sklearn.utils import check_X_y from sklearn.utils import check_random_state -from sklearn.utils.validation import _check_sample_weight from sklearn.utils.validation import check_is_fitted -import copy # ============================================================================= # Types and constants diff --git a/econml/sklearn_extensions/linear_model.py b/econml/sklearn_extensions/linear_model.py index cb999e14c..9bc5529f7 100644 --- a/econml/sklearn_extensions/linear_model.py +++ b/econml/sklearn_extensions/linear_model.py @@ -25,7 +25,6 @@ from sklearn.linear_model import LinearRegression, LassoCV, MultiTaskLassoCV, Lasso, MultiTaskLasso from sklearn.linear_model._base import _preprocess_data from sklearn.metrics import r2_score -from sklearn.model_selection import KFold, StratifiedKFold # TODO: consider working around relying on sklearn implementation details from sklearn.model_selection._split import _CVIterableWrapper from sklearn.multioutput import MultiOutputRegressor diff --git a/econml/sklearn_extensions/model_selection.py b/econml/sklearn_extensions/model_selection.py index 4d165a913..fc0c7a755 100644 --- a/econml/sklearn_extensions/model_selection.py +++ b/econml/sklearn_extensions/model_selection.py @@ -10,18 +10,16 @@ import abc import numpy as np -from collections.abc import Iterable import scipy.sparse as sp import sklearn from joblib import Parallel, delayed from sklearn.base import BaseEstimator, clone, is_classifier from sklearn.ensemble import (GradientBoostingClassifier, GradientBoostingRegressor, RandomForestClassifier, RandomForestRegressor) -from sklearn.exceptions import FitFailedWarning from sklearn.linear_model import (ElasticNet, ElasticNetCV, Lasso, LassoCV, MultiTaskElasticNet, MultiTaskElasticNetCV, MultiTaskLasso, MultiTaskLassoCV, Ridge, RidgeCV, RidgeClassifier, RidgeClassifierCV, LogisticRegression, LogisticRegressionCV) -from sklearn.model_selection import (BaseCrossValidator, GridSearchCV, GroupKFold, KFold, +from sklearn.model_selection import (GridSearchCV, GroupKFold, KFold, RandomizedSearchCV, StratifiedKFold, check_cv) # TODO: conisder working around relying on sklearn implementation details @@ -29,9 +27,8 @@ _fit_and_predict) from sklearn.neural_network import MLPClassifier, MLPRegressor from sklearn.pipeline import Pipeline, make_pipeline -from sklearn.preprocessing import LabelEncoder, PolynomialFeatures, StandardScaler +from sklearn.preprocessing import LabelEncoder, PolynomialFeatures from sklearn.utils import check_random_state, indexable -from sklearn.utils.multiclass import type_of_target from sklearn.utils.validation import _num_samples from .linear_model import WeightedLassoCVWrapper, WeightedLassoWrapper diff --git a/econml/solutions/causal_analysis/_causal_analysis.py b/econml/solutions/causal_analysis/_causal_analysis.py index f181c625e..6e17fbb98 100644 --- a/econml/solutions/causal_analysis/_causal_analysis.py +++ b/econml/solutions/causal_analysis/_causal_analysis.py @@ -8,16 +8,15 @@ import joblib import lightgbm as lgb -from numba.core.utils import erase_traceback import numpy as np from numpy.lib.function_base import iterable import pandas as pd from sklearn.base import BaseEstimator, TransformerMixin from sklearn.compose import ColumnTransformer -from sklearn.ensemble import GradientBoostingClassifier, RandomForestClassifier, RandomForestRegressor +from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor from sklearn.linear_model import Lasso, LassoCV, LogisticRegression, LogisticRegressionCV -from sklearn.pipeline import make_pipeline, Pipeline -from sklearn.preprocessing import OneHotEncoder, PolynomialFeatures, StandardScaler +from sklearn.pipeline import Pipeline +from sklearn.preprocessing import StandardScaler from sklearn.tree import _tree from sklearn.utils.validation import column_or_1d from ...cate_interpreter import SingleTreeCateInterpreter, SingleTreePolicyInterpreter @@ -25,7 +24,7 @@ from ...inference import NormalInferenceResults from ...sklearn_extensions.linear_model import WeightedLasso from ...sklearn_extensions.model_selection import GridSearchCVList -from ...utilities import _RegressionWrapper, get_feature_names_or_default, inverse_onehot, one_hot_encoder +from ...utilities import _RegressionWrapper, get_feature_names_or_default, one_hot_encoder # TODO: this utility is documented but internal; reimplement? from sklearn.utils import _safe_indexing diff --git a/econml/tests/test_ate_inference.py b/econml/tests/test_ate_inference.py index 7748ea481..f0d9b523a 100644 --- a/econml/tests/test_ate_inference.py +++ b/econml/tests/test_ate_inference.py @@ -4,7 +4,7 @@ import numpy as np import unittest from sklearn.preprocessing import PolynomialFeatures -from sklearn.linear_model import LinearRegression, LogisticRegression +from sklearn.linear_model import LinearRegression from econml.dml import LinearDML from econml.inference import BootstrapInference diff --git a/econml/tests/test_automated_ml.py b/econml/tests/test_automated_ml.py index a5d7bb6d6..27524d0cc 100644 --- a/econml/tests/test_automated_ml.py +++ b/econml/tests/test_automated_ml.py @@ -3,27 +3,11 @@ import unittest import pytest -from sklearn.linear_model import LinearRegression, Lasso, \ - LogisticRegression -from sklearn.pipeline import Pipeline -from sklearn.preprocessing import OneHotEncoder, FunctionTransformer, \ - PolynomialFeatures -from sklearn.model_selection import KFold from econml.dml import DML, LinearDML, SparseLinearDML, KernelDML, NonParamDML, CausalForestDML from econml.metalearners import SLearner, TLearner, XLearner, DomainAdaptationLearner from econml.dr import DRLearner -import numpy as np -from econml.utilities import shape, hstack, vstack, reshape, \ - cross_product -from econml.inference import BootstrapInference -from contextlib import ExitStack -from sklearn.ensemble import RandomForestRegressor, \ - GradientBoostingRegressor, GradientBoostingClassifier -import itertools -from econml.sklearn_extensions.linear_model import WeightedLasso -from econml.tests.test_statsmodels import _summarize +from sklearn.ensemble import GradientBoostingClassifier import copy -import logging from econml.data.dgps import ihdp_surface_B import os diff --git a/econml/tests/test_causal_analysis.py b/econml/tests/test_causal_analysis.py index 193f8383b..0cf3a1802 100644 --- a/econml/tests/test_causal_analysis.py +++ b/econml/tests/test_causal_analysis.py @@ -6,7 +6,6 @@ from contextlib import ExitStack import itertools import numpy as np -from numpy.core.fromnumeric import squeeze import pandas as pd import pytest diff --git a/econml/tests/test_deepiv.py b/econml/tests/test_deepiv.py index 2f755fbc3..ce8d4fcb2 100644 --- a/econml/tests/test_deepiv.py +++ b/econml/tests/test_deepiv.py @@ -19,8 +19,7 @@ from econml.iv.nnet._deepiv import _zero_grad from econml.iv.nnet import DeepIV -from econml.iv.nnet._deepiv import mog_model, mog_loss_model, mog_sample_model, response_loss_model -from econml.utilities import reshape +from econml.iv.nnet._deepiv import mog_model, mog_loss_model, mog_sample_model @pytest.mark.skipif(not keras_installed, reason="Keras not installed") diff --git a/econml/tests/test_discrete_outcome.py b/econml/tests/test_discrete_outcome.py index 91ddd6358..23efa54ba 100644 --- a/econml/tests/test_discrete_outcome.py +++ b/econml/tests/test_discrete_outcome.py @@ -4,20 +4,14 @@ import unittest import numpy as np import pandas as pd -from sklearn.preprocessing import PolynomialFeatures from sklearn.linear_model import LinearRegression, LogisticRegression -from sklearn.ensemble import RandomForestRegressor -from joblib import Parallel, delayed -from econml._ortho_learner import _OrthoLearner -from econml.dml import LinearDML, SparseLinearDML, KernelDML, CausalForestDML, NonParamDML +from econml.dml import LinearDML, SparseLinearDML, CausalForestDML from econml.dr import LinearDRLearner, ForestDRLearner -from econml.iv.dml import OrthoIV, DMLIV, NonParamDMLIV +from econml.iv.dml import OrthoIV, DMLIV from econml.iv.dr import DRIV, LinearDRIV, SparseLinearDRIV, ForestDRIV, IntentToTreatDRIV, LinearIntentToTreatDRIV -from econml.orf import DMLOrthoForest from econml.utilities import filter_none_kwargs -from copy import deepcopy class TestDiscreteOutcome(unittest.TestCase): diff --git a/econml/tests/test_dml.py b/econml/tests/test_dml.py index 452a71307..9114e46d7 100644 --- a/econml/tests/test_dml.py +++ b/econml/tests/test_dml.py @@ -7,20 +7,18 @@ from sklearn.linear_model import LinearRegression, Lasso, LassoCV, LogisticRegression from sklearn.pipeline import Pipeline from sklearn.preprocessing import OneHotEncoder, FunctionTransformer, PolynomialFeatures -from sklearn.model_selection import KFold, GroupKFold, check_cv +from sklearn.model_selection import KFold from econml.dml import DML, LinearDML, SparseLinearDML, KernelDML, CausalForestDML from econml.dml import NonParamDML import numpy as np import pandas as pd from econml.utilities import shape, hstack, vstack, reshape, cross_product -from econml.inference import BootstrapInference, EmpiricalInferenceResults, NormalInferenceResults +from econml.inference import BootstrapInference from contextlib import ExitStack from sklearn.ensemble import GradientBoostingRegressor, GradientBoostingClassifier import itertools -from econml.sklearn_extensions.linear_model import WeightedLasso, StatsModelsRLM, StatsModelsLinearRegression +from econml.sklearn_extensions.linear_model import WeightedLasso, StatsModelsRLM from econml.tests.test_statsmodels import _summarize -from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier -from sklearn.multioutput import MultiOutputRegressor from econml.grf import MultiOutputGRF from econml.tests.utilities import (GroupingModel, NestedModel) diff --git a/econml/tests/test_dmliv.py b/econml/tests/test_dmliv.py index bb6c36252..1fd491e22 100644 --- a/econml/tests/test_dmliv.py +++ b/econml/tests/test_dmliv.py @@ -6,14 +6,12 @@ import numpy as np import pytest -from scipy import special from sklearn.ensemble import RandomForestRegressor from sklearn.linear_model import LinearRegression, LogisticRegression, LogisticRegressionCV from sklearn.preprocessing import PolynomialFeatures from econml.iv.dml import OrthoIV, DMLIV, NonParamDMLIV -from econml.iv.dr._dr import _DummyCATE -from econml.sklearn_extensions.linear_model import StatsModelsLinearRegression, WeightedLassoCVWrapper +from econml.sklearn_extensions.linear_model import WeightedLassoCVWrapper from econml.utilities import shape from econml.tests.utilities import GroupingModel diff --git a/econml/tests/test_dominicks.py b/econml/tests/test_dominicks.py index 126084e84..1688b0ee1 100644 --- a/econml/tests/test_dominicks.py +++ b/econml/tests/test_dominicks.py @@ -9,7 +9,6 @@ from econml.dml import LinearDML from sklearn.base import TransformerMixin from sklearn.ensemble import RandomForestRegressor -from sklearn.linear_model import RidgeCV, LinearRegression import pytest @@ -29,8 +28,6 @@ def test_dominicks(): featnames = ["week", "feat"] + list(oj_data.columns[6:]) # Preprocess data - import datetime - import numpy as np # Convert 'week' to a date # week_zero = datetime.datetime.strptime("09/07/89", "%m/%d/%y") diff --git a/econml/tests/test_dowhy.py b/econml/tests/test_dowhy.py index 4d7c37ddf..c21326cda 100644 --- a/econml/tests/test_dowhy.py +++ b/econml/tests/test_dowhy.py @@ -10,8 +10,7 @@ from econml.metalearners import XLearner from econml.iv.dml import OrthoIV, DMLIV from econml.iv.dr import LinearDRIV -from econml.iv.dr._dr import _DummyCATE -from sklearn.linear_model import LinearRegression, LogisticRegression, Lasso +from sklearn.linear_model import LinearRegression, LogisticRegression from econml.sklearn_extensions.linear_model import StatsModelsLinearRegression diff --git a/econml/tests/test_driv.py b/econml/tests/test_driv.py index 4f9ee6696..734cb4694 100644 --- a/econml/tests/test_driv.py +++ b/econml/tests/test_driv.py @@ -2,7 +2,6 @@ # Licensed under the MIT License. from econml.iv.dr import (DRIV, LinearDRIV, SparseLinearDRIV, ForestDRIV, IntentToTreatDRIV, LinearIntentToTreatDRIV,) -from econml.iv.dr._dr import _DummyCATE from econml.sklearn_extensions.linear_model import StatsModelsLinearRegression from econml.utilities import shape from econml.tests.utilities import GroupingModel diff --git a/econml/tests/test_drlearner.py b/econml/tests/test_drlearner.py index b1a859429..9bd7ee6c4 100644 --- a/econml/tests/test_drlearner.py +++ b/econml/tests/test_drlearner.py @@ -6,21 +6,19 @@ import unittest import numpy as np -from numpy.random import normal, multivariate_normal, binomial import pytest import scipy.special -from sklearn.base import TransformerMixin from sklearn.ensemble import GradientBoostingClassifier, GradientBoostingRegressor, RandomForestRegressor from sklearn.exceptions import DataConversionWarning from sklearn.linear_model import LinearRegression, Lasso, LassoCV, LogisticRegression, LogisticRegressionCV -from sklearn.model_selection import KFold, StratifiedGroupKFold, check_cv +from sklearn.model_selection import KFold, StratifiedGroupKFold from sklearn.pipeline import Pipeline -from sklearn.preprocessing import OneHotEncoder, FunctionTransformer, PolynomialFeatures +from sklearn.preprocessing import FunctionTransformer, PolynomialFeatures from econml.dr import DRLearner, LinearDRLearner, SparseLinearDRLearner, ForestDRLearner from econml.inference import BootstrapInference, StatsModelsInferenceDiscrete -from econml.utilities import get_feature_names_or_default, shape, hstack, vstack, reshape, cross_product +from econml.utilities import get_feature_names_or_default, shape from econml.sklearn_extensions.linear_model import StatsModelsLinearRegression from econml.tests.utilities import (GroupingModel, NestedModel) diff --git a/econml/tests/test_dynamic_dml.py b/econml/tests/test_dynamic_dml.py index 118f51563..44b017bb1 100644 --- a/econml/tests/test_dynamic_dml.py +++ b/econml/tests/test_dynamic_dml.py @@ -5,13 +5,13 @@ import pickle import numpy as np from contextlib import ExitStack -from sklearn.preprocessing import OneHotEncoder, FunctionTransformer, PolynomialFeatures -from sklearn.linear_model import (LinearRegression, LassoCV, Lasso, MultiTaskLasso, - MultiTaskLassoCV, LogisticRegression) +from sklearn.preprocessing import PolynomialFeatures +from sklearn.linear_model import (LassoCV, Lasso, MultiTaskLasso, + LogisticRegression) from econml.panel.dml import DynamicDML from econml.panel.dml._dml import _get_groups_period_filter -from econml.inference import BootstrapInference, EmpiricalInferenceResults, NormalInferenceResults -from econml.utilities import shape, hstack, vstack, reshape, cross_product +from econml.inference import BootstrapInference +from econml.utilities import shape from econml.tests.dgp import DynamicPanelDGP diff --git a/econml/tests/test_federated_learning.py b/econml/tests/test_federated_learning.py index 6ed1c5185..dc2bb8c05 100644 --- a/econml/tests/test_federated_learning.py +++ b/econml/tests/test_federated_learning.py @@ -4,7 +4,6 @@ import numpy as np import unittest -from sklearn import clone from econml.dml import LinearDML from econml.dr import LinearDRLearner from econml.inference import StatsModelsInference, StatsModelsInferenceDiscrete diff --git a/econml/tests/test_grf_cython.py b/econml/tests/test_grf_cython.py index f3a879dae..ced0a6e02 100644 --- a/econml/tests/test_grf_cython.py +++ b/econml/tests/test_grf_cython.py @@ -2,13 +2,8 @@ # Licensed under the MIT License. import unittest -import logging -import time -import random import numpy as np -import sparse as sp -import pytest -from econml.tree import DepthFirstTreeBuilder, BestSplitter, Tree, MSE +from econml.tree import DepthFirstTreeBuilder, BestSplitter, Tree from econml.grf import LinearMomentGRFCriterion, LinearMomentGRFCriterionMSE from econml.grf._utils import matinv, lstsq, pinv, fast_max_eigv, fast_min_eigv from econml.utilities import cross_product diff --git a/econml/tests/test_grf_python.py b/econml/tests/test_grf_python.py index 147c5cabe..2f4a5a773 100644 --- a/econml/tests/test_grf_python.py +++ b/econml/tests/test_grf_python.py @@ -2,12 +2,8 @@ # Licensed under the MIT License. import unittest -import logging -import time -import random import numpy as np import pandas as pd -import pytest import joblib from econml.grf import RegressionForest, CausalForest, CausalIVForest, MultiOutputGRF from econml.utilities import cross_product diff --git a/econml/tests/test_integration.py b/econml/tests/test_integration.py index 4c13f6b20..8b82f5563 100644 --- a/econml/tests/test_integration.py +++ b/econml/tests/test_integration.py @@ -20,7 +20,7 @@ from econml.metalearners import XLearner, SLearner, TLearner from sklearn.compose import ColumnTransformer from sklearn.ensemble import GradientBoostingRegressor, GradientBoostingClassifier -from sklearn.linear_model import LinearRegression, MultiTaskLasso, LassoCV +from sklearn.linear_model import MultiTaskLasso, LassoCV from sklearn.preprocessing import PolynomialFeatures, FunctionTransformer from econml.iv.dr import LinearIntentToTreatDRIV from econml.iv.nnet import DeepIV diff --git a/econml/tests/test_linear_model.py b/econml/tests/test_linear_model.py index 336e8809f..bf28a5a5b 100644 --- a/econml/tests/test_linear_model.py +++ b/econml/tests/test_linear_model.py @@ -4,15 +4,12 @@ """Tests for linear_model extensions.""" import numpy as np -import pytest import unittest -import warnings from econml.sklearn_extensions.linear_model import (WeightedLasso, WeightedLassoCV, WeightedMultiTaskLassoCV, WeightedLassoCVWrapper, DebiasedLasso, MultiOutputDebiasedLasso, SelectiveRegularization) from econml.sklearn_extensions.model_selection import WeightedKFold from sklearn.linear_model import Lasso, LassoCV, LinearRegression, MultiTaskLassoCV, Ridge -from sklearn.model_selection import KFold from sklearn.base import clone diff --git a/econml/tests/test_metalearners.py b/econml/tests/test_metalearners.py index 2a306eb19..beb60af4a 100644 --- a/econml/tests/test_metalearners.py +++ b/econml/tests/test_metalearners.py @@ -3,7 +3,6 @@ import numpy as np import unittest -from numpy.random import normal, multivariate_normal, binomial from sklearn.exceptions import DataConversionWarning from sklearn.linear_model import LinearRegression from sklearn.pipeline import Pipeline diff --git a/econml/tests/test_missing_values.py b/econml/tests/test_missing_values.py index 265a10069..0924f5811 100644 --- a/econml/tests/test_missing_values.py +++ b/econml/tests/test_missing_values.py @@ -1,6 +1,5 @@ # Copyright (c) PyWhy contributors. All rights reserved. # Licensed under the MIT License. -import pytest import unittest import numpy as np from sklearn.linear_model import LinearRegression, LogisticRegression, Lasso diff --git a/econml/tests/test_orf.py b/econml/tests/test_orf.py index 2be8fbaef..262f6f0a7 100644 --- a/econml/tests/test_orf.py +++ b/econml/tests/test_orf.py @@ -3,13 +3,10 @@ import numpy as np import unittest -import pytest import warnings -from numpy.random import binomial, choice, normal, uniform -from sklearn.exceptions import DataConversionWarning -from sklearn.linear_model import LinearRegression, Lasso, LassoCV, LogisticRegression, LogisticRegressionCV +from numpy.random import choice, normal, uniform +from sklearn.linear_model import Lasso, LogisticRegression, LogisticRegressionCV from sklearn.multioutput import MultiOutputRegressor -from sklearn.pipeline import Pipeline from econml.orf import DMLOrthoForest, DROrthoForest from econml.sklearn_extensions.linear_model import WeightedLassoCVWrapper @@ -165,7 +162,6 @@ def test_multiple_treatments(self): self._test_ci(est, expected_te, tol=2.0, treatment_type='multi') def test_effect_shape(self): - import scipy.special np.random.seed(123) n = 40 # number of raw samples d = 4 # number of binary features + 1 diff --git a/econml/tests/test_ortho_learner.py b/econml/tests/test_ortho_learner.py index 686b6cad1..bb66bd019 100644 --- a/econml/tests/test_ortho_learner.py +++ b/econml/tests/test_ortho_learner.py @@ -3,12 +3,10 @@ from sklearn.datasets import make_regression from econml._ortho_learner import _OrthoLearner, _crossfit -from sklearn.preprocessing import PolynomialFeatures -from sklearn.linear_model import LinearRegression, LassoCV, Lasso +from sklearn.linear_model import LinearRegression, Lasso from sklearn.model_selection import KFold import numpy as np import unittest -import joblib import pytest try: diff --git a/econml/tests/test_policy_forest.py b/econml/tests/test_policy_forest.py index bf6a610aa..a329ffff3 100644 --- a/econml/tests/test_policy_forest.py +++ b/econml/tests/test_policy_forest.py @@ -2,19 +2,13 @@ # Licensed under the MIT License. import unittest -import logging -import time -import random import numpy as np import pandas as pd import pytest import joblib from econml.policy import PolicyTree, PolicyForest from econml.policy import DRPolicyTree, DRPolicyForest -from econml.utilities import cross_product -from copy import deepcopy from sklearn.utils import check_random_state -import scipy.stats from sklearn.preprocessing import PolynomialFeatures from sklearn.dummy import DummyClassifier, DummyRegressor from sklearn.model_selection import GroupKFold diff --git a/econml/tests/test_random_state.py b/econml/tests/test_random_state.py index 8fe45ebce..2ae5f3912 100644 --- a/econml/tests/test_random_state.py +++ b/econml/tests/test_random_state.py @@ -1,25 +1,13 @@ import unittest -import pytest -import pickle -from sklearn.linear_model import LinearRegression, Lasso, LassoCV, LogisticRegression -from sklearn.pipeline import Pipeline -from sklearn.preprocessing import OneHotEncoder, FunctionTransformer, PolynomialFeatures -from sklearn.model_selection import KFold, GroupKFold -from econml.dml import DML, LinearDML, SparseLinearDML, KernelDML +from sklearn.linear_model import LinearRegression +from econml.dml import LinearDML, SparseLinearDML, KernelDML from econml.dml import NonParamDML, CausalForestDML from econml.dr import DRLearner, SparseLinearDRLearner, LinearDRLearner, ForestDRLearner -from econml.iv.dml import OrthoIV, DMLIV, NonParamDMLIV +from econml.iv.dml import OrthoIV, NonParamDMLIV from econml.iv.dr import (LinearDRIV, IntentToTreatDRIV, LinearIntentToTreatDRIV) import numpy as np -from econml.utilities import shape, hstack, vstack, reshape, cross_product -from econml.inference import BootstrapInference -from contextlib import ExitStack -from sklearn.ensemble import GradientBoostingRegressor, GradientBoostingClassifier -import itertools -from econml.sklearn_extensions.linear_model import WeightedLasso, StatsModelsRLM, StatsModelsLinearRegression -from econml.tests.test_statsmodels import _summarize +from econml.sklearn_extensions.linear_model import StatsModelsLinearRegression from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier -from econml.iv.dr._dr import _DummyCATE class TestRandomState(unittest.TestCase): diff --git a/econml/tests/test_refit.py b/econml/tests/test_refit.py index 6a8217a30..4669d8e79 100644 --- a/econml/tests/test_refit.py +++ b/econml/tests/test_refit.py @@ -5,10 +5,10 @@ from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier from sklearn.preprocessing import PolynomialFeatures from sklearn.pipeline import Pipeline -from econml.dml import (DML, LinearDML, SparseLinearDML, KernelDML, NonParamDML, CausalForestDML) -from econml.dr import (DRLearner, LinearDRLearner, SparseLinearDRLearner, ForestDRLearner) +from econml.dml import (DML, LinearDML, SparseLinearDML, NonParamDML) +from econml.dr import (LinearDRLearner, SparseLinearDRLearner) from econml.iv.dml import (OrthoIV, DMLIV, NonParamDMLIV) -from econml.iv.dr import (LinearDRIV, IntentToTreatDRIV, LinearIntentToTreatDRIV) +from econml.iv.dr import (IntentToTreatDRIV, LinearIntentToTreatDRIV) from econml.sklearn_extensions.linear_model import (DebiasedLasso, WeightedLasso, StatsModelsRLM, StatsModelsLinearRegression) from econml.inference import NormalInferenceResults, BootstrapInference diff --git a/econml/tests/test_shap.py b/econml/tests/test_shap.py index 0761d565b..4c6983dc7 100644 --- a/econml/tests/test_shap.py +++ b/econml/tests/test_shap.py @@ -9,7 +9,7 @@ from econml.dr import DRLearner, ForestDRLearner from econml.metalearners import TLearner, SLearner, XLearner, DomainAdaptationLearner from sklearn.linear_model import LinearRegression, LogisticRegression, Lasso -from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier +from sklearn.ensemble import RandomForestRegressor from sklearn.preprocessing import PolynomialFeatures diff --git a/econml/tests/test_statsmodels.py b/econml/tests/test_statsmodels.py index ba9b4dfd3..cf4dfc2e1 100644 --- a/econml/tests/test_statsmodels.py +++ b/econml/tests/test_statsmodels.py @@ -7,7 +7,6 @@ import pytest import scipy.special -from sklearn.base import clone from sklearn.ensemble import RandomForestRegressor from sklearn.linear_model import LinearRegression, LogisticRegression from sklearn.preprocessing import PolynomialFeatures @@ -23,7 +22,7 @@ from econml.sklearn_extensions.linear_model import WeightedLasso, StatsModelsLinearRegression from econml.sklearn_extensions.linear_model import StatsModelsLinearRegression as OLS from econml.sklearn_extensions.linear_model import StatsModels2SLS -from econml.utilities import (ndim, transpose, shape, reshape, hstack, WeightedModelWrapper) +from econml.utilities import (ndim, transpose, shape, reshape) class StatsModelsOLS: diff --git a/econml/tests/test_treatment_featurization.py b/econml/tests/test_treatment_featurization.py index 1127e3284..fdf0bd75a 100644 --- a/econml/tests/test_treatment_featurization.py +++ b/econml/tests/test_treatment_featurization.py @@ -4,19 +4,17 @@ import unittest import numpy as np from sklearn.preprocessing import PolynomialFeatures -from sklearn.linear_model import LassoCV, LinearRegression, LogisticRegression +from sklearn.linear_model import LinearRegression from sklearn.ensemble import RandomForestRegressor -from joblib import Parallel, delayed from econml._ortho_learner import _OrthoLearner from econml.dml import LinearDML, SparseLinearDML, KernelDML, CausalForestDML, NonParamDML from econml.iv.dml import OrthoIV, DMLIV, NonParamDMLIV from econml.iv.dr import DRIV, LinearDRIV, SparseLinearDRIV, ForestDRIV from econml.orf import DMLOrthoForest -from sklearn.preprocessing import OneHotEncoder, FunctionTransformer +from sklearn.preprocessing import FunctionTransformer from econml.sklearn_extensions.linear_model import StatsModelsLinearRegression, WeightedLassoCVWrapper -from econml.utilities import jacify_featurizer from econml.iv.sieve import DPolynomialFeatures from copy import deepcopy diff --git a/econml/tests/test_two_stage_least_squares.py b/econml/tests/test_two_stage_least_squares.py index befc18f28..3d2cf9e77 100644 --- a/econml/tests/test_two_stage_least_squares.py +++ b/econml/tests/test_two_stage_least_squares.py @@ -5,7 +5,6 @@ import unittest import numpy as np -import warnings import pytest from econml.utilities import shape, reshape diff --git a/econml/tests/test_utilities.py b/econml/tests/test_utilities.py index 676f498b7..467d74573 100644 --- a/econml/tests/test_utilities.py +++ b/econml/tests/test_utilities.py @@ -2,7 +2,6 @@ # Licensed under the MIT License. import unittest -import logging import time import random import warnings diff --git a/econml/tree/_tree_classes.py b/econml/tree/_tree_classes.py index ffc15861e..f65db4722 100644 --- a/econml/tree/_tree_classes.py +++ b/econml/tree/_tree_classes.py @@ -1,4 +1,3 @@ -import abc import numbers from math import ceil import numpy as np @@ -9,8 +8,7 @@ from ._criterion import Criterion from . import _tree from ..utilities import deprecated -from sklearn.model_selection import train_test_split -from sklearn.utils import check_array, check_X_y +from sklearn.utils import check_array from sklearn.utils import check_random_state from sklearn.utils.validation import _check_sample_weight @@ -238,7 +236,7 @@ def fit(self, X, y, n_y, n_outputs, n_relevant_outputs, sample_weight=None, chec criterion_val = criterion else: valid_criteria = self._get_valid_criteria() - if not (self.criterion in valid_criteria): + if self.criterion not in valid_criteria: raise ValueError("Input criterion is not a valid criterion") criterion = valid_criteria[self.criterion]( self.n_outputs_, self.n_relevant_outputs_, self.n_features_in_, self.n_y_, n_samples, max_train, diff --git a/econml/utilities.py b/econml/utilities.py index df51625f4..618ac8f38 100644 --- a/econml/utilities.py +++ b/econml/utilities.py @@ -8,27 +8,18 @@ import scipy.sparse import sklearn import sparse as sp -import itertools import inspect -import types -from operator import getitem from collections import defaultdict, Counter from sklearn import clone -from sklearn.base import TransformerMixin, BaseEstimator -from sklearn.linear_model import LassoCV, MultiTaskLassoCV, Lasso, MultiTaskLasso +from sklearn.base import TransformerMixin from functools import reduce, wraps from sklearn.utils import check_array, check_X_y from sklearn.utils.validation import assert_all_finite from sklearn.preprocessing import OneHotEncoder, PolynomialFeatures, LabelEncoder import warnings from warnings import warn -from collections.abc import Iterable -from sklearn.utils.multiclass import type_of_target -import numbers from statsmodels.iolib.table import SimpleTable from statsmodels.iolib.summary import summary_return -from statsmodels.compat.python import lmap -import copy from inspect import signature MAX_RAND_SEED = np.iinfo(np.int32).max diff --git a/notebooks/AutomatedML/Automated Machine Learning For EconML.ipynb b/notebooks/AutomatedML/Automated Machine Learning For EconML.ipynb index d7b14dc77..03c09a114 100644 --- a/notebooks/AutomatedML/Automated Machine Learning For EconML.ipynb +++ b/notebooks/AutomatedML/Automated Machine Learning For EconML.ipynb @@ -48,33 +48,19 @@ ], "source": [ "import logging\n", - "import sys\n", "# Main imports\n", - "from econml.dml import DML, LinearDML,SparseLinearDML,CausalForestDML, NonParamDML, KernelDML\n", + "from econml.dml import DML, LinearDML,CausalForestDML, NonParamDML\n", "from matplotlib import pyplot as plt\n", - "import pandas as pd\n", - "import os\n", - "import copy\n", "\n", - "import azureml.core\n", - "from azureml.core.experiment import Experiment\n", - "from azureml.core.workspace import Workspace\n", - "from azureml.automl.core.featurization import FeaturizationConfig\n", - "from azureml.core.dataset import Dataset\n", - "from azureml.explain.model._internal.explanation_client import ExplanationClient\n", "\n", "#Import scikit-learn model AutoML wrapper\n", "from econml.automated_ml import addAutomatedML,setAutomatedMLWorkspace, EconAutoMLConfig\n", "# Imports for linear double machine learning evaluation\n", - "from econml.dml import LinearDML\n", - "from matplotlib import pyplot as plt\n", - "import pandas as pd\n", - "import os\n", "# Helper imports\n", "import math\n", "import numpy as np\n", "from itertools import product\n", - "from sklearn.linear_model import Lasso, LassoCV, LogisticRegression, LogisticRegressionCV,LinearRegression,MultiTaskElasticNet,MultiTaskElasticNetCV\n", + "from sklearn.linear_model import Lasso\n", "from sklearn.ensemble import RandomForestRegressor,RandomForestClassifier\n", "from sklearn.preprocessing import PolynomialFeatures\n", "from sklearn.model_selection import train_test_split\n", @@ -178,12 +164,8 @@ "outputs": [], "source": [ "from sklearn.model_selection import GridSearchCV\n", - "from sklearn.pipeline import Pipeline\n", - "from sklearn.preprocessing import PolynomialFeatures\n", - "from sklearn.linear_model import LassoCV, LogisticRegressionCV\n", - "from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor\n", - "from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier\n", - "from econml.sklearn_extensions.linear_model import WeightedLassoCV\n", + "from sklearn.ensemble import GradientBoostingRegressor\n", + "from sklearn.ensemble import GradientBoostingClassifier\n", "\n", "class GridSearchCVList:\n", "\n", @@ -983,7 +965,7 @@ "metadata": {}, "outputs": [], "source": [ - "from econml.metalearners import TLearner, SLearner, XLearner, DomainAdaptationLearner\n", + "from econml.metalearners import TLearner, SLearner, DomainAdaptationLearner\n", "from econml.data.dgps import ihdp_surface_B\n", "Y, T, X, expected_te = ihdp_surface_B()" ] diff --git a/notebooks/Causal Forest and Orthogonal Random Forest Examples.ipynb b/notebooks/Causal Forest and Orthogonal Random Forest Examples.ipynb index 26e37fd82..8f261e707 100644 --- a/notebooks/Causal Forest and Orthogonal Random Forest Examples.ipynb +++ b/notebooks/Causal Forest and Orthogonal Random Forest Examples.ipynb @@ -46,9 +46,7 @@ "execution_count": 1, "metadata": {}, "outputs": [], - "source": [ - "import econml" - ] + "source": [] }, { "cell_type": "code", @@ -59,12 +57,12 @@ "# Main imports\n", "from econml.orf import DMLOrthoForest, DROrthoForest\n", "from econml.dml import CausalForestDML\n", - "from econml.sklearn_extensions.linear_model import WeightedLassoCVWrapper, WeightedLasso, WeightedLassoCV\n", + "from econml.sklearn_extensions.linear_model import WeightedLassoCVWrapper, WeightedLasso\n", "\n", "# Helper imports\n", "import numpy as np\n", "from itertools import product\n", - "from sklearn.linear_model import Lasso, LassoCV, LogisticRegression, LogisticRegressionCV\n", + "from sklearn.linear_model import Lasso, LogisticRegression\n", "import matplotlib.pyplot as plt\n", "\n", "%matplotlib inline" diff --git a/notebooks/Causal Model Selection with the RScorer.ipynb b/notebooks/Causal Model Selection with the RScorer.ipynb index c964252f3..d92d2198e 100644 --- a/notebooks/Causal Model Selection with the RScorer.ipynb +++ b/notebooks/Causal Model Selection with the RScorer.ipynb @@ -37,9 +37,7 @@ "execution_count": 2, "metadata": {}, "outputs": [], - "source": [ - "import econml" - ] + "source": [] }, { "cell_type": "code", @@ -437,7 +435,7 @@ "metadata": {}, "outputs": [], "source": [ - "from econml.data.dgps import ihdp_surface_B, ihdp_surface_A\n", + "from econml.data.dgps import ihdp_surface_B\n", "Y, T, X, expected_te = ihdp_surface_B(random_state=123)" ] }, diff --git a/notebooks/Choosing First Stage Models.ipynb b/notebooks/Choosing First Stage Models.ipynb index 7388e8c34..5087fdfc5 100644 --- a/notebooks/Choosing First Stage Models.ipynb +++ b/notebooks/Choosing First Stage Models.ipynb @@ -48,14 +48,12 @@ "source": [ "# Imports\n", "import numpy as np\n", - "import scipy.special\n", "from econml.dml import LinearDML\n", "from sklearn.linear_model import Lasso, LassoCV\n", "from sklearn.ensemble import GradientBoostingRegressor\n", "from sklearn.model_selection import GridSearchCV\n", "from sklearn.preprocessing import PolynomialFeatures\n", "import matplotlib.pyplot as plt\n", - "import matplotlib\n", "\n", "%matplotlib inline" ] diff --git a/notebooks/CustomerScenarios/Case Study - Customer Segmentation at An Online Media Company - EconML + DoWhy.ipynb b/notebooks/CustomerScenarios/Case Study - Customer Segmentation at An Online Media Company - EconML + DoWhy.ipynb index 90fc84b57..4f9a158d0 100644 --- a/notebooks/CustomerScenarios/Case Study - Customer Segmentation at An Online Media Company - EconML + DoWhy.ipynb +++ b/notebooks/CustomerScenarios/Case Study - Customer Segmentation at An Online Media Company - EconML + DoWhy.ipynb @@ -57,8 +57,6 @@ "# Some imports to get us started\n", "\n", "# Utilities\n", - "import os\n", - "import urllib.request\n", "import numpy as np\n", "import pandas as pd\n", "from networkx.drawing.nx_pydot import to_pydot\n", diff --git a/notebooks/CustomerScenarios/Case Study - Customer Segmentation at An Online Media Company.ipynb b/notebooks/CustomerScenarios/Case Study - Customer Segmentation at An Online Media Company.ipynb index 2e57078aa..709c17277 100644 --- a/notebooks/CustomerScenarios/Case Study - Customer Segmentation at An Online Media Company.ipynb +++ b/notebooks/CustomerScenarios/Case Study - Customer Segmentation at An Online Media Company.ipynb @@ -2,13 +2,14 @@ "cells": [ { "cell_type": "markdown", + "metadata": {}, "source": [ "" - ], - "metadata": {} + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "# Customer Segmentation: Estimate Individualized Responses to Incentives\n", "\n", @@ -24,11 +25,11 @@ "4. [Understand Treatment Effects with EconML](#interpret)\n", "5. [Make Policy Decisions with EconML](#policy)\n", "6. [Conclusions](#conclusion)\n" - ], - "metadata": {} + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "# Background \n", "\n", @@ -37,17 +38,16 @@ "The global online media market is growing fast over the years. Media companies are always interested in attracting more users into the market and encouraging them to buy more songs or become members. In this example, we'll consider a scenario where one experiment a media company is running is to give small discount (10%, 20% or 0) to their current users based on their income level in order to boost the likelihood of their purchase. The goal is to understand the **heterogeneous price elasticity of demand** for people with different income level, learning which users would respond most strongly to a small discount. Furthermore, their end goal is to make sure that despite decreasing the price for some consumers, the demand is raised enough to boost the overall revenue.\n", "\n", "EconML’s `DML` based estimators can be used to take the discount variation in existing data, along with a rich set of user features, to estimate heterogeneous price sensitivities that vary with multiple customer features. Then, the `SingleTreeCateInterpreter` provides a presentation-ready summary of the key features that explain the biggest differences in responsiveness to a discount, and the `SingleTreePolicyInterpreter` recommends a policy on who should receive a discount in order to increase revenue (not only demand), which could help the company to set an optimal price for those users in the future. " - ], - "metadata": {} + ] }, { "cell_type": "code", "execution_count": 1, + "metadata": {}, + "outputs": [], "source": [ "# Some imports to get us started\r\n", "# Utilities\r\n", - "import os\r\n", - "import urllib.request\r\n", "import numpy as np\r\n", "import pandas as pd\r\n", "\r\n", @@ -62,12 +62,11 @@ "import matplotlib.pyplot as plt\r\n", "\r\n", "%matplotlib inline" - ], - "outputs": [], - "metadata": {} + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "# Data \n", "\n", @@ -114,46 +113,26 @@ "Y &= \\gamma(X) \\cdot T + \\beta(X,W)\n", "\\end{align}\n", "\n" - ], - "metadata": {} + ] }, { "cell_type": "code", "execution_count": 2, + "metadata": {}, + "outputs": [], "source": [ "# Import the sample pricing data\n", "file_url = \"https://msalicedatapublic.z5.web.core.windows.net/datasets/Pricing/pricing_sample.csv\"\n", "train_data = pd.read_csv(file_url)" - ], - "outputs": [], - "metadata": {} + ] }, { "cell_type": "code", "execution_count": 3, - "source": [ - "# Data sample\n", - "train_data.head()" - ], + "metadata": {}, "outputs": [ { - "output_type": "execute_result", "data": { - "text/plain": [ - " account_age age avg_hours days_visited friends_count has_membership \\\n", - "0 3 53 1.834234 2 8 1 \n", - "1 5 54 7.171411 7 9 0 \n", - "2 3 33 5.351920 6 9 0 \n", - "3 2 34 6.723551 0 8 0 \n", - "4 4 30 2.448247 5 8 1 \n", - "\n", - " is_US songs_purchased income price demand \n", - "0 1 4.903237 0.960863 1.0 3.917117 \n", - "1 1 3.330161 0.732487 1.0 11.585706 \n", - "2 1 3.036203 1.130937 1.0 24.675960 \n", - "3 1 7.911926 0.929197 1.0 6.361776 \n", - "4 0 7.148967 0.533527 0.8 12.624123 " - ], "text/html": [ "