创建和测试分类器
Creating and tesing a classifier
我在 excel 文件中有两列。
第 1 行有确切的用户输入,第 2 行有其原因。例如
ROW 1 ROW 2
money deducted cause 1
delivery is late cause 2
something here cause 48
payment problem cause 1
. .
. .
任务是实现一个分类器,下次给出特定用户输入时,它可以将其归类为原因之一,即让分类器了解这些情况并预测未来值。
我有一些关于分类的知识,但我只是想知道如何使用 one vs rest 分类器来实现它。
这就是您使用 scikit-learn 实现此 classifier 的方式。根据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)
这就是训练一个class预测器的全部,然后你可以很容易地预测任何你想要的。
更多参考: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))
这将为您提供 class 标签,然后您可以将其作为索引传递给 target_names。
希望对您有所帮助!
我在 excel 文件中有两列。 第 1 行有确切的用户输入,第 2 行有其原因。例如
ROW 1 ROW 2
money deducted cause 1
delivery is late cause 2
something here cause 48
payment problem cause 1
. .
. .
任务是实现一个分类器,下次给出特定用户输入时,它可以将其归类为原因之一,即让分类器了解这些情况并预测未来值。
我有一些关于分类的知识,但我只是想知道如何使用 one vs rest 分类器来实现它。
这就是您使用 scikit-learn 实现此 classifier 的方式。根据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)
这就是训练一个class预测器的全部,然后你可以很容易地预测任何你想要的。 更多参考: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))
这将为您提供 class 标签,然后您可以将其作为索引传递给 target_names。
希望对您有所帮助!