• Working With Text Data of sklearn


    Working With Text Data

    https://scikit-learn.org/stable/tutorial/text_analytics/working_with_text_data.html#working-with-text-data

         分析文本文档,关于20个不同主题。

        包括以下步骤:

        (1)加载文件内容和分类

        (2)提取适合机器学习的特征向量

        (3)训练线性模型执行分类

        (4)使用网格搜索确定好了配置,对于特征提取和分类器训练

    The goal of this guide is to explore some of the main scikit-learn tools on a single practical task: analyzing a collection of text documents (newsgroups posts) on twenty different topics.

    In this section we will see how to:

    • load the file contents and the categories

    • extract feature vectors suitable for machine learning

    • train a linear model to perform categorization

    • use a grid search strategy to find a good configuration of both the feature extraction components and the classifier

    Loading the 20 newsgroups dataset

    https://scikit-learn.org/stable/tutorial/text_analytics/working_with_text_data.html#loading-the-20-newsgroups-dataset

         二十个新闻组数据集,关于不同主题的文本数据集合。

        现在被广泛应用于文本分类和聚类。

    The dataset is called “Twenty Newsgroups”. Here is the official description, quoted from the website:

    The 20 Newsgroups data set is a collection of approximately 20,000 newsgroup documents, partitioned (nearly) evenly across 20 different newsgroups. To the best of our knowledge, it was originally collected by Ken Lang, probably for his paper “Newsweeder: Learning to filter netnews,” though he does not explicitly mention this collection. The 20 newsgroups collection has become a popular data set for experiments in text applications of machine learning techniques, such as text classification and text clustering.

         加载方式如下。

    In order to get faster execution times for this first example we will work on a partial dataset with only 4 categories out of the 20 available in the dataset:

    >>>
    >>> categories = ['alt.atheism', 'soc.religion.christian',
    ...               'comp.graphics', 'sci.med']
    

    We can now load the list of files matching those categories as follows:

    >>>
    >>> from sklearn.datasets import fetch_20newsgroups
    >>> twenty_train = fetch_20newsgroups(subset='train',
    ...     categories=categories, shuffle=True, random_state=42)
    

         也可以自己下载后, 使用load_files接口加载数据。

    In the following we will use the built-in dataset loader for 20 newsgroups from scikit-learn. Alternatively, it is possible to download the dataset manually from the website and use the sklearn.datasets.load_files function by pointing it to the 20news-bydate-train sub-folder of the uncompressed archive folder.

    Extracting features from text files

    文本本身不适合机器学习, 需要将文本内容转化为数值型的特征向量。

    In order to perform machine learning on text documents, we first need to turn the text content into numerical feature vectors.

    词袋

          词袋是特征向量的一种形式。

          方法是,按照文档,统计每个文档中的词出现的数目, 或者对词的数目做进一步处理。

          每个文档的词可能大部分都是不同的, 词袋的存储形式,视图上是一个二维矩阵, 但是存储上应该是稀疏矩阵。

    Bags of words

    The most intuitive way to do so is to use a bags of words representation:

    1. Assign a fixed integer id to each word occurring in any document of the training set (for instance by building a dictionary from words to integer indices).

    2. For each document #i, count the number of occurrences of each word w and store it in X[i, j] as the value of feature #j where j is the index of word w in the dictionary.

    The bags of words representation implies that n_features is the number of distinct words in the corpus: this number is typically larger than 100,000.

    If n_samples == 10000, storing X as a NumPy array of type float32 would require 10000 x 100000 x 4 bytes = 4GB in RAM which is barely manageable on today’s computers.

    Fortunately, most values in X will be zeros since for a given document less than a few thousand distinct words will be used. For this reason we say that bags of words are typically high-dimensional sparse datasets. We can save a lot of memory by only storing the non-zero parts of the feature vectors in memory.

    scipy.sparse matrices are data structures that do exactly this, and scikit-learn has built-in support for these structures.

    Tokenizing text with scikit-learn

          文档是由若干词语组成, 需要将文档中的词语解析出来,这个解析的过程就是 word tokonization. -- 词语标签化。

         最简单的一个标签工具,就是 CountVectorizer, 计数向量化, 构建一个特征词典, 每个词语就是一个特征, 特征向量的值就是此的数目。

    Text preprocessing, tokenizing and filtering of stopwords are all included in CountVectorizer, which builds a dictionary of features and transforms documents to feature vectors:

    >>>
    from sklearn.feature_extraction.text import CountVectorizer
    count_vect = CountVectorizer()
    X_train_counts = count_vect.fit_transform(twenty_train.data)
    X_train_counts.shape
    
    

    CountVectorizer supports counts of N-grams of words or consecutive characters. Once fitted, the vectorizer has built a dictionary of feature indices:

    >>>
    count_vect.vocabulary_.get(u'algorithm')
    
    

    The index value of a word in the vocabulary is linked to its frequency in the whole training corpus.

    CountVectorizer

    https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.CountVectorizer.html

         转换多个文档为标签数目矩阵。

    Convert a collection of text documents to a matrix of token counts

    This implementation produces a sparse representation of the counts using scipy.sparse.csr_matrix.

    If you do not provide an a-priori dictionary and you do not use an analyzer that does some kind of feature selection then the number of features will be equal to the vocabulary size found by analyzing the data.

    >>> from sklearn.feature_extraction.text import CountVectorizer
    >>> corpus = [
    ...     'This is the first document.',
    ...     'This document is the second document.',
    ...     'And this is the third one.',
    ...     'Is this the first document?',
    ... ]
    >>> vectorizer = CountVectorizer()
    >>> X = vectorizer.fit_transform(corpus)
    >>> print(vectorizer.get_feature_names())
    ['and', 'document', 'first', 'is', 'one', 'second', 'the', 'third', 'this']
    >>> print(X.toarray())
    [[0 1 1 1 0 0 1 0 1]
     [0 2 0 1 0 1 1 0 1]
     [1 0 0 1 1 0 1 1 1]
     [0 1 1 1 0 0 1 0 1]]
    >>> vectorizer2 = CountVectorizer(analyzer='word', ngram_range=(2, 2))
    >>> X2 = vectorizer2.fit_transform(corpus)
    >>> print(vectorizer2.get_feature_names())
    ['and this', 'document is', 'first document', 'is the', 'is this',
    'second document', 'the first', 'the second', 'the third', 'third one',
     'this document', 'this is', 'this the']
     >>> print(X2.toarray())
     [[0 0 1 1 0 0 1 0 0 0 0 1 0]
     [0 1 0 1 0 1 0 1 0 0 1 0 0]
     [1 0 0 1 0 0 0 0 1 1 0 1 0]
     [0 0 1 0 1 0 1 0 0 0 0 0 1]]

    From occurrences to frequencies

          长的文档,词统计的数目大, 短的文档, 词数目小。

          没有可比性, 把词数目,转化为百分比的数据, 可以进行文档之间的横向比较。

    Occurrence count is a good start but there is an issue: longer documents will have higher average count values than shorter documents, even though they might talk about the same topics.

    To avoid these potential discrepancies it suffices to divide the number of occurrences of each word in a document by the total number of words in the document: these new features are called tf for Term Frequencies.

         对于所有文档都具有的词,往往具有较小的信息量。

        基于这个理念, 使用逆文档频率方法, 可以降低这种词对信息量的贡献。

    Another refinement on top of tf is to downscale weights for words that occur in many documents in the corpus and are therefore less informative than those that occur only in a smaller portion of the corpus.

    This downscaling is called tf–idf for “Term Frequency times Inverse Document Frequency”.

    Both tf and tf–idf can be computed as follows using TfidfTransformer:

    from sklearn.feature_extraction.text import TfidfTransformer
    tf_transformer = TfidfTransformer(use_idf=False).fit(X_train_counts)
    X_train_tf = tf_transformer.transform(X_train_counts)
    X_train_tf.shape

    TfidfVectorizer

    https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.TfidfVectorizer.html

    Convert a collection of raw documents to a matrix of TF-IDF features.

    Equivalent to CountVectorizer followed by TfidfTransformer.

    >>> from sklearn.feature_extraction.text import TfidfVectorizer
    >>> corpus = [
    ...     'This is the first document.',
    ...     'This document is the second document.',
    ...     'And this is the third one.',
    ...     'Is this the first document?',
    ... ]
    >>> vectorizer = TfidfVectorizer()
    >>> X = vectorizer.fit_transform(corpus)
    >>> print(vectorizer.get_feature_names())
    ['and', 'document', 'first', 'is', 'one', 'second', 'the', 'third', 'this']
    >>> print(X.shape)
    (4, 9)

    Training a classifier

          特征向量准备好, 使用朴素贝叶斯分类器训练模型。

    Now that we have our features, we can train a classifier to try to predict the category of a post. Let’s start with a naïve Bayes classifier, which provides a nice baseline for this task. scikit-learn includes several variants of this classifier; the one most suitable for word counts is the multinomial variant:

    >>>
    from sklearn.naive_bayes import MultinomialNB
    clf = MultinomialNB().fit(X_train_tfidf, twenty_train.target)
    

    To try to predict the outcome on a new document we need to extract the features using almost the same feature extracting chain as before. The difference is that we call transform instead of fit_transform on the transformers, since they have already been fit to the training set:

    >>>
    >>> docs_new = ['God is love', 'OpenGL on the GPU is fast']
    >>> X_new_counts = count_vect.transform(docs_new)
    >>> X_new_tfidf = tfidf_transformer.transform(X_new_counts)
    
    >>> predicted = clf.predict(X_new_tfidf)
    
    >>> for doc, category in zip(docs_new, predicted):
    ...     print('%r => %s' % (doc, twenty_train.target_names[category]))
    ...
    'God is love' => soc.religion.christian
    'OpenGL on the GPU is fast' => comp.graphics
    

    Building a pipeline

        可以使用流水线,将向量化 和 模型训练连接在一起。

    In order to make the vectorizer => transformer => classifier easier to work with, scikit-learn provides a Pipeline class that behaves like a compound classifier:

    >>>
    >>> from sklearn.pipeline import Pipeline
    >>> text_clf = Pipeline([
    ...     ('vect', CountVectorizer()),
    ...     ('tfidf', TfidfTransformer()),
    ...     ('clf', MultinomialNB()),
    ... ])
    

    The names vect, tfidf and clf (classifier) are arbitrary. We will use them to perform grid search for suitable hyperparameters below. We can now train the model with a single command:

    >>>
    >>> text_clf.fit(twenty_train.data, twenty_train.target)
    Pipeline(...)
    

    Evaluation of the performance on the test set

         在测试集合上检验模型性能。简单查看比对正确率。

    Evaluating the predictive accuracy of the model is equally easy:

    >>>
    >>> import numpy as np
    >>> twenty_test = fetch_20newsgroups(subset='test',
    ...     categories=categories, shuffle=True, random_state=42)
    >>> docs_test = twenty_test.data
    >>> predicted = text_clf.predict(docs_test)
    >>> np.mean(predicted == twenty_test.target)
    0.8348...
    

    We achieved 83.5% accuracy. Let’s see if we can do better with a linear support vector machine (SVM), which is widely regarded as one of the best text classification algorithms (although it’s also a bit slower than naïve Bayes). We can change the learner by simply plugging a different classifier object into our pipeline:

    >>>
    >>> from sklearn.linear_model import SGDClassifier
    >>> text_clf = Pipeline([
    ...     ('vect', CountVectorizer()),
    ...     ('tfidf', TfidfTransformer()),
    ...     ('clf', SGDClassifier(loss='hinge', penalty='l2',
    ...                           alpha=1e-3, random_state=42,
    ...                           max_iter=5, tol=None)),
    ... ])
    
    >>> text_clf.fit(twenty_train.data, twenty_train.target)
    Pipeline(...)
    >>> predicted = text_clf.predict(docs_test)
    >>> np.mean(predicted == twenty_test.target)
    0.9101...
    

         也可以使用metrics工具来查看详细分类性能。

    We achieved 91.3% accuracy using the SVM. scikit-learn provides further utilities for more detailed performance analysis of the results:

    >>>
    >>> from sklearn import metrics
    >>> print(metrics.classification_report(twenty_test.target, predicted,
    ...     target_names=twenty_test.target_names))
                            precision    recall  f1-score   support
    
               alt.atheism       0.95      0.80      0.87       319
             comp.graphics       0.87      0.98      0.92       389
                   sci.med       0.94      0.89      0.91       396
    soc.religion.christian       0.90      0.95      0.93       398
    
                  accuracy                           0.91      1502
                 macro avg       0.91      0.91      0.91      1502
              weighted avg       0.91      0.91      0.91      1502
    
    
    >>> metrics.confusion_matrix(twenty_test.target, predicted)
    array([[256,  11,  16,  36],
           [  4, 380,   3,   2],
           [  5,  35, 353,   3],
           [  5,  11,   4, 378]])
    

    As expected the confusion matrix shows that posts from the newsgroups on atheism and Christianity are more often confused for one another than with computer graphics.

    Parameter tuning using grid search

          使用网格搜索来进行模型和参数选择。

    We’ve already encountered some parameters such as use_idf in the TfidfTransformer. Classifiers tend to have many parameters as well; e.g., MultinomialNB includes a smoothing parameter alpha and SGDClassifier has a penalty parameter alpha and configurable loss and penalty terms in the objective function (see the module documentation, or use the Python help function to get a description of these).

    Instead of tweaking the parameters of the various components of the chain, it is possible to run an exhaustive search of the best parameters on a grid of possible values. We try out all classifiers on either words or bigrams, with or without idf, and with a penalty parameter of either 0.01 or 0.001 for the linear SVM:

    >>>
    from sklearn.model_selection import GridSearchCV
    parameters = {
        'vect__ngram_range': [(1, 1), (1, 2)],
        'tfidf__use_idf': (True, False),
        'clf__alpha': (1e-2, 1e-3),
    }
    

    Obviously, such an exhaustive search can be expensive. If we have multiple CPU cores at our disposal, we can tell the grid searcher to try these eight parameter combinations in parallel with the n_jobs parameter. If we give this parameter a value of -1, grid search will detect how many cores are installed and use them all:

    >>>
    >>> gs_clf = GridSearchCV(text_clf, parameters, cv=5, n_jobs=-1)
    

    The grid search instance behaves like a normal scikit-learn model. Let’s perform the search on a smaller subset of the training data to speed up the computation:

    >>>
    >>> gs_clf = gs_clf.fit(twenty_train.data[:400], twenty_train.target[:400])
    

    The result of calling fit on a GridSearchCV object is a classifier that we can use to predict:

    >>>
    >>> twenty_train.target_names[gs_clf.predict(['God is love'])[0]]
    'soc.religion.christian'
    

    The object’s best_score_ and best_params_ attributes store the best mean score and the parameters setting corresponding to that score:

    >>>
    >>> gs_clf.best_score_
    0.9...
    >>> for param_name in sorted(parameters.keys()):
    ...     print("%s: %r" % (param_name, gs_clf.best_params_[param_name]))
    ...
    clf__alpha: 0.001
    tfidf__use_idf: True
    vect__ngram_range: (1, 1)
    

    A more detailed summary of the search is available at gs_clf.cv_results_.

    The cv_results_ parameter can be easily imported into pandas as a DataFrame for further inspection.

    Language identification

         学习不同国家语言, 预测用户输入哪国语言。

    • Write a text classification pipeline using a custom preprocessor and CharNGramAnalyzer using data from Wikipedia articles as training set.

    • Evaluate the performance on some held out test set.

    https://github.com/scikit-learn/scikit-learn/blob/master/doc/tutorial/text_analytics/solutions/exercise_01_language_train_model.py

    """Build a language detector model
    The goal of this exercise is to train a linear classifier on text features
    that represent sequences of up to 3 consecutive characters so as to be
    recognize natural languages by using the frequencies of short character
    sequences as 'fingerprints'.
    """
    # Author: Olivier Grisel <olivier.grisel@ensta.org>
    # License: Simplified BSD
    
    import sys
    
    from sklearn.feature_extraction.text import TfidfVectorizer
    from sklearn.linear_model import Perceptron
    from sklearn.pipeline import Pipeline
    from sklearn.datasets import load_files
    from sklearn.model_selection import train_test_split
    from sklearn import metrics
    
    
    # The training data folder must be passed as first argument
    languages_data_folder = sys.argv[1]
    dataset = load_files(languages_data_folder)
    
    # Split the dataset in training and test set:
    docs_train, docs_test, y_train, y_test = train_test_split(
        dataset.data, dataset.target, test_size=0.5)
    
    
    # TASK: Build a vectorizer that splits strings into sequence of 1 to 3
    # characters instead of word tokens
    vectorizer = TfidfVectorizer(ngram_range=(1, 3), analyzer='char',
                                 use_idf=False)
    
    # TASK: Build a vectorizer / classifier pipeline using the previous analyzer
    # the pipeline instance should stored in a variable named clf
    clf = Pipeline([
        ('vec', vectorizer),
        ('clf', Perceptron()),
    ])
    
    # TASK: Fit the pipeline on the training set
    clf.fit(docs_train, y_train)
    
    # TASK: Predict the outcome on the testing set in a variable named y_predicted
    y_predicted = clf.predict(docs_test)
    
    # Print the classification report
    print(metrics.classification_report(y_test, y_predicted,
                                        target_names=dataset.target_names))
    
    # Plot the confusion matrix
    cm = metrics.confusion_matrix(y_test, y_predicted)
    print(cm)
    
    #import matlotlib.pyplot as plt
    #plt.matshow(cm, cmap=plt.cm.jet)
    #plt.show()
    
    # Predict the result on some short new sentences:
    sentences = [
        'This is a language detection test.',
        'Ceci est un test de dxe9tection de la langue.',
        'Dies ist ein Test, um die Sprache zu erkennen.',
    ]
    predicted = clf.predict(sentences)
    
    for s, p in zip(sentences, predicted):
        print('The language of "%s" is "%s"' % (s, dataset.target_names[p]))

    Sentiment Analysis on movie reviews

    https://github.com/scikit-learn/scikit-learn/blob/master/doc/tutorial/text_analytics/solutions/exercise_02_sentiment.py

         根据已有正向和反向的评价资料,训练模型,预测任意输入的评价结果。

    • Write a text classification pipeline to classify movie reviews as either positive or negative.

    • Find a good set of parameters using grid search.

    • Evaluate the performance on a held out test set.

    """Build a sentiment analysis / polarity model
    Sentiment analysis can be casted as a binary text classification problem,
    that is fitting a linear classifier on features extracted from the text
    of the user messages so as to guess whether the opinion of the author is
    positive or negative.
    In this examples we will use a movie review dataset.
    """
    # Author: Olivier Grisel <olivier.grisel@ensta.org>
    # License: Simplified BSD
    
    import sys
    from sklearn.feature_extraction.text import TfidfVectorizer
    from sklearn.svm import LinearSVC
    from sklearn.pipeline import Pipeline
    from sklearn.model_selection import GridSearchCV
    from sklearn.datasets import load_files
    from sklearn.model_selection import train_test_split
    from sklearn import metrics
    
    
    if __name__ == "__main__":
        # NOTE: we put the following in a 'if __name__ == "__main__"' protected
        # block to be able to use a multi-core grid search that also works under
        # Windows, see: http://docs.python.org/library/multiprocessing.html#windows
        # The multiprocessing module is used as the backend of joblib.Parallel
        # that is used when n_jobs != 1 in GridSearchCV
    
        # the training data folder must be passed as first argument
        movie_reviews_data_folder = sys.argv[1]
        dataset = load_files(movie_reviews_data_folder, shuffle=False)
        print("n_samples: %d" % len(dataset.data))
    
        # split the dataset in training and test set:
        docs_train, docs_test, y_train, y_test = train_test_split(
            dataset.data, dataset.target, test_size=0.25, random_state=None)
    
        # TASK: Build a vectorizer / classifier pipeline that filters out tokens
        # that are too rare or too frequent
        pipeline = Pipeline([
            ('vect', TfidfVectorizer(min_df=3, max_df=0.95)),
            ('clf', LinearSVC(C=1000)),
        ])
    
        # TASK: Build a grid search to find out whether unigrams or bigrams are
        # more useful.
        # Fit the pipeline on the training set using grid search for the parameters
        parameters = {
            'vect__ngram_range': [(1, 1), (1, 2)],
        }
        grid_search = GridSearchCV(pipeline, parameters, n_jobs=-1)
        grid_search.fit(docs_train, y_train)
    
        # TASK: print the mean and std for each candidate along with the parameter
        # settings for all the candidates explored by grid search.
        n_candidates = len(grid_search.cv_results_['params'])
        for i in range(n_candidates):
            print(i, 'params - %s; mean - %0.2f; std - %0.2f'
                     % (grid_search.cv_results_['params'][i],
                        grid_search.cv_results_['mean_test_score'][i],
                        grid_search.cv_results_['std_test_score'][i]))
    
        # TASK: Predict the outcome on the testing set and store it in a variable
        # named y_predicted
        y_predicted = grid_search.predict(docs_test)
    
        # Print the classification report
        print(metrics.classification_report(y_test, y_predicted,
                                            target_names=dataset.target_names))
    
        # Print and plot the confusion matrix
        cm = metrics.confusion_matrix(y_test, y_predicted)
        print(cm)
    
        # import matplotlib.pyplot as plt
        # plt.matshow(cm)
        # plt.show()

    Where to from here

    Here are a few suggestions to help further your scikit-learn intuition upon the completion of this tutorial:

  • 相关阅读:
    分布式版本控制系统Git的安装与使用
    随笔--第一篇博客
    字符串、文件操作,英文词频统计预处理
    了解大数据的特点、来源与数据呈现方式
    第五次作业-结对项目四则运算 “软件”之升级版
    第四次作业:个人项目-小学四则运算 “软件”之初版
    读《构建之法》1-5章有感
    第二次作业——分布式版本控制系统Git的安装与使用
    第一次作业——感想
    【大数据】字符串、文件操作,英文词频统计预处理
  • 原文地址:https://www.cnblogs.com/lightsong/p/14278225.html
Copyright © 2020-2023  润新知