python - 创建和测试分类器

标签 python machine-learning classification multiclass-classification

我的 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/

相关文章:

python - 规范化 pandas 中的数据

Python:检查 numpy 数组是否包含具有特定属性的对象

image-processing - 带有 SIFT/VLFEAT 的图像描述符

python - 以 y_true 取决于 y_pred 的方式自定义 Keras 的损失函数

Python MySQL - 带参数和其他占位符的 mysqldb 查询

python - 如何在setup.cfg中指明use_scm_version?

tensorflow - CNN 的模型架构设计

python - 卷积自动编码器图像尺寸误差

algorithm - 您如何使用为加权综合评分中的特征分配权重?

python - 使用 python api 和 scikit-learn wapper 的 XGBoost 的不同结果