I have created a multilabel classification dataset using make_multilabel_classification from scikit learn:
from sklearn.datasets import make_multilabel_classification as mmc
X, Y, p_c, p_w_c = mmc(
n_samples = 1000,
n_features = 50,
n_classes = 17,
n_labels = 6,
length = 5000,
allow_unlabeled = False,
return_distributions = True,
random_state = 1
)
df = pd.DataFrame(np.column_stack((X, Y))) # , columns = "W", "X", "Y", "Z", "A", "B", "C", "D", "E", "F", "G", "H"]
X = df.iloc[:,:50]
Y = df.iloc[:,50:]
Now I also created an instance of a Random Forest in order for me to obtain the OOB (Out-of-Bag) error score.
from sklearn.ensemble import RandomForestClassifier
from sklearn.feature_selection import SelectFromModel
from sklearn.model_selection import train_test_split
rf = RandomForestClassifier(
oob_score = True,
random_state = 50,
warm_start = True,
n_estimators = 200,
verbose = 3,
)
rf.fit(X, Y)
oob_error = 1 - rf.oob_score_
# Print the OOB error
print(f'OOB error: {oob_error:.3f}')
But the problem occurs when I try to fit X
and Y
based from specific values of n_features
and n_classes
to the Random Forest. It seems that if I set n_classes = 17
, and n_features to just any value
, it will produce an error which is a ValueError: could not broadcast input array from shape (376,2) into shape (376,)
for some odd reason, even though the dimensionality of X and Y are in proper form.
On the other hand, if I set n_classes = 15
or n_classes = 16
, as long as its value is less than 17, and n_features to just any value
, it will successfully fit X
and Y
to the Random Forest
and compute the OOB error score
.
Currently I'm quite confused on why this error of "could not broadcast input array" occur, especially since I did several changes on the n_classes
and n_features
already but the error still occurs especially if n_classes value
is greater than or equal to 17.
Full source code here at Google Colab
May I know on which part I should fine tune on this Random Forest model in acquiring its OOB Error score since I am literally confused into which I area I should be fixing within the model. Your responses would indeed be highly appreciated! Thank you very much.