Class weights in Random Forest model - TFDF library

103 Views Asked by At

I want to add class weights to the Random Forest model from the Random Forest Decision Trees library to take into account the minority class.

I have not found in the documentation how to do it or how to modify it in the loss function. I know that in the sklearn library it can be done easily, but I am interested in using the TensorFlow library. Thank you very much for all the suggestions.

1

There are 1 best solutions below

0
On

Tensorflow Decision Forests allows users to specify a weight column in the training dataset. The entries of this column will be used as example weights. This is the only way to add class weights.

When feeding a Pandas DataFrame transformed with tfdf.keras.pd_dataframe_to_tf_dataset(), the weight column can be specified directly with the weights= parameter, e.g.,

ds = tfdf.keras.pd_dataframe_to_tf_dataset(train_ds_pd, label="my_label", weights="my_weights")

When feeding a Tensorflow dataset directly, separate the weight column as a third channel in the tf.Dataset (as expected by Keras). The second channel of the tf.Dataset must always be the label column.

Full Disclosure: I'm one of the authors of Tensorflow Decision Forests.