html旅游网站制作代码,页面模板图片,做网站前段用什么软件,个人网站搭建详细步骤原文#xff1a;towardsdatascience.com/heres-what-i-learned-about-information-theory-through-wordle-c835319cc87f Wordle 是由《纽约时报》开发的一款令人上瘾的在线每日单词谜题游戏。
规则很简单。玩家有六次机会猜测一个五字母单词。Wordle 通过用绿色、灰色和黄色突…原文towardsdatascience.com/heres-what-i-learned-about-information-theory-through-wordle-c835319cc87fWordle 是由《纽约时报》开发的一款令人上瘾的在线每日单词谜题游戏。规则很简单。玩家有六次机会猜测一个五字母单词。Wordle 通过用绿色、灰色和黄色突出显示你猜测的单词中的字母来对你每次猜测提供反馈。绿色表示你对给定字母和位置的猜测是正确的。黄色表示字母存在于单词中但位置放错了。灰色表示字母在单词中不存在。一个完美的猜测将导致所有五个字母都变成绿色。一个糟糕的猜测可能会导致所有字母都是灰色。如果猜测部分正确你可能会看到黄色和灰色的混合这表明一些字母存在但位置放错了。我相信许多读者之前都玩过 Wordle但如果你还没有尝试过可以这里试试。https://github.com/OpenDocCN/towardsdatascience-blog-zh-2024/raw/master/docs/img/b5709d915ea2ada6fd409fe32f7e9386.pngWordle 网站截图 网站由于其有趣的反馈循环有助于缩小可能的单词列表Wordle 是信息论概念如何增强决策的一个完美例子。我玩游戏是为了娱乐但内心那个书呆子想要深入了解猜测背后的“为什么”。什么使得一些猜测比其他猜测更好反馈如何引导我找到正确的单词在这篇文章中我们将探讨信息论如何回答上述问题以及它如何应用于提高你寻找正确单词的策略。文章需要你具备概率的基本理解。让我们开始吧。信息论信息论量化了系统中的不确定性以及我们如何有效地减少这种不确定性。其核心概念之一是熵或香农熵它衡量了一组可能结果中的不确定性量。给定概率分布P(x)熵的方程为 -https://github.com/OpenDocCN/towardsdatascience-blog-zh-2024/raw/master/docs/img/f9eee7da454a28609b5892b917bf0cb0.png香农熵公式。图由作者提供熵告诉我们我们对随机变量结果的确定性有多高。熵越低我们对结果就越确定。熵越低我们拥有的信息就越多。当 p0.5 时熵达到最高。0.5 的概率表示最大的不确定性因此信息最少。在 p0 和 p1 时我们具有最低的熵最高的确定性以及最高的信息。熵和信息成反比。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_counterCounter(target)# First pass to mark greensforiinrange(5):ifguess[i]target[i]:feedback[i]greentarget_counter[guess[i]]-1# Second pass to mark yellowsforiinrange(5):iffeedback[i]grayandguess[i]intarget_counterandtarget_counter[guess[i]]0:feedback[i]yellowtarget_counter[guess[i]]-1returnfeedback以下函数将根据给定的单词列表计算特定猜测的熵。首先它将获取列表中每个单词的反馈。存储每种类型的反馈模式GYBGG、GYGBY 等的频率。猜测的熵是通过反馈模式频率的概率分布来计算的。importmath# Function to compute entropy for a list of words given the current guessdefcompute_entropy(words,guess):feedback_countsCounter()forwordinwords:feedbacktuple(get_feedback(guess,word))feedback_counts[feedback]1total_wordslen(words)entropy0.0forfeedbackinfeedback_counts.values():pfeedback/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(keylambdax:x[1],reverseTrue)print(nTop 10 suggestions based on entropy:)fori,(word,entropy)inenumerate(entropy_list[:10]):print(f{i1}.{word}(Entropy:{entropy:.4f}))我编写了一个可以在 Python 环境中运行的 Python 脚本用于交互式地玩游戏。该代码随机选择一个单词作为目标要求您猜测一个单词并在每次猜测之前输出顶级建议以提供帮助。importrandom# Simulate a game of Wordle using information theory for guessingdefplay_wordle(words):targetrandom.choice(words)remaining_wordswords guesses0print(fTarget word has been chosen (hidden for simulation).)# Start by showing suggestions before the first guesssuggest_words(remaining_words)whileguesses6:# Get the players guessguessinput(fnEnter your no.{guesses1}guess: ).strip().lower()ifguessnotinremaining_words:print(Invalid guess. Please enter a valid 5-letter word from the suggestions.)continueguesses1# Fetch the feedbackfeedbackget_feedback(guess,target)print(fFeedback for {guess}:{feedback})iffeedback[green]*5:print(fSuccess! Found the word {guess} in{guesses}guesses.)return# Filter the words based on feedbackremaining_wordsfilter_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(fFailed to guess the word. The correct word was:{target})if__name____main__:wordsload_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_wordsfilter_words(words,guess,feedback)# Compute entropy for each remaining wordentropy_list[(word,compute_entropy(remaining_words,word))forwordinremaining_words]entropy_list.sort(keylambdax:x[1],reverseTrue)# Get the top 10 suggestionstop_suggestionsentropy_list[:10]print(nTop 10 suggestions based on entropy:)fori,(word,entropy)inenumerate(top_suggestions):print(f{i1}.{word}(Entropy:{entropy:.4f}))return[wordforword,_intop_suggestions],remaining_wordsif__name____main__:# Example word list (replace with a larger dictionary for real use)remaining_wordswordsforguess_numberinrange(1,7):print(fn--- Guess{guess_number}---)# Input the users guessguessinput(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_inputinput(Enter feedback (e.g., xygxx): ).strip().lower()feedback[greenifcgelseyellowifcyelsegrayforcinfeedback_input]# Process and update suggestionstop_suggestions,remaining_wordswordle_assistant(remaining_words,guess,feedback)iflen(remaining_words)1:print(fnCongratulations! The target word is:{remaining_words[0]})breakelifnotremaining_words:print(nNo words remaining. Something went wrong with the feedback.)breakelse:print(fn{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希望您觉得我的文章有趣感谢您阅读我的文章