# Install TF-DF
!pip install tensorflow tensorflow_decision_forests

# Load TF-DF
import tensorflow_decision_forests as tfdf
import pandas as pd

# Load a dataset in a Pandas dataframe.
train_df = pd.read_csv("project/train.csv")
test_df = pd.read_csv("project/test.csv")

# Convert the dataset into a TensorFlow dataset.
train_ds = tfdf.keras.pd_dataframe_to_tf_dataset(train_df, label="my_label")
test_ds = tfdf.keras.pd_dataframe_to_tf_dataset(test_df, label="my_label")

# Train a Random Forest model.
model = tfdf.keras.RandomForestModel()
model.fit(train_ds)

# Summary of the model structure.
model.summary()

# Compute model accuracy.
model.compile(metrics=["accuracy"])
model.evaluate(test_ds, return_dict=True)

# Export the model to a SavedModel.
model.save("project/model")
# Install YDF
!pip install ydf -U

import ydf
import pandas as pd

# Load a dataset with Pandas
ds_path = "https://raw.githubusercontent.com/google/yggdrasil-decision-forests/main/yggdrasil_decision_forests/test_data/dataset/"
train_ds = pd.read_csv(ds_path + "adult_train.csv")
test_ds = pd.read_csv(ds_path + "adult_test.csv")

# Train a Gradient Boosted Trees model
model = ydf.GradientBoostedTreesLearner(label="income").train(train_ds)

# Look at a model (input features, training logs, structure, etc.)
model.describe()

# Evaluate a model (e.g. roc, accuracy, confusion matrix, confidence intervals)
model.evaluate(test_ds)

# Generate predictions
model.predict(test_ds)

# Analyse a model (e.g. partial dependence plot, variable importance)
model.analyze(test_ds)

# Benchmark the inference speed of a model
model.benchmark(test_ds)

# Save the model
model.save("/tmp/my_model")

# Export the model as a TensorFlow Saved Model
model.to_tensorflow_saved_model("/tmp/my_saved_model")

YDF 是 Google 用于训练决策森林的新库。

YDF 扩展了 TF-DF 的功能,提供新功能、简化的 API、更快的训练时间、更新的文档以及与流行的 ML 库的增强兼容性。

访问新网站

TensorFlow 决策森林 (TF-DF) 是一个用于在 TensorFlow 中训练、运行和解释 决策森林 模型(例如,随机森林、梯度提升树)的库。TF-DF 支持分类、回归、排名和提升。

关键词:决策森林、TensorFlow、随机森林、梯度提升树、CART、模型解释。

文档和资源

提供以下资源

社区