news 2026/4/3 6:07:15

在 Wordle 中我学到了关于信息论的知识

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
在 Wordle 中我学到了关于信息论的知识

原文:towardsdatascience.com/heres-what-i-learned-about-information-theory-through-wordle-c835319cc87f

Wordle 是由《纽约时报》开发的一款令人上瘾的在线每日单词谜题游戏。

规则很简单。玩家有六次机会猜测一个五字母单词。Wordle 通过用绿色、灰色和黄色突出显示你猜测的单词中的字母来对你每次猜测提供反馈。

绿色表示你对给定字母和位置的猜测是正确的。黄色表示字母存在于单词中,但位置放错了。灰色表示字母在单词中不存在。

一个完美的猜测将导致所有五个字母都变成绿色。一个糟糕的猜测可能会导致所有字母都是灰色。如果猜测部分正确,你可能会看到黄色和灰色的混合,这表明一些字母存在但位置放错了。

我相信许多读者之前都玩过 Wordle,但如果你还没有尝试过,可以这里试试。

https://github.com/OpenDocCN/towardsdatascience-blog-zh-2024/raw/master/docs/img/b5709d915ea2ada6fd409fe32f7e9386.png

Wordle 网站截图 网站

由于其有趣的反馈循环有助于缩小可能的单词列表,Wordle 是信息论概念如何增强决策的一个完美例子。我玩游戏是为了娱乐,但内心那个书呆子想要深入了解猜测背后的“为什么”。什么使得一些猜测比其他猜测更好?反馈如何引导我找到正确的单词?

在这篇文章中,我们将探讨信息论如何回答上述问题,以及它如何应用于提高你寻找正确单词的策略。文章需要你具备概率的基本理解。

让我们开始吧。

信息论

信息论量化了系统中的不确定性以及我们如何有效地减少这种不确定性。其核心概念之一是熵(或香农熵),它衡量了一组可能结果中的不确定性量。给定概率分布P(x),熵的方程为 -

https://github.com/OpenDocCN/towardsdatascience-blog-zh-2024/raw/master/docs/img/f9eee7da454a28609b5892b917bf0cb0.png

香农熵公式。图由作者提供

熵告诉我们我们对随机变量结果的确定性有多高。熵越低,我们对结果就越确定。熵越低,我们拥有的信息就越多。当 p=0.5 时,熵达到最高。0.5 的概率表示最大的不确定性,因此信息最少。在 p=0 和 p=1 时,我们具有最低的熵,最高的确定性,以及最高的信息。熵和信息成反比。

https://github.com/OpenDocCN/towardsdatascience-blog-zh-2024/raw/master/docs/img/686f3e1b2124c7fb4ae0965d0fc6886c.png

熵曲线。图片由作者提供

“信息”如何在 Wordle 游戏中发挥作用?

假设英语词典中有 2000 个五字母单词。所有单词都有可能成为每日单词。换句话说,P(x) = 1/2000。你的第一次猜测时的熵会是多少?

https://github.com/OpenDocCN/towardsdatascience-blog-zh-2024/raw/master/docs/img/9f3281dcb73be2b99ae5e5f17a580ee2.png

熵计算。图片由作者提供

使用上述公式计算的熵将等于log(2000) = 10.97 比特。这意味着在你开始第一次猜测时,你大约有 11 比特的不确定性。

假设原始单词是“PLANT”。你在第一次尝试中猜测了“ROGUE”。屏幕将显示灰色、灰色、灰色、灰色和灰色方块。恐慌模式!不!

在收到反馈后,你成功消除了字母 R、O、G、U 和 E。通过这样做,假设你将可能的单词列表减少到 100!你的第二次猜测之前的熵会是多少?使用相同的公式,熵将等于log(100) = 6.67 比特

熵从 10.97 比特下降到 6.67 比特!这是信息增益。“ROGUE”用五个灰字给出,但给我们带来了 4.5 比特的信息。信息增益越高,猜测在减少可能的单词池方面的作用就越大。

因此,我们需要猜测一个高熵单词。如果你在第二次猜测之前猜测了一个单词,将可能的单词列表缩小到 10,你将得到log(10) = 3.32 比特的熵。这将导致 7.65 比特的信息增益!

游戏

WORDSRATED声称英语词典中有 12987 个五字母单词。Wordle 使用这些五字母单词的子集(2315 个单词)进行游戏。在这篇文章中,我将使用 2315 个五字母单词的词典。

策略是建议我们猜测的前十个建议。游戏中每个步骤的最高建议将基于上一节中讨论的熵计算生成。熵较高的单词有更高的可能是答案。

代码

以下 Python 函数根据提供的猜测和实际单词(目标)获取反馈。该函数输出游戏在猜测后显示的颜色列表。

fromcollectionsimportCounter# Function to calculate feedback for a guess against the target worddefget_feedback(guess,target):feedback=['gray']*5target_counter=Counter(target)# First pass to mark greensforiinrange(5):ifguess[i]==target[i]:feedback[i]='green'target_counter[guess[i]]-=1# Second pass to mark yellowsforiinrange(5):iffeedback[i]=='gray'andguess[i]intarget_counterandtarget_counter[guess[i]]>0:feedback[i]='yellow'target_counter[guess[i]]-=1returnfeedback

以下函数将根据给定的单词列表计算特定猜测的熵。首先,它将获取列表中每个单词的反馈。存储每种类型的反馈模式(GYBGG、GYGBY 等)的频率。猜测的熵是通过反馈模式频率的概率分布来计算的。

importmath# Function to compute entropy for a list of words given the current guessdefcompute_entropy(words,guess):feedback_counts=Counter()forwordinwords:feedback=tuple(get_feedback(guess,word))feedback_counts[feedback]+=1total_words=len(words)entropy=0.0forfeedbackinfeedback_counts.values():p=feedback/total_words entropy-=p*math.log2(p)returnentropy

以下辅助函数从单词列表中过滤可能的单词,并根据最高熵建议前 10 个单词。

# Function to filter words based on feedbackdeffilter_words(words,guess,feedback):defmatch_feedback(word):returnget_feedback(guess,word)==feedbackreturn[wordforwordinwordsifmatch_feedback(word)]# Function to suggest top 10 guesses based on entropydefsuggest_words(words):entropy_list=[(word,compute_entropy(words,word))forwordinwords]entropy_list.sort(key=lambdax:x[1],reverse=True)print("nTop 10 suggestions based on entropy:")fori,(word,entropy)inenumerate(entropy_list[:10]):print(f"{i+1}.{word}(Entropy:{entropy:.4f})")

我编写了一个可以在 Python 环境中运行的 Python 脚本,用于交互式地玩游戏。该代码随机选择一个单词作为目标,要求您猜测一个单词,并在每次猜测之前输出顶级建议以提供帮助。

importrandom# Simulate a game of Wordle using information theory for guessingdefplay_wordle(words):target=random.choice(words)remaining_words=words guesses=0print(f"Target word has been chosen (hidden for simulation).")# Start by showing suggestions before the first guesssuggest_words(remaining_words)whileguesses<6:# Get the player's guessguess=input(f"nEnter your no.{guesses+1}guess: ").strip().lower()ifguessnotinremaining_words:print("Invalid guess. Please enter a valid 5-letter word from the suggestions.")continueguesses+=1# Fetch the feedbackfeedback=get_feedback(guess,target)print(f"Feedback for '{guess}':{feedback}")iffeedback==['green']*5:print(f"Success! Found the word '{guess}' in{guesses}guesses.")return# Filter the words based on feedbackremaining_words=filter_words(remaining_words,guess,feedback)ifnotremaining_words:returnprint(f"{len(remaining_words)}possible words remaining")# Suggest top 10 guesses based on entropysuggest_words(remaining_words)print(f"Failed to guess the word. The correct word was:{target}")if__name__=='__main__':words=load_word_list()play_wordle(words)

模拟

我进行了一些模拟。以下图像展示了使用 Python 实现的 Wordle 游戏的示例。

https://github.com/OpenDocCN/towardsdatascience-blog-zh-2024/raw/master/docs/img/e5013b8dc2cda0f42de2faf48594986e.png

猜出 “exert” 需要 3 次猜测。图由作者提供

https://github.com/OpenDocCN/towardsdatascience-blog-zh-2024/raw/master/docs/img/6911622c2eb40d96a02d18d98832c85c.png

猜出 “Guide” 需要 3 次猜测。图由作者提供

Wordle 辅助工具

我创建了一个 Wordle 辅助工具,在您实时玩 Wordle 游戏时可以帮助您。它会在每次猜测后提供前十个单词的建议。您必须向程序提供您的猜测和收到的反馈。辅助工具将生成一个包含前 10 个建议的列表。

以下代码片段包含了 Wordle 辅助工具的代码。

