• 使用sklearn对文档进行向量化&使用sklearn进行量纲缩放


     1 """
     2 演示内容:文档的向量化
     3 """
     4 from sklearn.feature_extraction.text import CountVectorizer
     5 corpus = [
     6 'Jobs was the chairman of Apple Inc., and he was very famous',
     7 'I like to use apple computer',
     8 'And I also like to eat apple'
     9 ] 
    10  
    11 #未经停用词过滤的文档向量化
    12 vectorizer =CountVectorizer()
    13 print(vectorizer.fit_transform(corpus).todense())  #转化为完整特征矩阵
    14 print(vectorizer.vocabulary_)
    15 print(" ")
    16  
    17 #经过停用词过滤后的文档向量化
    18 import nltk
    19 nltk.download('stopwords')
    20 stopwords = nltk.corpus.stopwords.words('english')
    21 print (stopwords)
    22 
    23 print(" ")
    24 vectorizer =CountVectorizer(stop_words='english')
    25 print("after stopwords removal:
    ", vectorizer.fit_transform(corpus).todense())
    26 print("after stopwords removal:
    ", vectorizer.vocabulary_)
    27  
    28 print(" ")
    29 #采用ngram模式进行文档向量化
    30 vectorizer =CountVectorizer(ngram_range=(1,2))    #表示从1-2,既包括unigram,也包括bigram
    31 print("N-gram mode:
    ",vectorizer.fit_transform(corpus).todense())  #转化为完整特征矩阵
    32 print(" ")
    33 print("N-gram mode:
    ",vectorizer.vocabulary_)

     未经停用词过滤的文档向量化:

    所有的停用词:

    经过停用词过滤的文档向量化:

    采用n-gram模式进行文档向量化:

     1 """
     2 演示内容:量纲的特征缩放
     3 (两种方法:标准化缩放法和区间缩放法。每种方法举了两个例子:简单二维矩阵和iris数据集)
     4 """
     5 #方法1:标准化缩放法 例1:对简单示例二维矩阵的列数据进行
     6 from sklearn import preprocessing   
     7 import numpy as np  
     8 #采用numpy的array表示,因为要用到其mean等函数,而list没有这些函数
     9 X = np.array([[0, 0], 
    10         [0, 0], 
    11         [100, 1], 
    12         [1, 1]])  
    13 
    14 X_mean = X.mean(axis=0)     #均值
    15 X_std = X.std(axis=0)       #方差
    16 X1 = (X-X_mean)/X_std       #对x进行标准化
    17 print (X1)
    18 print ("")
    19  
    20 #也可以用preprocessing来标准化X,和上面的结果一致  
    21 X_scale = preprocessing.scale(X)  
    22 print (X_scale)
    23 
    24  
    25 #方法1: 标准化缩放法 例2:对iris数据二维矩阵的列数据进行。这次采用一个集成的方法StandardScaler
    26 from sklearn import datasets
    27 iris = datasets.load_iris()
    28 X_scale = preprocessing.scale(iris.data)  
    29 print (X_scale)
    30  
    31 
    32 #方法2: 区间缩放法 例3:对简单示例二维矩阵的列数据进行
    33 from sklearn.preprocessing import MinMaxScaler
    34  
    35 data = [[0, 0], 
    36         [0, 0], 
    37         [100, 1], 
    38         [1, 1]]
    39  
    40 scaler = MinMaxScaler()
    41 print(scaler.fit(data))
    42 print(scaler.transform(data))
    43  
    44 #方法2: 区间缩放法 例4:对iris数据二维矩阵的列数据进行
    45 from sklearn.preprocessing import MinMaxScaler
    46 data = iris.data
    47  
    48 scaler = MinMaxScaler()
    49 print(scaler.fit(data))
    50 print(scaler.transform(data))

     对二维矩阵使用标准化缩放法:

    对二维矩阵使用区间缩放法:

  • 相关阅读:
    总公司路由排错?
    黄永成-thinkphp讲解-个人博客讲解26集
    fedora配置网络
    只有文本编辑器才是王道, 什么ide都是evil的浮云, 看看linus linux的内核开发工具vim emacs
    痛苦的事, 伤心的事, 一生只需要一次, 别折腾, 别忘痛- 人生, 软件, 所有人皆如是!
    我再也不-或许永远不-用zend studio-受够了!
    linux安装-版本选择-终极决定
    黄永成-thinkphp讲解-个人博客讲解25集
    linux的多媒体 播放 软件版权问题
    如何解决winows启动后出现grub?
  • 原文地址:https://www.cnblogs.com/cxq1126/p/13025452.html
Copyright © 2020-2023  润新知