Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 选择sklearn管道对用户文本数据进行分类_Python_Machine Learning_Scikit Learn_Feature Selection - Fatal编程技术网

Python 选择sklearn管道对用户文本数据进行分类

Python 选择sklearn管道对用户文本数据进行分类,python,machine-learning,scikit-learn,feature-selection,Python,Machine Learning,Scikit Learn,Feature Selection,我正在用Python开发一个机器学习应用程序(使用sklearn模块),目前正试图确定一个用于执行推理的模型。问题的简要说明: 考虑到许多用户数据的实例,我试图根据相对关键字包含将它们分类为不同的类别。它是受监督的,所以我有很多已经分类的预分类数据的实例。(每段数据的字数在2到12个左右。) 我目前正试图在两种潜在模式之间做出选择: 计数向量器+多项式朴素贝叶斯。使用sklearn的CountVectorizer获取训练数据中的关键字计数。然后,使用朴素贝叶斯(naivebayes)使用skle

我正在用Python开发一个机器学习应用程序(使用sklearn模块),目前正试图确定一个用于执行推理的模型。问题的简要说明:

考虑到许多用户数据的实例,我试图根据相对关键字包含将它们分类为不同的类别。它是受监督的,所以我有很多已经分类的预分类数据的实例。(每段数据的字数在2到12个左右。)

我目前正试图在两种潜在模式之间做出选择:

  • 计数向量器+多项式朴素贝叶斯。使用sklearn的CountVectorizer获取训练数据中的关键字计数。然后,使用朴素贝叶斯(naivebayes)使用sklearn的多项式nb模型对数据进行分类

  • 对关键字计数使用tf idf术语权重+标准朴素贝叶斯。使用CountVectorizer获取训练数据的关键字计数矩阵,使用sklearn的TFIDFTranformer将该数据转换为tf idf加权,然后将其转储到标准Naive Bayes模型中

  • 我已经阅读了这两种方法中使用的类的文档,它们似乎都很好地解决了我的问题


    对于这类问题,使用标准朴素贝叶斯模型的tf idf权重可能优于多项式朴素贝叶斯模型,有什么明显的原因吗?这两种方法都有明显的问题吗?

    朴素贝叶斯和多项式nb是相同的算法。不同之处在于tfidf转换会惩罚语料库中大量文档中出现的单词

    我的忠告是: 使用tfidf并调整特征的tfidf矢量化的子线性、二进制参数和规范化参数

    还可以尝试scikit learn中提供的各种不同的分类器,如果您正确调整正则化类型(l1或l2)和正则化参数(alpha)的值,我怀疑这些分类器会给您带来更好的结果

    如果您对它们进行适当的调整,我怀疑您可以使用带有“对数”损失(逻辑回归)或“铰链”损失(SVM)的SGDClassizer获得更好的结果


    人们通常通过scikit learn中的GridSearchCV类来调整参数。

    我同意David的评论。你会想训练不同的模型,看看哪个是最好的

    import pandas as pd
    import numpy as np
    
    from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
    from sklearn.naive_bayes import MultinomialNB, GaussianNB, BernoulliNB
    from sklearn.pipeline import Pipeline
    from sklearn.grid_search import GridSearchCV
    
    from pprint import pprint
    
    df = pd.DataFrame({'Keyword': ['buy widget', 'buy widgets', 'fiberglass widget',
                                   'fiberglass widgets', 'how much are widget',
                                   'how much are widgets', 'installing widget',
                                   'installing widgets', 'vinyl widget', 'vinyl widgets',
                                   'widget cost', 'widget estimate', 'widget install',
                                   'widget installation', 'widget price', 'widget pricing',
                                   'widgets cost', 'widgets estimate', 'widgets install',
                                   'widgets installation', 'widgets price', 'widgets pricing',
                                   'wood widget', 'wood widgets'],
                       'Label': ['Buy', 'Buy', 'Fiberglass', 'Fiberglass', 'Cost', 'Cost',
                                 'Install', 'Install', 'Vinyl', 'Vinyl', 'Cost', 'Estimate',
                                 'Install', 'Install', 'Cost', 'Cost', 'Cost', 'Estimate',
                                 'Install', 'Install', 'Cost', 'Cost', 'Wood', 'Wood']},
                      columns=['Label', 'Keyword'])
    
    X = df['Keyword']
    y = df['Label']
    
    ##pipeline = Pipeline(steps=[
    ##  ('cvect', CountVectorizer()),
    ##  ('mnb', MultinomialNB())
    ##  ])
    
    pipeline = Pipeline(steps=[
      ('tfidf', TfidfVectorizer()),
      ('bnb', BernoulliNB())
      ])
    
    parameters = {'tfidf__ngram_range': [(1,1), (1,2)],
                  'tfidf__stop_words': [None, 'english'],
                  'tfidf__use_idf': [True, False],
                  'bnb__alpha': [0.0, 0.5, 1.0],
                  'bnb__binarize': [None, 0.2, 0.5, 0.7, 1.0],
                  'bnb__fit_prior': [True, False]}
    
    grid = GridSearchCV(pipeline, parameters, scoring='accuracy', cv=2, verbose=1)
    grid.fit(X, y)
    
    print('Best score:', grid.best_score_)
    print('Best parameters:', pprint(grid.best_params_, indent=2))
    
    # Here's how to predict (uncomment)
    #pred = grid.predict(['buy wood widget', 'how much is a widget'])
    
    #print(pred)
    

    建立多个模型,看看哪一个有最好的评价简历分数。我有点困惑。你说朴素贝叶斯和多项式NB是一样的,但是多项式NB不是对训练数据施加了不同的分布吗?你可以阅读朴素贝叶斯的解释。NaiveBayes是一组关于标签分布的假设不同的分类器(Gaussian、BernoulliNB、多项式nb)。您可以使用此脚本尝试不同的分类器。你只需要注入你自己的特性。对于单个分类器的参数,请使用GridSearchCV进行调优。不要忘记,调整参数会对性能产生巨大的影响。