使用sklearn CountVectorizer 实现n-gram

2024-06-04 12:48

本文主要是介绍使用sklearn CountVectorizer 实现n-gram,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

#coding=utf-8
'''
Created on 2018-1-25'''from sklearn.feature_extraction.text import CountVectorizertext = ["A smile is the most charming part of a person forever.","A smile is"]# ngram_range=(2, 2)表明适应2-gram,decode_error="ignore"忽略异常字符,token_pattern按照单词切割
ngram_vectorizer = CountVectorizer(ngram_range=(2, 2), decode_error="ignore",token_pattern = r'\b\w+\b',min_df=1)x1 = ngram_vectorizer.fit_transform(text)
print x1
# (0, 7)    1
# (0, 0)    1
# (0, 5)    1
# (0, 6)    1
# (0, 2)    1
# (0, 4)    1
# (0, 9)    1
# (0, 3)    1
# (0, 8)    1
# (0, 1)    1
# (1, 8)    1
# (1, 1)    1
print x1.toarray()
# [[1 1 1 1 1 1 1 1 1 1]
#  [0 1 0 0 0 0 0 0 1 0]]
# 查看生成的词表
print ngram_vectorizer.vocabulary_
# {u'person forever': 7, u'part of': 6, u'smile is': 8, u'a smile': 1, u'of a': 5, u'the most': 9, u'is the': 3, u'charming part': 2, u'a person': 0, u'most charming': 4}# 如果ngram_range=(2, 4),则表示2,3,4个单词切割
ngram_vectorizer = CountVectorizer(ngram_range=(2, 4), decode_error="ignore",token_pattern = r'\b\w+\b',min_df=1)
x1 = ngram_vectorizer.fit_transform(text)
print x1
# (0, 16)    1
# (0, 19)    1
# (0, 7)    1
# (0, 13)    1
# (0, 26)    1
# (0, 10)    1
# (0, 23)    1
# (0, 4)    1
# (0, 1)    1
# (0, 15)    1
# (0, 18)    1
# (0, 6)    1
# (0, 12)    1
# (0, 25)    1
# (0, 9)    1
# (0, 22)    1
# (0, 3)    1
# (0, 20)    1
# (0, 0)    1
# (0, 14)    1
# (0, 17)    1
# (0, 5)    1
# (0, 11)    1
# (0, 24)    1
# (0, 8)    1
# (0, 21)    1
# (0, 2)    1
# (1, 3)    1
# (1, 21)    1
# (1, 2)    1
print ngram_vectorizer.vocabulary_
# {u'smile is': 21, u'charming part of a': 7, u'a smile': 2, u'part of': 17, u'is the most charming': 10, u'the most': 24, u'of a person forever': 16, u'the most charming': 25, u'most charming part': 12, u'is the': 8, u'charming part': 5, u'most charming': 11, u'part of a': 18, u'smile is the most': 23, u'person forever': 20, u'is the most': 9, u'most charming part of': 13, u'of a': 14, u'smile is the': 22, u'charming part of': 6, u'a person forever': 1, u'the most charming part': 26, u'a smile is the': 4, u'part of a person': 19, u'a smile is': 3, u'a person': 0, u'of a person': 15}

这篇关于使用sklearn CountVectorizer 实现n-gram的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1030130

相关文章

mysql递归查询语法WITH RECURSIVE的使用

《mysql递归查询语法WITHRECURSIVE的使用》本文主要介绍了mysql递归查询语法WITHRECURSIVE的使用,WITHRECURSIVE用于执行递归查询,特别适合处理层级结构或递归... 目录基本语法结构:关键部分解析:递归查询的工作流程:示例:员工与经理的层级关系解释:示例:树形结构的数

Redis中RedisSearch使用及应用场景

《Redis中RedisSearch使用及应用场景》RedisSearch是一个强大的全文搜索和索引模块,可以为Redis添加高效的搜索功能,下面就来介绍一下RedisSearch使用及应用场景,感兴... 目录1. RedisSearch的基本概念2. RedisSearch的核心功能(1) 创建索引(2

Redis中HyperLogLog的使用小结

《Redis中HyperLogLog的使用小结》Redis的HyperLogLog是一种概率性数据结构,用于统计唯一元素的数量(基数),本文主要介绍了Redis中HyperLogLog的使用小结,感兴... 目录 一、HyperlogLog 是什么?️ 二、使用方法1. 添加数据2. 查询基数China编程3.

Linux系统调试之ltrace工具使用与调试过程

《Linux系统调试之ltrace工具使用与调试过程》:本文主要介绍Linux系统调试之ltrace工具使用与调试过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、ltrace 定义与作用二、ltrace 工作原理1. 劫持进程的 PLT/GOT 表2. 重定

Python实现剪贴板历史管理器

《Python实现剪贴板历史管理器》在日常工作和编程中,剪贴板是我们使用最频繁的功能之一,本文将介绍如何使用Python和PyQt5开发一个功能强大的剪贴板历史管理器,感兴趣的可以了解下... 目录一、概述:为什么需要剪贴板历史管理二、功能特性全解析2.1 核心功能2.2 增强功能三、效果展示3.1 主界面

Springboot实现推荐系统的协同过滤算法

《Springboot实现推荐系统的协同过滤算法》协同过滤算法是一种在推荐系统中广泛使用的算法,用于预测用户对物品(如商品、电影、音乐等)的偏好,从而实现个性化推荐,下面给大家介绍Springboot... 目录前言基本原理 算法分类 计算方法应用场景 代码实现 前言协同过滤算法(Collaborativ

POI从入门到实战轻松完成EasyExcel使用及Excel导入导出功能

《POI从入门到实战轻松完成EasyExcel使用及Excel导入导出功能》ApachePOI是一个流行的Java库,用于处理MicrosoftOffice格式文件,提供丰富API来创建、读取和修改O... 目录前言:Apache POIEasyPoiEasyExcel一、EasyExcel1.1、核心特性

Java 如何创建和使用ExecutorService

《Java如何创建和使用ExecutorService》ExecutorService是Java中用来管理和执行多线程任务的一种高级工具,可以有效地管理线程的生命周期和任务的执行过程,特别是在需要处... 目录一、什么是ExecutorService?二、ExecutorService的核心功能三、如何创建

springboot实现配置文件关键信息加解密

《springboot实现配置文件关键信息加解密》在项目配置文件中常常会配置如数据库连接信息,redis连接信息等,连接密码明文配置在配置文件中会很不安全,所以本文就来聊聊如何使用springboot... 目录前言方案实践1、第一种方案2、第二种方案前言在项目配置文件中常常会配置如数据库连接信息、Red

Python+Tkinter实现Windows Hosts文件编辑管理工具

《Python+Tkinter实现WindowsHosts文件编辑管理工具》在日常开发和网络调试或科学上网场景中,Hosts文件修改是每个开发者都绕不开的必修课,本文将完整解析一个基于Python... 目录一、前言:为什么我们需要专业的Hosts管理工具二、工具核心功能全景图2.1 基础功能模块2.2 进