您现在的位置是:热点 >>正文
关于自然语言处理之One Hot模型
热点942人已围观
简介...
最近工作实在有点忙,前阵子关于梯度和导数的事情把人折腾的够呛,数学学不好,搞机器学习和神经网络真是头疼;想转到应用层面轻松一下,想到了自然语言处理,one hot模型是基础也是入门,看起来很简单的一个列表转矩阵、词典的功能,想着手工实现一下,结果看了一下CountVectorizer,发现不是那么回事儿,还是放弃了。
顾名思义,单热表示从一个零向量开始,如果单词出现在句子或文档中,则将向量中的相应条目设置为 1。
对句子进行标记,忽略标点符号,并将所有的单词都用小写字母表示,就会得到一个大小为 8 的词汇表: {time, fruit, flies, like, a, an, arrow, banana} 。所以,我们可以用一个八维的单热向量来表示每个单词。在本书中,我们使用 1[w] 表示标记/单词 w 的单热表示。
对于短语、句子或文档,压缩的单热表示仅仅是其组成词的逻辑或的单热表示。短语 like a banana 的单热表示将是一个 3×8 矩阵,其中的列是 8 维的单热向量。通常还会看到“折叠”或二进制编码,其中文本/短语由词汇表长度的向量表示,用 0 和 1 表示单词的缺失或存在。like a banana 的二进制编码是: [0,0,关于0,1,1,0,0,1] 。
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer import seaborn as sns import matplotlib.pyplot as plt import jieba import jieba.analyse # 单热表示从一个零向量开始,如果单词出现在句子或文档中,则将向量中的相应条目设置为 1。 # 英文的处理和展示 corpus = ['Time flies flies like an arrow.', 'Fruit flies like a banana.'] one_hot_vectorize自然r = CountVectorizer(binary=True) one_hot = one_hot_vectorizer.fit_transform(corpus).toarray() sns.heatmap(one_hot, annot=True, cbar=False, yticklabels=['Sentence 1', 'Sentence 2']) plt.show()
# 中文的处理和展示 # 获取停用词列表 def get_stopwords_list(stopwordfile): stopwords = [line.strip() for line in open(stopwordfile, encoding='UTF-8').readlines()] &nbs语言p; return stopwords # 移除停用词 def movestopwords(sentence): stopwords = get_stopwords_list('stopwords.txt') # 这里加载停用词的路径 santi_words = [x for x in sentence if len(x) > 1 and x not in stopwords] return santi_words # 语料 corpus = ["小明硕士毕业于中国科学处理院计算所,后在日本京都大学深造。", "小王本科在清华大学,后在南京计算所工作和深造,后在日本早稻田大学深造", "小李本科在清华大学,硕士毕业于中国科学院计算所,博士在南京大学"] newcorpus = [] for str in corpus: orgwordlist = jieba.lcut(str) # jieba分词 wordlist = movestopwords(orgwordlist) # 移除停用词 &之Onbsp;newword = " ".join(wordlist) # 按照语料库要求进行空格分隔 newcorpus.append(newword) # 按照语料库要求转换成列表 # newcorpus # ['小明 硕士 毕业 中国科学院 计算所 日本京都大学 深造', # '小王 本科 清华大学 南京 计算所 工作 深造 日本早稻田大学 深造', # '小李 本科 清华大学 硕士 毕业 中国科学院 计算所 博士 南京大学'] one_hot_vectorizer = CountVectorizer(binary=True)&nnebsp; # 创建词袋数据结构 one_hot = one_hot_vectorizer.fit_transform(newcorpus).toarray() # 转换语料,并矩阵化 # 下面为热词的输出结果 # one_hot_vectorizer.vocabulary_ # {'小明': 4, '˶ʿ': 14, '毕业': 11, '中国科学院': 0, '计算所': 15, '日本京都大学': 8, '深造': 12, '小王': 6, '本科': 10, '清华大学': 13, '南京': 1, '工作': 7, '日本早稻田大学': 9, '小李': 5, '博士': H 3, '南京大学': 2} # one_hot_vectorizer.get_feature_names() # ['中国科学院', '南京', '南京大学', '博士', '小明', '小李', '小王', '工作', '日本京都大学', '日本早稻田大学', '本科', '毕业', '深造', '清华大学', '˶ʿ', '计算所'] # one_hot # [[1 0 0 0 1 0 0 0 1 0 0 1 1 0 1 1] # [0 1 0 0 0 0 1 1 0 1 ot;1 0 1 1 0 1] # [1 0 1 1 0 1 0 0 0 0 1 1 0 1 1 1]] sns.set_style({'font.sans-serif': ['SimHei', 'Arial']}) sns.heatmap(one_hot, annot=True, cbar=False, xticklabels=one_hot_vectorizer.get_feature_names(), yticklabels=['语句1', '语句2', '语句3']) plt.show()
IDF 表示惩模型罚常见的符号,并奖励向量表示中的罕见符号。符号 w 的 IDF(w) 对语料库的定义为其中 n[w] 是包含单词 w 的文档数量, N 是文档总数。TF-IDF 分数就是 TF(w) * IDF(w) 的乘积。首先,请注意在所有文档(例如, n[w] = N ), IDF(w) 为 0, TF-IDF 得分为 0,完全惩罚了这一项。其次,如果一个术语很少出现(可能只出现在一个文档中),那么 IDF 就是 log n 的最大值
tfidf_vectorizer = TfidfVectorizer() # 创建词袋数据结构 tfidf = tfidf_vectorizer.fit_transform(newcorpus).toarray() # 转换语料,并矩阵化 # 下面为热词的输出结果 # tfidf_vectorizer.vocabulary_ # '小明': 4, '˶ʿ': 14, '毕业': 11, '中国科学院': 0,关于 '计算所': 15, '日本京都大学': 8, '深造': 12, '小王': 6, '本科': 10, '清华大学': 13, '南京': 1, '工作': 7, '日本早稻田大学': 9, '小李': 5, '博士': 3, '南京大学': 2} # tfidf_vectorizer.get_feature_names() # ['中国科学院', '南京', '南京大学', '博士', '小明', '小李', '小王', '工作', '日本京都大学', '日本早稻田大学', '本科', '毕业', '深造', '清华大学', '˶ʿ', '计算所'] # 自然tfidf # [[0.35221512 0. 0. 0. 0.46312056 0. 0. 0. 0.46312056 0. 0. 0.35221512 0.35221512 0.&n语言bsp; 0.35221512 0.27352646] # [0. 0.35761701 0. 0. 0. 0. 0.35761701 0.35761701 0. 0.35761701 0.27197695 0. 0.54395391 0.27197处理695 0. 0.21121437] # [0.30443385 0. 0.40029393 0.40029393 0. 0.40029393 0. 0. 0. 0. 0.30443385之O 0.30443385 0. 0.30443385 0.30443385 0.23642005]] sns.heatmap(tfidf, annot=True, cbar=False, xticklabels=tfidf_vectorizer.get_feature_names(), yticklabels=['语句1', '语句2', '语句3'], vmin=0, vmax=1, cm
Tags:
相关文章
深信服:与中国电信签订战略合作协议 拟5亿元参与其战略配售
热点8月11日消息,深信服对外发布公告称,公司以自有资金参与中国电信的战略配售,获配金额为5亿元;公司与中国电信签订战略合作协议,双方将在云计算、网络安全等业务领域就资本、技术、资源等方面加强合作。 中国...
【热点】
阅读更多标签: 小米17 Ultra 保时捷 比亚迪 龙哥讲电车
热点Hello,大家早上好,又是元气满满的一天,先来浏览新鲜的早报吧~ 【小米 17 Ultra 发布】 小米 17 Ultra 手机 12 月 25 日晚正式发布。该机搭载徕卡 1 英寸光影大师主摄、徕...
【热点】
阅读更多英伟达收购Arm再有变数?高通总裁:若失败,愿接盘
热点新浪科技讯 北京时间6月15日早间消息,据报道,美国芯片巨头高通(Qualcom)近日向外界表示,如果英伟达(Nvidia)对英国芯片设计公司Arm的400亿美元收购案被监管机构叫停,那么它将对投资A...
【热点】
阅读更多
热门文章
最新文章
友情链接
- 标签: 康师傅
- https://www.impactpool.org/search/?type=79&q=谷歌站群软件【tg:seogoogle987】6I2
- https://www.military-history.org/?s=谷歌站群软件【tg:seogoogle987】,oHK
- https://www.maine.gov/pfr/professionallicensing/search/node?keys=谷歌站群软件【tg:seogoogle987】,jwb
- https://yscc1986.net/search/谷歌站群软件【tg:seogoogle987】U8L
- https://www.mi.com/search?keyword=谷歌站群软件【tg:seogoogle987】,2Ca
- https://www.xbiao.com/search/index?tpf&wd=谷歌站群软件【tg:seogoogle987】,YDs
- https://www.remit.co.jp/zh/?s=谷歌站群软件【tg:seogoogle987】,IDJ
- https://www.goodreads.com/quotes/tag/谷歌站群软件【tg:seogoogle987】ixH
- https://www.goodreads.com/quotes/tag/谷歌站群软件【tg:seogoogle987】B5O
- https://www.sonepar.ch/fr/search?q=谷歌站群软件【tg:seogoogle987】,v3D
- https://www.ons.gov.uk/search?q=谷歌站群软件【tg:seogoogle987】,eHh
- https://www.takachi-enclosure.com/search?eqYB=gnI31Ou&text=谷歌站群软件【tg:seogoogle987】,CF9
- https://www.kawainter.com/catalogsearch/result/?q=谷歌站群软件【tg:seogoogle987】,Yks
- https://www.normani.de/search?q=谷歌站群软件【tg:seogoogle987】,8G7
- https://xn--lgernevedlystskoven-lxb.dk/search/谷歌站群软件【tg:seogoogle987】,8Xq
- https://www.lauben.com/en/?action=SEARCH&s=谷歌站群软件【tg:seogoogle987】,ou0
- https://www.revere.org/search?action=SEARCH&q=谷歌站群软件【tg:seogoogle987】,hqS
- https://store.line.me/search/sticker/ja?q=谷歌站群软件【tg:seogoogle987】1h4
- https://www.parriscues.com/catalogsearch/result/?q=谷歌站群软件【tg:seogoogle987】,UJv
- https://www.zisha.com/shop/list.shtml?wap_keyword=谷歌站群软件【tg:seogoogle987】,goR
- https://www.world-architects.com/en/search?term=谷歌站群软件【tg:seogoogle987】,jLZ
- https://www.nslcleaders.org/?s=谷歌站群软件【tg:seogoogle987】,fuq
- https://www.southmeadows.co.uk/2019/07/?s=谷歌站群软件【tg:seogoogle987】,OrJ
- https://www.remit.co.jp/pt/?action=CHECK&s=谷歌站群软件【tg:seogoogle987】,4Xt
- https://www.ushmm.org/search/results.php?q=谷歌站群软件【tg:seogoogle987】,v5P
- https://www.teamlab.art/es/?submit=谷歌站群软件【tg:seogoogle987】,zLS
- https://www.ushmm.org/search/results/?ID=8s0587Oi8UTv74zPPM4fuhwjNpe2hsEE&q=谷歌站群软件【tg:seogoogle987】,ZzP
- https://www.universal-music.co.jp/search/?q=谷歌站群软件【tg:seogoogle987】,Zmi
- https://www.springfieldlibrary.org/library/?s=谷歌站群软件【tg:seogoogle987】,ana
- https://www.flickr.com/photos/tags/谷歌站群软件【tg:seogoogle987】OFr
- https://www.kubb-tool.bfs.admin.ch/en/search?action=CHECK&q=谷歌站群软件【tg:seogoogle987】,MBS
- https://www.youthforum.org/search-results?cx=4f5236218ab5e8ef9&ie=UTF-8&q=谷歌站群软件【tg:seogoogle987】,zIK
- https://www.ntv.co.jp/3min/search/?action=QUERY&q=谷歌站群软件【tg:seogoogle987】,362
- https://www.openstreetmap.org/search?query=谷歌站群软件【tg:seogoogle987】,XTC
- https://www.remit.co.jp/vi/?s=谷歌站群软件【tg:seogoogle987】Rmf
- https://5gaa.org/?token=6e2l7hjt2i26gzgctgwdrv7v0j6nxorq&s=谷歌站群软件【tg:seogoogle987】Wzk
- https://www.nissin.com/jp/search/?cat=105&q=谷歌站群软件【tg:seogoogle987】,WEV
- https://www.totolink.vn/search/谷歌站群软件【tg:seogoogle987】,MI2
- https://www.remit.co.jp/en/?s=谷歌站群软件【tg:seogoogle987】,wUn
- https://www.maigoo.com/search/?block=&q=谷歌站群软件【tg:seogoogle987】,Iv9
- https://www.valuecolleges.com/?action=QUERY&s=谷歌站群软件【tg:seogoogle987】,azS
- https://www.krisinformation.se/en/search?q=谷歌站群软件【tg:seogoogle987】,7Oy
- https://www.so.com/s?ie=utf-8&src=360sou_home&_re=0&q=谷歌站群软件【tg:seogoogle987】,9zi
- https://www.rea.govt.nz/search?searchterm=谷歌站群软件【tg:seogoogle987】,2aO
- https://www.nbm.org/?action=QUERY&s=谷歌站群软件【tg:seogoogle987】,rJg
- https://www.mexperience.com/?T=1686132112324&s=谷歌站群软件【tg:seogoogle987】,azu
- https://www.temu.com/search_result.html?search_key=谷歌站群软件【tg:seogoogle987】CMl
- https://www.varian.com/en-au/search?cat=113&search=谷歌站群软件【tg:seogoogle987】,M3l
- https://www.synonyms.com/antonyms/谷歌站群软件【tg:seogoogle987】,ww1
- https://www.yidian51.com/taobao/谷歌站群软件【tg:seogoogle987】,x1x