使用 Python 第三方库 daft 绘制 PGM 中的贝叶斯网络

daft 的官方文档请见 DAFT:BEAUTIFULLY RENDERED PROBABILISTIC GRAPHICAL MODELS.

from matplotlib import rc
rc("font", family="serif", size=12)
rc("text", usetex=True)

import daft

pgm = daft.PGM([3.6, 2.7], origin=[1.15, 0.65])
pgm.add_node(daft.Node("cloudy", r"cloudy", 3, 3, aspect=1.8))
pgm.add_node(daft.Node("rain", r"rain", 2, 2, aspect=1.2))
pgm.add_node(daft.Node("sprinkler", r"sprinkler", 4, 2, aspect=2.1))
pgm.add_node(daft.Node("wet", r"grass wet", 3, 1, aspect=2.4, observed=True))
pgm.add_edge("cloudy", "rain")
pgm.add_edge("cloudy", "sprinkler")
pgm.add_edge("rain", "wet")
pgm.add_edge("sprinkler", "wet")
pgm.render()
pgm.figure.savefig("wordy.pdf")
pgm.figure.savefig("wordy.png", dpi=150)
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
以下是一个简单的C#打字练习软件设计: 1. 首先,需要创建一个Windows窗体应用程序。 2. 在窗体添加一个文本框和一个标签,用于显示练习文本和用户输入的文本。 3. 在窗体添加一个计时器控件,用于计算用户完成练习所需的时间。 4. 在窗体添加一个按钮,用于开始练习。 5. 在代码,需要定义一个字符串数组,用于存储练习文本。 6. 在按钮的Click事件,随机选择一个字符串数组的文本,并将其显示在标签。 7. 在文本框的TextChanged事件,将用户输入的文本与练习文本进行比较,并将正确的字符显示为绿色,错误的字符显示为红色。 8. 在计时器的Tick事件,更新计时器的显示,并检查用户是否完成了练习。 9. 如果用户完成了练习,停止计时器并显示完成时间和准确性。 以下是示例代码: ```csharp public partial class Form1 : Form { private string[] texts = { "The quick brown fox jumps over the lazy dog.", "Sphinx of black quartz, judge my vow.", "How vexingly quick daft zebras jump!", "Pack my box with five dozen liquor jugs." }; private string currentText; private int currentIndex; private int correctCount; private int errorCount; private bool isStarted; public Form1() { InitializeComponent(); } private void btnStart_Click(object sender, EventArgs e) { if (!isStarted) { currentIndex = new Random().Next(texts.Length); currentText = texts[currentIndex]; lblText.Text = currentText; txtInput.Text = ""; correctCount = 0; errorCount = 0; lblAccuracy.Text = ""; lblTime.Text = "00:00:00"; timer1.Start(); isStarted = true; } } private void txtInput_TextChanged(object sender, EventArgs e) { if (isStarted) { string inputText = txtInput.Text; int minLength = Math.Min(currentText.Length, inputText.Length); for (int i = 0; i < minLength; i++) { if (inputText[i] == currentText[i]) { txtInput.Select(i, 1); txtInput.SelectionColor = Color.Green; } else { txtInput.Select(i, 1); txtInput.SelectionColor = Color.Red; errorCount++; } } if (inputText.Length > currentText.Length) { txtInput.Select(currentText.Length, inputText.Length - currentText.Length); txtInput.SelectionColor = Color.Red; errorCount += inputText.Length - currentText.Length; } lblAccuracy.Text = $"{100 * correctCount / (correctCount + errorCount)}%"; } } private void timer1_Tick(object sender, EventArgs e) { TimeSpan timeSpan = TimeSpan.FromSeconds(timer1.Interval / 1000 + timer1.Tag == null ? 0 : (int)timer1.Tag); lblTime.Text = timeSpan.ToString("hh\\:mm\\:ss"); if (isStarted && txtInput.Text == currentText) { timer1.Stop(); MessageBox.Show($"Congratulations! You finished the text in {timeSpan.TotalSeconds} seconds with {lblAccuracy.Text} accuracy."); isStarted = false; } } } ```
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

五道口纳什

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值