Preliminaries

# Load libraries
from sklearn.ensemble import RandomForestClassifier
import numpy as np
from sklearn import datasets

Load Iris Flower Dataset

# Load data
iris = datasets.load_iris()
X = iris.data
y = iris.target

Adjust Iris Dataset To Make Classes Imbalanced

# Make class highly imbalanced by removing first 40 observations
X = X[40:,:]
y = y[40:]

# Create target vector indicating if class 0, otherwise 1
y = np.where((y == 0), 0, 1)

Train Random Forest While Balancing Classes

When using RandomForestClassifier a useful setting is class_weight=balanced wherein classes are automatically weighted inversely proportional to how frequently they appear in the data. Specifically:

=n/knj

where  is the total number of classes.

# Create decision tree classifer object
clf = RandomForestClassifier(random_state=0, n_jobs=-1, class_weight="balanced")

# Train model
model = clf.fit(X, y)

https://chrisalbon.com/machine_learning/trees_and_forests/handle_imbalanced_classes_in_random_forests/



类别不平衡处理方法:
https://segmentfault.com/a/1190000015248984

相关文章:

  • 2022-12-23
  • 2021-11-30
  • 2022-12-23
  • 2022-12-23
  • 2022-01-01
  • 2021-06-19
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-31
  • 2022-12-23
  • 2022-01-17
相关资源
相似解决方案