我的 Excel 文件中有两列。 第 1 行有确切的用户输入,第 2 行有其原因。例如
ROW 1 ROW 2
money deducted cause 1
delivery is late cause 2
something here cause 48
payment problem cause 1
. .
. .
任务是实现一个分类器,下次当给出特定用户输入时,它可以将其分类为原因之一,即使分类器了解这些情况并预测 future 值。
我对分类有一些了解,但我只是想知道如何使用一个与其余分类器来实现这一点。
最佳答案
这就是您如何使用 scikit-learn 实现该分类器。根据target_names的索引将所有训练语句传递给X_train和相应的标签。
X_train = np.array(["money deducted",
"delivery is late",
"something here",
"payment problem"])
y_labels = [(1, ), (2, ), (3, ), (1, )]
y_train = MultiLabelBinarizer().fit_transform(y_labels)
target_names = ['cause1', 'cause2', 'cause48']
classifier = Pipeline([
('vectorizer', CountVectorizer()),
('tfidf', TfidfTransformer()),
('clf', OneVsRestClassifier(LinearSVC()))])
classifier.fit(X_train, y_train)
这就是训练分类器的全部内容,然后你就可以轻松预测你想要的任何内容。 更多引用:http://scikit-learn.org/stable/modules/generated/sklearn.multiclass.OneVsRestClassifier.html
然后将 y_lables 拟合并转换为二值化器:
mlb.fit_transform(y_labels)
然后预测如下:
mlb.inverse_transform(classifier.predict(X_test))
这将为您提供类标签,然后您可以将其作为索引传递给 target_names。
希望对你有帮助!
关于python - 创建和测试分类器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42716240/