注意
点击 here 下载完整的示例代码
不同层级数量
HiClass 支持层次结构中的不同级别数量。 对于这个例子,我们将为每个节点训练一个本地分类器, 其层次结构类似于下图:
输出:
[['Mammal' 'Wolf' 'Dog']
['Mammal' 'Cat' '']
['Reptile' 'Lizard' '']
['Reptile' 'Snake' '']
['Bird' '' '']]
import numpy as np
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 = np.array(
[
["Bird"],
["Reptile", "Snake"],
["Reptile", "Lizard"],
["Mammal", "Cat"],
["Mammal", "Wolf", "Dog"],
],
dtype=object,
)
# 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)
脚本总运行时间: ( 0 分钟 0.035 秒)