Note
Click here to download the full example code
Different Number of Levels
HiClass supports different number of levels in the hierarchy. For this example, we will train a local classifier per node with a hierarchy similar to the following image:
Out:
/home/docs/checkouts/readthedocs.org/user_builds/hiclass/envs/v4.2.8/lib/python3.8/site-packages/sklearn/utils/_array_api.py:380: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
array = numpy.asarray(array, order=order, dtype=dtype)
[['Mammal' 'Wolf' 'Dog']
['Mammal' 'Cat' '']
['Reptile' 'Lizard' '']
['Reptile' 'Snake' '']
['Bird' '' '']]
from sklearn.linear_model import LogisticRegression
from hiclass import LocalClassifierPerNode
# Define data
X_train = [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
X_test = [[9, 10], [7, 8], [5, 6], [3, 4], [1, 2]]
Y_train = [
["Bird"],
["Reptile", "Snake"],
["Reptile", "Lizard"],
["Mammal", "Cat"],
["Mammal", "Wolf", "Dog"],
]
# Use random forest classifiers for every node
rf = LogisticRegression()
classifier = LocalClassifierPerNode(local_classifier=rf)
# Train local classifier per node
classifier.fit(X_train, Y_train)
# Predict
predictions = classifier.predict(X_test)
print(predictions)
Total running time of the script: ( 0 minutes 0.048 seconds)