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")
# 打印前5个训练样本
train_ds.head(5)
# 训练一个模型
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")
# 打印前5个训练样本
train_ds.head(5)
# 训练一个模型
model = ydf.RandomForestLearner(label="income").train(train_ds)
Train model on 22792 examples Model trained in 0:00:01.194396
预测分析¶
与模型分析 (model.analyze
) 不同,模型分析检查整个模型,而预测分析 (model.analyze_prediction
) 解释模型的单个预测。下一个示例解释了模型对第一个测试实例的预测。
In [3]:
Copied!
model.analyze_prediction(test_ds.iloc[:1])
model.analyze_prediction(test_ds.iloc[:1])
Out[3]: