Python中的马尔可夫链(入门)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python中的马尔可夫链(入门),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2723字,纯文字阅读大概需要4分钟。
内容图文
![Python中的马尔可夫链(入门)](/upload/InfoBanner/zyjiaocheng/662/a038fbf5e57948cfb2537c56060ba23e.jpg)
我是python的新手,正尝试制作markov链.其他示例显示了对象实例的用法,而我还没有走那么远.我还没有对值部分进行随机选择,但是到目前为止,基本上我对这段代码的输出不知所措.
filename = open("dr-suess.txt")
def make_list(filename):
"""make file a list and a list of tuple tup_pairs"""
file_string = filename.read() #read whole file
file_list = file_string.split() #split on whitespace (not worrying about
# puncuation right now)
tup_pairs = []
for i in range(len(file_list)-1):
tup_pairs.append((file_list[i], file_list[i+1])) #making my tuple pair list
return tup_pairs, file_list
def mapping(filename):
tup_pairs, file_list = make_list(filename)
dictionary = {}
for pair in tup_pairs:
dictionary[pair] = [] #setting the value of dict to empty list
tup_pairs = set(tup_pairs) #throwing out repeated tuples
for word in file_list:
word_number = file_list.index(word) #index number of iter word
if word_number > 1: #because there is no -2/-1 index
compared_tuple = (file_list[word_number-2], file_list[word_number-1]) #to find
#preceeding pair to compare
for pair in tup_pairs:
if compared_tuple == pair:
dictionary[pair].append(word) #should append the word to my dict value (list)
print dictionary #getting weird results (some words should appear that dont, some
# don't appear that should)
mapping(filename)
输出:
Lindsays-MBP:markov lindsayg$python markov.py
{('a', 'fox?'): [], ('Sam', 'I'): ['am?'], **('you,', 'could'): ['you', 'you', 'you', 'you', 'you', 'yo**u']**, ('could', 'you'): ['in', 'with', 'in', 'with'], ('you', 'with'): [], ('box?', 'Would'): [], ('ham?', 'Would'): [], ('I', 'am?'): [], ('you', 'in'): ['a', 'a', 'a', 'a'], ('a', 'house?'): [], ('like', 'green'): ['eggs'], ('like', 'them,'): ['Sam'], ('and', 'ham?'): [], ('Would', 'you'): ['like', 'like'], ('a', 'mouse?'): [], ('them,', 'Sam'): ['I'], ('in', 'a'): ['house?', 'box?'], ('with', 'a'): ['mouse?', 'fox?'], ('house?', 'Would'): [], ('a', 'box?'): [], ('Would', 'you,'): ['could', 'could', 'could', 'could'], ('green', 'eggs'): ['and'], ('you', 'like'): ['green', 'them,'], ('mouse?', 'Would'): [], ('fox?', 'Would'): [], ('eggs', 'and'): ['ham?']}
一个奇怪的输出示例(应该只有4个“ you”值,并且只有六个):
('you,', 'could'): ['you', 'you', 'you', 'you', 'you', 'you']
正在使用的fyi文件文本:
Would you, could you in a house?
Would you, could you with a mouse?
Would you, could you in a box?
Would you, could you with a fox?
Would you like green eggs and ham?
Would you like them, Sam I am?
解决方法:
您的问题是找到单词索引的方式:index提供了第一个实例.有6个“ you”(和4个“ you”不同),它们每个都将获得相同的索引word_number = 3,因此它们都将添加到该对中(“ Would”,“ you”).
要获取索引,您应该使用内置的enumerate:
for word_number, word in enumerate(file_list):
...
内容总结
以上是互联网集市为您收集整理的Python中的马尔可夫链(入门)全部内容,希望文章能够帮你解决Python中的马尔可夫链(入门)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。