In [ ]:
Copied!
pip install ydf -U
pip install ydf -U
In [1]:
Copied!
import ydf
import pandas as pd
import ydf
import pandas as pd
In [2]:
Copied!
ds_path = "https://raw.githubusercontent.com/google/yggdrasil-decision-forests/main/yggdrasil_decision_forests/test_data/dataset"
train_ds = pd.read_csv(f"{ds_path}/adult_train.csv")
test_ds = pd.read_csv(f"{ds_path}/adult_test.csv")
# Print the first 5 training examples
train_ds.head(5)
# Train a model
model = ydf.RandomForestLearner(label="income").train(train_ds)
ds_path = "https://raw.githubusercontent.com/google/yggdrasil-decision-forests/main/yggdrasil_decision_forests/test_data/dataset"
train_ds = pd.read_csv(f"{ds_path}/adult_train.csv")
test_ds = pd.read_csv(f"{ds_path}/adult_test.csv")
# Print the first 5 training examples
train_ds.head(5)
# Train a model
model = ydf.RandomForestLearner(label="income").train(train_ds)
Train model on 22792 examples Model trained in 0:00:01.194396
Prediction analysis¶
In contrast to model analysis (model.analyze
), which examines a model as a whole, prediction analysis (model.analyze_prediction
) explains a single prediction of the model. The next example explains the model's prediction on the first test example.
In [3]:
Copied!
model.analyze_prediction(test_ds.iloc[:1])
model.analyze_prediction(test_ds.iloc[:1])
Out[3]:
Counterfactual examples¶
Counterfactual examples are the training examples that are most similar to a prediction according to a model. Counterfactual examples can be used to explain the model's prediction by examining the similarities and differences between their features.
For more information, see the standalone counterfactual notebook.