Код: Выделить всё
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import RandomForestRegressor
from econml.dml import CausalForestDML
x_data = pd.read_csv('users_data.csv')
Y = x_data['total_cost'].values
X = x_data[['total_view_counts', 'total_view_days', 'daily_view_max_avg_price', 'daily_add_cart_avg_price', 'total_order_count', 'acc_total_cost', 'view_category_days_percentage']].values
T = x_data['is_coupon'].values
X_train, X_test, Y_train, Y_test, T_train, T_test = train_test_split(X, Y, T, test_size=0.2, stratify = T, random_state=42)
cf_model = CausalForestDML(
model_y = RandomForestRegressor(n_estimators=100, max_depth=5),
# since Y is a list of continuous values
model_t = RandomForestClassifier(n_estimators=100, max_depth=5),
# since T is a list of binary values, or discrete values
n_estimators=100,
min_samples_leaf = 20,
discrete_treatment = True,
discrete_outcome = False,
random_state = 42
)
cf_model.fit(Y_train, T_train, X=X_train)
# operands could not be broadcast together with shapes (83301, 1)(0, 1)
te_pred = cf_model.effect(X_test)
>
Подробнее здесь: https://stackoverflow.com/questions/798 ... ift-values