# Function to suggest top 10 words based on entropy and return all possible wordsdefwordle_assistant(words,guess,feedback):# Filter the remaining words based on feedbackremaining_words=filter_words(words,guess,feedback)# Compute entropy for each remaining wordentropy_list=[(word,compute_entropy(remaining_words,word))forwordinremaining_words]entropy_list.sort(key=lambdax:x[1],reverse=True)# Get the top 10 suggestionstop_suggestions=entropy_list[:10]print("nTop 10 suggestions based on entropy:")fori,(word,entropy)inenumerate(top_suggestions):print(f"{i+1}.{word}(Entropy:{entropy:.4f})")return[wordforword,_intop_suggestions],remaining_wordsif__name__=="__main__":# Example word list (replace with a larger dictionary for real use)remaining_words=wordsforguess_numberinrange(1,7):print(f"n--- Guess{guess_number}---")# Input the user's guessguess=input("Enter your guess: ").strip().lower()ifguessnotinremaining_words:print("Invalid guess. Make sure the word is valid and in the list of remaining suggestions.")continue# Input feedback for the guessfeedback_input=input("Enter feedback (e.g., 'xygxx'): ").strip().lower()feedback=['green'ifc=='g'else'yellow'ifc=='y'else'gray'forcinfeedback_input]# Process and update suggestionstop_suggestions,remaining_words=wordle_assistant(remaining_words,guess,feedback)iflen(remaining_words)==1:print(f"nCongratulations! The target word is:{remaining_words[0]}")breakelifnotremaining_words:print("nNo words remaining. Something went wrong with the feedback.")breakelse:print(f"n{len(remaining_words)}words remain in the list.")

以下图示说明了我是如何使用熵来猜测正确单词的。

https://github.com/OpenDocCN/towardsdatascience-blog-zh-2024/raw/master/docs/img/f5c411930bf66b285447fb1900f70dd6.png

使用 Wordle 辅助工具进行的实时 Wordle 模拟

对不起,我破坏了游戏的乐趣。

尽管如此,该模拟从纽约时报官方为 Wordle 定义的 2315 个单词的词典中生成建议。在约 12000 个五字母长单词的词典上执行此类模拟可能是一项有趣的练习。

本文使用的代码已上传至此 –github.com/sm823zw/wordle-simulation

希望您觉得我的文章有趣!

感谢您阅读我的文章!

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/1 22:07:43

29、在GCP上部署Kubernetes:负载均衡、持久磁盘与集群搭建全攻略

在GCP上部署Kubernetes:负载均衡、持久磁盘与集群搭建全攻略 1. 负载均衡 GCP提供了以下几种类型的负载均衡器: - Layer 4 TCP LoadBalancer - Layer 4 UDP LoadBalancer - Layer 7 HTTP(S) LoadBalancer Layer 4的负载均衡器(TCP和UDP)类似于AWS Classic ELB,而Lay…

作者头像 李华
网站建设 2026/3/31 7:29:51

13、SharePoint 2010 列表编程全解析

SharePoint 2010 列表编程全解析 在 SharePoint 2010 开发中,列表是一个核心对象,它不仅是开发者经常操作的对象,也是终端用户常用的工具。本文将详细介绍 SharePoint 2010 列表的结构、功能以及多种编程访问方式。 1. SharePoint 列表概述 列表是 SharePoint 中的一种数…

作者头像 李华
网站建设 2026/3/16 16:56:59

Kotaemon能否实现定时任务触发知识更新?

Kotaemon能否实现定时任务触发知识更新&#xff1f; 在金融、医疗、法律等专业领域&#xff0c;智能问答系统的可信度往往取决于一个看似不起眼却至关重要的因素——知识的时效性。当用户询问“最新的医保政策有哪些变化&#xff1f;”而系统仍引用半年前的条款时&#xff0c;再…

作者头像 李华
网站建设 2026/3/31 19:40:44

Kotaemon支持Prometheus监控指标暴露吗?

Kotaemon 支持 Prometheus 监控指标暴露吗&#xff1f; 在构建现代 AI 应用的实践中&#xff0c;一个绕不开的问题是&#xff1a;当系统上线后出现响应变慢、答案质量波动或频繁报错时&#xff0c;我们如何快速定位问题&#xff1f;尤其是在基于检索增强生成&#xff08;RAG&am…

作者头像 李华
网站建设 2026/3/30 5:09:01

2、从 Windows 迁移到 Linux 桌面的理由与相关分析

从 Windows 迁移到 Linux 桌面的理由与相关分析 迁移考量因素 在考虑从 Windows 迁移到 Linux 桌面时,有几个关键因素需要考量: 1. 打破单一供应商依赖 :当前的 Windows 软件可能会让你陷入对单一供应商的依赖,例如微软 Office。软件产品缺乏竞争会导致价格上涨,并且…

作者头像 李华