zoukankan      html  css  js  c++  java
  • TFIDF练习

    直接上代码吧:

     1 """
     2     测试Demo
     3 """
     4 import lightgbm as lgb
     5 import numpy as np
     6 from sklearn.feature_extraction.text import TfidfVectorizer
     7 from sklearn.feature_extraction.text import CountVectorizer
     8 
     9 
    10 def use_lgb():
    11     # 训练数据,500个样本,10个维度
    12     train_data = np.random.rand(500, 10)
    13     # 构建二分类数据
    14     label = np.random.randint(2, size=500)
    15     # 放入到dataset中
    16     train = lgb.Dataset(train_data, label=label)
    17     print(train)
    18 
    19 
    20 def use_tfidf():
    21     sentence = ['没有 你 的 地方 都是 他乡', '没有 你 的 旅行 都是 流浪']
    22     # 不去掉停用词
    23     c = CountVectorizer(stop_words=None)
    24 
    25     # 拟合模型返回文本矩阵
    26     count_word_tf = c.fit_transform(sentence)
    27 
    28     # print(count_word_tf.toarray())
    29     # # 查看那些词,以字典的形式
    30     # print(c.vocabulary_)
    31     # # 得到特征
    32     # print(c.get_feature_names())
    33 
    34 
    35 ###############################
    36     stopword = ['都是']
    37     # 构建一个tfidf向量器,去除停用词
    38     tfidf = TfidfVectorizer(stop_words=stopword)
    39 
    40     # 给出tfidf的权重,将tfidf矩阵抽取出来
    41     weight = tfidf.fit_transform(sentence).toarray()
    42     # 给出特征名称
    43     word = tfidf.get_feature_names()
    44 
    45     print("有哪些词:")
    46     print(word)
    47 
    48     print("
    词汇表以及他们的位置索引:")
    49     for key, value in tfidf.vocabulary_.items():
    50         print(key, value)
    51 
    52     print("
    词频矩阵:")
    53     print(weight)
    54     print(len(weight))
    55 
    56     # 打印每类文本中的tfidf权重,第一个for变量所有样本,第二个for遍历某一类文档下的所有权重
    57     for i in range(len(weight)):
    58         print("这里输出的是第{}文本的词语tfidf权重".format(i))
    59         for j in range(len(word)):
    60             # 经过tfidf后,找出每篇文档相关的词,这些词就是精心挑选出来的。然后根据这些词到文档中去找到tfidf值
    61             print(word[j], weight[i][j])
    62 
    63 
    64 if __name__ == '__main__':
    65     use_tfidf()

    输出:

     1 有哪些词:
     2 ['他乡', '地方', '旅行', '没有', '流浪']
     3 
     4 词汇表以及他们的位置索引:
     5 他乡 0
     6 旅行 2
     7 流浪 4
     8 地方 1
     9 没有 3
    10 
    11 词频矩阵:
    12 [[0.6316672  0.6316672  0.         0.44943642 0.        ]
    13  [0.         0.         0.6316672  0.44943642 0.6316672 ]]
    14 2
    15 这里输出的是第0文本的词语tfidf权重
    16 他乡 0.6316672017376245
    17 地方 0.6316672017376245
    18 旅行 0.0
    19 没有 0.4494364165239821
    20 流浪 0.0
    21 这里输出的是第1文本的词语tfidf权重
    22 他乡 0.0
    23 地方 0.0
    24 旅行 0.6316672017376245
    25 没有 0.4494364165239821
    26 流浪 0.6316672017376245

    本文参考:https://blog.csdn.net/the_lastest/article/details/79093407

  • 相关阅读:
    mysql清空表中内容
    Proteus元件查找
    HDG12864F1 proteus仿真取模【PCtoLCD完美版】
    OLED取模(汉字加图片)
    Failed to connect to ESP8266: Timed out waiting for packet header
    AD常用快捷键
    Authentication method 'caching_sha2_password' not supported by any of the available plugins.
    spark阶段学习总结(三)
    spark阶段学习总结(一)
    spark阶段学习总结(二)
  • 原文地址:https://www.cnblogs.com/demo-deng/p/9622221.html
Copyright © 2011-2022 走看看