How to set class weights in DecisionTreeClassifier for multi-class setting

Written by- Aionlinecourse1994 times views

In a multi-class setting, you can set class weights in the DecisionTreeClassifier by using the class_weight parameter. This parameter can be set to a dictionary or a "balanced" string.

If you set class_weight to a dictionary, the keys should be the class labels and the values should be the corresponding weights. For example, if you have a 3-class problem with the class labels 0, 1, and 2, you can set the class weights as follows:
class_weights = {0: 1, 1: 2, 2: 1}
clf = DecisionTreeClassifier(class_weight=class_weights)
This will assign a weight of 1 to class 0, a weight of 2 to class 1, and a weight of 1 to class 2.

Alternatively, you can set class_weight to the string "balanced", which will automatically adjust the weights inversely proportional to the class frequencies in the input data. For example, if the class frequency for class 0 is 20%, the weight for class 0 will be 1 / 0.2 = 5. The weights for the other classes will be similarly adjusted.
clf = DecisionTreeClassifier(class_weight='balanced')
You can also use the class_weight parameter in combination with the sample_weight parameter, which allows you to set weights for individual samples in the training set.