zoukankan      html  css  js  c++  java
  • 【Scikit】实现Multi-label text classification代码模板

    Refer to: https://stackoverflow.com/a/10527953

    code:

    # -*- coding: utf-8 -*-
    import numpy as np
    from sklearn.pipeline import Pipeline
    from sklearn.feature_extraction.text import CountVectorizer
    from sklearn.svm import LinearSVC
    from sklearn.feature_extraction.text import TfidfTransformer
    from sklearn.multiclass import OneVsRestClassifier
    from sklearn.preprocessing import MultiLabelBinarizer
    
    X_train = np.array(["new york is a hell of a town",
                        "new york was originally dutch",
                        "the big apple is great",
                        "new york is also called the big apple",
                        "nyc is nice",
                        "people abbreviate new york city as nyc",
                        "the capital of great britain is london",
                        "london is in the uk",
                        "london is in england",
                        "london is in great britain",
                        "it rains a lot in london",
                        "london hosts the british museum",
                        "new york is great and so is london",
                        "i like london better than new york"])
    y_train_text = [["new york"],["new york"],["new york"],["new york"],["new york"],
                    ["new york"],["london"],["london"],["london"],["london"],
                    ["london"],["london"],["new york","london"],["new york","london"]]
    
    X_test = np.array(['nice day in nyc',
                       'welcome to london',
                       'london is rainy',
                       'it is raining in britian',
                       'it is raining in britian and the big apple',
                       'it is raining in britian and nyc',
                       'hello welcome to new york. enjoy it here and london too'])
    target_names = ['New York', 'London']
    
    mlb = MultiLabelBinarizer()
    Y = mlb.fit_transform(y_train_text)
    
    classifier = Pipeline([
        ('vectorizer', CountVectorizer()),
        ('tfidf', TfidfTransformer()),
        ('clf', OneVsRestClassifier(LinearSVC()))])
    
    classifier.fit(X_train, Y)
    predicted = classifier.predict(X_test)
    all_labels = mlb.inverse_transform(predicted)
    
    for item, labels in zip(X_test, all_labels):
        print('{0} => {1}'.format(item, ', '.join(labels)))

    Output:

    nice day in nyc => new york
    welcome to london => london
    london is rainy => london
    it is raining in britian => london
    it is raining in britian and the big apple => new york
    it is raining in britian and nyc => london, new york
    hello welcome to new york. enjoy it here and london too => london, new york
  • 相关阅读:
    移动端获取屏幕的宽度,根据屏幕大小动态设置html的rem字体大小
    解析CSS3伪类选择器nth-of-type和nth-child的用法,以及两者的区别
    移动端的1px的解决方案
    Vue中import from的来源:省略后缀与加载文件夹
    flex布局
    前端开发人员快速创建本地服务器
    centos6.5Xen4.2安装
    centos6.5kvm虚拟化安装部署
    CentOS搭建svn服务器支持https访问
    CentOS6.5搭建LNMP
  • 原文地址:https://www.cnblogs.com/XBWer/p/6594623.html
Copyright © 2011-2022 走看看