Skip to content

Commit c933fa7

Browse files
committed
Merge branch 'feature/refactor_imports' into dev
2 parents c1160e4 + 71eebeb commit c933fa7

File tree

5 files changed

+31
-22
lines changed

5 files changed

+31
-22
lines changed

sklift/metrics/metrics.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,11 @@ def perfect_uplift_curve(y_true, treatment):
8787
check_consistent_length(y_true, treatment)
8888
y_true, treatment = np.array(y_true), np.array(treatment)
8989

90-
CR_num = np.sum((y_true == 1) & (treatment == 0)) # Control Responders
91-
TN_num = np.sum((y_true == 0) & (treatment == 1)) # Treated Non-Responders
90+
cr_num = np.sum((y_true == 1) & (treatment == 0)) # Control Responders
91+
tn_num = np.sum((y_true == 0) & (treatment == 1)) # Treated Non-Responders
9292

93-
summand = y_true if CR_num > TN_num else treatment
93+
# express an ideal uplift curve through y_true and treatment
94+
summand = y_true if cr_num > tn_num else treatment
9495
perfect_uplift = 2 * (y_true == treatment) + summand
9596

9697
return uplift_curve(y_true, perfect_uplift, treatment)
@@ -227,6 +228,7 @@ def perfect_qini_curve(y_true, treatment, negative_effect=True):
227228
if not isinstance(negative_effect, bool):
228229
raise TypeError(f'Negative_effects flag should be bool, got: {type(negative_effect)}')
229230

231+
# express an ideal uplift curve through y_true and treatment
230232
if negative_effect:
231233
x_perfect, y_perfect = qini_curve(
232234
y_true, y_true * treatment - y_true * (1 - treatment), treatment

sklift/models/models.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import warnings
2+
23
import numpy as np
34
import pandas as pd
45
from sklearn.base import BaseEstimator
5-
from sklearn.utils.validation import check_consistent_length
66
from sklearn.utils.multiclass import type_of_target
7+
from sklearn.utils.validation import check_consistent_length
78

89

910
class SoloModel(BaseEstimator):
@@ -92,23 +93,23 @@ def fit(self, X, y, treatment, estimator_fit_params=None):
9293
if self.method == 'dummy':
9394
if isinstance(X, np.ndarray):
9495
X_mod = np.column_stack((X, treatment))
95-
elif isinstance(X, pd.core.frame.DataFrame):
96+
elif isinstance(X, pd.DataFrame):
9697
X_mod = X.assign(treatment=treatment)
9798
else:
9899
raise TypeError("Expected numpy.ndarray or pandas.DataFrame in training vector X, got %s" % type(X))
99100

100101
if self.method == 'treatment_interaction':
101102
if isinstance(X, np.ndarray):
102103
X_mod = np.column_stack((X, np.multiply(X, np.array(treatment).reshape(-1, 1)), treatment))
103-
elif isinstance(X, pd.core.frame.DataFrame):
104+
elif isinstance(X, pd.DataFrame):
104105
X_mod = pd.concat([
105106
X,
106107
X.apply(lambda x: x * treatment)
107108
.rename(columns=lambda x: str(x) + '_treatment_interaction')
108-
], axis=1)\
109+
], axis=1) \
109110
.assign(treatment=treatment)
110111
else:
111-
raise TypeError("Expected numpy.ndarray or pandas.DataFrame in training vector X, got %s" % type(X))
112+
raise TypeError("Expected numpy.ndarray or pandas.DataFrame in training vector X, got %s" % type(X))
112113

113114
self._type_of_target = type_of_target(y)
114115

