注意
点击 here 下载完整的示例代码
构建管道
HiClass 可以在 scikit-learn 管道中使用,并且完全支持稀疏矩阵作为输入。 此示例展示了这两个功能的使用。
输出:
[['Credit reporting' 'Reports']
['Loan' 'Student loan']]
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline
from hiclass import LocalClassifierPerParentNode
# Define data
X_train = [
"Struggling to repay loan",
"Unable to get annual report",
]
X_test = [
"Unable to get annual report",
"Struggling to repay loan",
]
Y_train = [["Loan", "Student loan"], ["Credit reporting", "Reports"]]
# We will use logistic regression classifiers for every parent node
lr = LogisticRegression()
# Let's build a pipeline using CountVectorizer and TfidfTransformer
# to extract features as sparse matrices
pipeline = Pipeline(
[
("count", CountVectorizer()),
("tfidf", TfidfTransformer()),
("lcppn", LocalClassifierPerParentNode(local_classifier=lr)),
]
)
# Now, let's train a local classifier per parent node
pipeline.fit(X_train, Y_train)
# Finally, let's predict using the pipeline
predictions = pipeline.predict(X_test)
print(predictions)
脚本总运行时间: ( 0 分钟 0.028 秒)