每次运行代码时,情节看起来都不同

发布于 2025-01-31 01:49:27 字数 759 浏览 1 评论 0原文

我的代码有问题。每次运行时,它看起来都不同。有什么想法吗?我看不到任何问题。我从2H开始查看此代码,但找不到问题...

import numpy as np
import matplotlib.pyplot as plt
from scipy import stats


x = np.arange(0,24, 1)
y = stats.poisson.pmf(x, mu=13)
a =stats.poisson.rvs(mu=13, size=5000)

#plt.stem(a,x)
plt.hist(a,bins=x,density=True,edgecolor="red")
plt.title("Poisson Verteilung mit Erwartungswert 13")
plt.xlabel("Anzahl M.")
plt.ylabel("Wahrscheinlichkeit")
"""
i=10
o=30
while i != o:
  if y[i]*100<0.5:
    #print(i)
    break
  i+=1
"""

#plot to specific x value
plt.xlim(0, 23)

plt.plot()
plt.plot(13, y[13], marker='x', markersize=5, color="black",label="Varianz")
plt.plot(13, y[13], marker='x', markersize=5, color="black",label="Median")
plt.legend(loc="upper left")

I have a problem with my code. It looks different everytime i run it. Any ideas? I don't see any problem. I am looking at this code since 2h and I can't find the problem...

import numpy as np
import matplotlib.pyplot as plt
from scipy import stats


x = np.arange(0,24, 1)
y = stats.poisson.pmf(x, mu=13)
a =stats.poisson.rvs(mu=13, size=5000)

#plt.stem(a,x)
plt.hist(a,bins=x,density=True,edgecolor="red")
plt.title("Poisson Verteilung mit Erwartungswert 13")
plt.xlabel("Anzahl M.")
plt.ylabel("Wahrscheinlichkeit")
"""
i=10
o=30
while i != o:
  if y[i]*100<0.5:
    #print(i)
    break
  i+=1
"""

#plot to specific x value
plt.xlim(0, 23)

plt.plot()
plt.plot(13, y[13], marker='x', markersize=5, color="black",label="Varianz")
plt.plot(13, y[13], marker='x', markersize=5, color="black",label="Median")
plt.legend(loc="upper left")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

衣神在巴黎 2025-02-07 01:49:28

您正在使用随机统计分发,每次运行代码a时创建了新的随机分布。
为了获得可重复性(运行时,您的代码选择始终是相同的随机分发),您必须

np.random.seed(42)

完整的代码

import numpy as np
import matplotlib.pyplot as plt
from scipy import stats


np.random.seed(42)
x = np.arange(0,24, 1)
y = stats.poisson.pmf(x, mu=13)
a =stats.poisson.rvs(mu=13, size=5000)

#plt.stem(a,x)
plt.hist(a,bins=x,density=True,edgecolor="red")
plt.title("Poisson Verteilung mit Erwartungswert 13")
plt.xlabel("Anzahl M.")
plt.ylabel("Wahrscheinlichkeit")
"""
i=10
o=30
while i != o:
  if y[i]*100<0.5:
    #print(i)
    break
  i+=1
"""

#plot to specific x value
plt.xlim(0, 23)

plt.plot()
plt.plot(13, y[13], marker='x', markersize=5, color="black",label="Varianz")
plt.plot(13, y[13], marker='x', markersize=5, color="black",label="Median")
plt.legend(loc="upper left")

plt.show()

“在此处输入图像描述”

You are using random statistical distribution, each time you run your code a new random distribution is created.
In order to get repeatability (your code picks always the same random distribution when you run it) you have to set a seed at the beginning of your code:

np.random.seed(42)

You can choose which seed you prefer, 42 is a common one.

Complete Code

import numpy as np
import matplotlib.pyplot as plt
from scipy import stats


np.random.seed(42)
x = np.arange(0,24, 1)
y = stats.poisson.pmf(x, mu=13)
a =stats.poisson.rvs(mu=13, size=5000)

#plt.stem(a,x)
plt.hist(a,bins=x,density=True,edgecolor="red")
plt.title("Poisson Verteilung mit Erwartungswert 13")
plt.xlabel("Anzahl M.")
plt.ylabel("Wahrscheinlichkeit")
"""
i=10
o=30
while i != o:
  if y[i]*100<0.5:
    #print(i)
    break
  i+=1
"""

#plot to specific x value
plt.xlim(0, 23)

plt.plot()
plt.plot(13, y[13], marker='x', markersize=5, color="black",label="Varianz")
plt.plot(13, y[13], marker='x', markersize=5, color="black",label="Median")
plt.legend(loc="upper left")

plt.show()

Plot

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文