专栏名称: Python开发者
人生苦短,我用 Python。伯乐在线旗下账号「Python开发者」分享 Python 相关的技术文章、工具资源、精选课程、热点资讯等。
目录
相关文章推荐
Python初级入门到精通  ·  Python-finally语句与应用 ·  9 小时前  
Python爱好者社区  ·  太炸裂了!亚马逊云来了 ·  昨天  
Python爱好者社区  ·  全球首个满级QQ正式诞生!满级后长这样 ·  昨天  
Python大全  ·  16个必知必会的Python技能! ·  昨天  
Python大全  ·  用Python实现智能乒乓球游戏! ·  2 天前  
Python大全  ·  用Python实现智能乒乓球游戏! ·  2 天前  
51好读  ›  专栏  ›  Python开发者

用神经网络训练一个文本分类器

Python开发者  · 公众号  · Python  · 2017-08-19 20:22

正文

请到「今天看啥」查看全文


), "documents" )

print ( len ( classes ), "classes" , classes )

print ( len ( words ), "unique stemmed words" , words )


12 documents

3 classes [ 'greeting' , 'goodbye' , 'sandwich' ]

26 unique stemmed words [ 'sandwich' , 'hav' , 'a' , 'how' , 'for' , 'ar' , 'good' , 'mak' , 'me' , 'it' , 'day' , 'soon' , 'nic' , 'lat' , 'going' , 'you' , 'today' , 'can' , 'lunch' , 'is' , "'s" , 'see' , 'to' , 'talk' , 'yo' , 'what' ]


注意每个单词都是词根并且小写。词根有助于机器将“have”和“having”等同起来。同时我们也不关心大小写。



我们将训练集中的每个句子转换为词包。


# create our training data

training = []

output = []

# create an empty array for our output

output_empty = [ 0 ] * len ( classes )

# training set, bag of words for each sentence

for doc in documents :

# initialize our bag of words

bag = []

# list of tokenized words for the pattern

pattern_words = doc [ 0 ]

# stem each word

pattern_words = [ stemmer . stem ( word . lower ()) for word in pattern_words ]

# create our bag of words array

for w in words :

bag . append ( 1 ) if w in pattern_words else bag . append ( 0 )

training . append ( bag )

# output is a '0' for each tag and '1' for current tag

output_row = list ( output_empty )

output_row [ classes . index ( doc [ 1 ])] = 1

output . append ( output_row )

# sample training/output

i = 0

w = documents [ i ][ 0 ]

print ([ stemmer . stem ( word . lower ()) for word in w ])

print ( training [ i ])

print ( output [ i ])


[ 'how' , 'ar' , 'you' , '?' ]

[ 0 , 0 , 0 , 1 , 0 , 1 ,







请到「今天看啥」查看全文


推荐文章
Python初级入门到精通  ·  Python-finally语句与应用
9 小时前
Python爱好者社区  ·  太炸裂了!亚马逊云来了
昨天
Python爱好者社区  ·  全球首个满级QQ正式诞生!满级后长这样
昨天
Python大全  ·  16个必知必会的Python技能!
昨天
Python大全  ·  用Python实现智能乒乓球游戏!
2 天前
Python大全  ·  用Python实现智能乒乓球游戏!
2 天前
考研英语时事阅读  ·  【早起打卡】1228-六成圣诞商品来自中国义乌
8 年前
柠檬offer  ·  【实习】 IT桔子招聘实习生
8 年前