@@ -132,7 +133,7 @@ def predict(self, X):
132133
if isinstance(X, np.ndarray):
133134
X_mod_trmnt = np.column_stack((X, np.ones(X.shape[0])))
134135
X_mod_ctrl = np.column_stack((X, np.zeros(X.shape[0])))
135-
elif isinstance(X, pd.core.frame.DataFrame):
136+
elif isinstance(X, pd.DataFrame):
136137
X_mod_trmnt = X.assign(treatment=np.ones(X.shape[0]))
137138
X_mod_ctrl = X.assign(treatment=np.zeros(X.shape[0]))
138139
else:
@@ -142,18 +143,18 @@ def predict(self, X):
142143
if isinstance(X, np.ndarray):
143144
X_mod_trmnt = np.column_stack((X, np.multiply(X, np.ones((X.shape[0], 1))), np.ones(X.shape[0])))
144145
X_mod_ctrl = np.column_stack((X, np.multiply(X, np.zeros((X.shape[0], 1))), np.zeros(X.shape[0])))
145-
elif isinstance(X, pd.core.frame.DataFrame):
146+
elif isinstance(X, pd.DataFrame):
146147
X_mod_trmnt = pd.concat([
147148
X,
148149
X.apply(lambda x: x * np.ones(X.shape[0]))
149150
.rename(columns=lambda x: str(x) + '_treatment_interaction')
150-
], axis=1)\
151+
], axis=1) \
151152
.assign(treatment=np.ones(X.shape[0]))
152153
X_mod_ctrl = pd.concat([
153154
X,
154155
X.apply(lambda x: x * np.zeros(X.shape[0]))
155156
.rename(columns=lambda x: str(x) + '_treatment_interaction')
156-
], axis=1)\
157+
], axis=1) \
157158
.assign(treatment=np.zeros(X.shape[0]))
158159
else:
159160
raise TypeError("Expected numpy.ndarray or pandas.DataFrame in training vector X, got %s" % type(X))
@@ -208,6 +209,7 @@ class ClassTransformation(BaseEstimator):
208209
.. _ClassTransformation in documentation:
209210
https://scikit-uplift.readthedocs.io/en/latest/api/models.html#class-transformation
210211
"""
212+
211213
def __init__(self, estimator):
212214
self.estimator = estimator
213215
self._type_of_target = None

sklift/tests/conftest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import numpy as np
2-
import pandas as pd
31
import itertools
42
from collections import defaultdict
5-
import pytest
63

4+
import numpy as np
5+
import pandas as pd
6+
import pytest
77

88
n_vals = (100, 1000)
99
k_vals = (1, 5)

sklift/tests/test_models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from sklearn.linear_model import LogisticRegression, LinearRegression
33
from sklearn.pipeline import Pipeline
44
from sklearn.preprocessing import StandardScaler
5+
56
from ..models import (
67
SoloModel,
78
ClassTransformation,

sklift/viz/base.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from ..metrics import (
66
uplift_curve, perfect_uplift_curve, uplift_auc_score,
77
qini_curve, perfect_qini_curve, qini_auc_score,
8-
treatment_balance_curve
8+
treatment_balance_curve, uplift_by_percentile
99
)
1010

1111

@@ -191,12 +191,16 @@ def plot_uplift_by_percentile(y_true, uplift, treatment, strategy='overall', kin
191191
std=True, total=True, bins=bins)
192192

193193
percentiles = df.index[:bins].values.astype(float)
194-
response_rate_trmnt, std_trmnt = df.loc[percentiles, 'response_rate_treatment'].values, \
195-
df.loc[percentiles, 'std_treatment'].values
196-
response_rate_ctrl, std_ctrl = df.loc[percentiles, 'response_rate_control'].values, \
197-
df.loc[percentiles, 'std_control'].values
198-
uplift_score, std_uplift = df.loc[percentiles, 'uplift'].values, \
199-
df.loc[percentiles, 'std_uplift'].values
194+
195+
response_rate_trmnt = df.loc[percentiles, 'response_rate_treatment'].values
196+
std_trmnt = df.loc[percentiles, 'std_treatment'].values
197+
198+
response_rate_ctrl = df.loc[percentiles, 'response_rate_control'].values
199+
std_ctrl = df.loc[percentiles, 'std_control'].values
200+
201+
uplift_score = df.loc[percentiles, 'uplift'].values
202+
std_uplift = df.loc[percentiles, 'std_uplift'].values
203+
200204
uplift_weighted_avg = df.loc['total', 'uplift']
201205

202206
check_consistent_length(percentiles, response_rate_trmnt, response_rate_ctrl, uplift_score,

0 commit comments

Comments
 (0)