git: https://github.com/linyi0604/MachineLearning

分别使用词袋法和nltk自然预言处理包提供的文本特征提取
 from sklearn.feature_extraction.text import CountVectorizer
import nltk
# nltk.download("punkt")
# nltk.download('averaged_perceptron_tagger') '''
分别使用词袋法和nltk自然预言处理包提供的文本特征提取
''' sent1 = "The cat is walking in the bedroom."
sent2 = "A dog was running across the kitchen."
# 使用词袋法 将文本转化为特征向量
count_vec = CountVectorizer()
sentences = [sent1, sent2]
# 输出转化后的特征向量
# print(count_vec.fit_transform(sentences).toarray())
'''
[[0 1 1 0 1 1 0 0 2 1 0]
[1 0 0 1 0 0 1 1 1 0 1]]
'''
# 输出转化后特征的含义
# print(count_vec.get_feature_names())
'''
['across', 'bedroom', 'cat', 'dog', 'in', 'is', 'kitchen', 'running', 'the', 'walking', 'was']
''' # 使用nltk对文本进行语言分析
# 对句子词汇分割和正则化 把aren't 分割成 are 和 n't I'm 分割成 I和'm
tokens1 = nltk.word_tokenize(sent1)
tokens2 = nltk.word_tokenize(sent2)
# print(tokens1)
# print(tokens2)
'''
['The', 'cat', 'is', 'walking', 'in', 'the', 'bedroom', '.']
['A', 'dog', 'was', 'running', 'across', 'the', 'kitchen', '.']
'''
# 整理词汇表 按照ASCII的顺序排序
vocab_1 = sorted(set(tokens1))
vocab_2 = sorted(set(tokens2))
# print(vocab_1)
# print(vocab_2)
'''
['.', 'The', 'bedroom', 'cat', 'in', 'is', 'the', 'walking']
['.', 'A', 'across', 'dog', 'kitchen', 'running', 'the', 'was']
'''
# 初始化stemer 寻找每个单词最原始的词根
stemmer = nltk.stem.PorterStemmer()
stem_1 = [stemmer.stem(t) for t in tokens1]
stem_2 = [stemmer.stem(t) for t in tokens2]
# print(stem_1)
# print(stem_2)
'''
['the', 'cat', 'is', 'walk', 'in', 'the', 'bedroom', '.']
['A', 'dog', 'wa', 'run', 'across', 'the', 'kitchen', '.']
'''
# 利用词性标注器 对词性进行标注
pos_tag_1 = nltk.tag.pos_tag(tokens1)
pos_tag_2 = nltk.tag.pos_tag(tokens2)
# print(pos_tag_1)
# print(pos_tag_2)
'''
[('The', 'DT'), ('cat', 'NN'), ('is', 'VBZ'), ('walking', 'VBG'), ('in', 'IN'), ('the', 'DT'), ('bedroom', 'NN'), ('.', '.')]
[('A', 'DT'), ('dog', 'NN'), ('was', 'VBD'), ('running', 'VBG'), ('across', 'IN'), ('the', 'DT'), ('kitchen', 'NN'), ('.', '.')]
'''

机器学习之路: python nltk 文本特征提取的更多相关文章

  1. 机器学习之路: python k近邻分类器 KNeighborsClassifier 鸢尾花分类预测

    使用python语言 学习k近邻分类器的api 欢迎来到我的git查看源代码: https://github.com/linyi0604/MachineLearning from sklearn.da ...

  2. 机器学习之路--Python

    常用数据结构 1.list 列表 有序集合 classmates = ['Michael', 'Bob', 'Tracy'] len(classmates) classmates[0] len(cla ...

  3. 机器学习之路: python 回归树 DecisionTreeRegressor 预测波士顿房价

    python3 学习api的使用 git: https://github.com/linyi0604/MachineLearning 代码: from sklearn.datasets import ...

  4. 机器学习之路: python 线性回归LinearRegression, 随机参数回归SGDRegressor 预测波士顿房价

    python3学习使用api 线性回归,和 随机参数回归 git: https://github.com/linyi0604/MachineLearning from sklearn.datasets ...

  5. 机器学习之路: python 决策树分类DecisionTreeClassifier 预测泰坦尼克号乘客是否幸存

    使用python3 学习了决策树分类器的api 涉及到 特征的提取,数据类型保留,分类类型抽取出来新的类型 需要网上下载数据集,我把他们下载到了本地, 可以到我的git下载代码和数据集: https: ...

  6. python —— 文本特征提取 CountVectorize

    CountVectorize 来自:python学习 文本特征提取(二) CountVectorizer TfidfVectorizer 中文处理 - CSDN博客 https://blog.csdn ...

  7. 机器学习之路:python 文本特征提取 CountVectorizer, TfidfVectorizer

    本特征提取: 将文本数据转化成特征向量的过程 比较常用的文本特征表示法为词袋法词袋法: 不考虑词语出现的顺序,每个出现过的词汇单独作为一列特征 这些不重复的特征词汇集合为词表 每一个文本都可以在很长的 ...

  8. 【NLP】干货!Python NLTK结合stanford NLP工具包进行文本处理

    干货!详述Python NLTK下如何使用stanford NLP工具包 作者:白宁超 2016年11月6日19:28:43 摘要:NLTK是由宾夕法尼亚大学计算机和信息科学使用python语言实现的 ...

  9. 【NLP】Python NLTK处理原始文本

    Python NLTK 处理原始文本 作者:白宁超 2016年11月8日22:45:44 摘要:NLTK是由宾夕法尼亚大学计算机和信息科学使用python语言实现的一种自然语言工具包,其收集的大量公开 ...

随机推荐

  1. exec操作文件描述符

    exec命令可以用来替代当前shell:换句话说,并没有启动子shell.使用这一命令时任何环境都将被清除,并重新启动一个shell. 它的一半形式为: exec command 其中,command ...

  2. Python练习-一个简单的生成器

    今天我们学习了生成器,怎么理解生成器呢,其实就是使用函数的方式自己建立一个迭代器 # 编辑者:闫龙 #做一个简单的生成器 def EasyGene(*args): #建立一个生成器方法并传递多个参数 ...

  3. 新手向-同步关键字synchronized对this、class、object、方法的区别

    synchronized的语义 实验 分析 在看源代码时遇到多线程需要同步的时候,总是会看见几种写法,修饰方法.修饰静态方法.synchronized(Xxx.class).synchronized( ...

  4. Wood Cut

    Given n pieces of wood with length L[i] (integer array). Cut them into small pieces to guarantee you ...

  5. Linux Module框架【转】

    转自:http://www.cnblogs.com/LittleHann/p/4558719.html catalog 1. 概述 2. 使用模块 3. 插入和删除模块 4. 自动化与热插拔 5. 版 ...

  6. ubuntu git 简单入门【转】

    转自:http://blog.chinaunix.net/uid-20718384-id-3334859.html 1. 安装 sudo apt-get install git-core 2.  初始 ...

  7. python 中的__del__

    # -*- coding: utf-8 -*- # @Time : 2018/9/19 20:21 # @Author : cxa # @File : delDemo.py # @Software: ...

  8. poj1976

    dp #include <cstdio> #include <cstring> #include <algorithm> using namespace std; ...

  9. 两行代码搞定js对象深浅拷贝

    有一段时间没有更新博客了,忙于工作.2018年刚过去,今天来开启2018第一篇博文.好了,咱们步入正题. 先上代码 /** * 遍历对象 * 1.判断是不是原始值 * 2.判断是数组还是对象 * 3. ...

  10. java基础42 File类的构造函数及其方法

    本文知识点(目录): 1.IO流(Input Output)    2.IO流技术的运用场景    3.File类的构造函数(方法)    4.File类的一些常用方法    5.实例(解析File类 ...