![](/img/trans.png)
[英]Python Pandas - Calculate Min, Max, and Mean with a function
[英]Question about mean, max, and min function python
import statistics as stat
from statistics import mean
from random import seed, choice
from math import hypot
import random
from turtle import *
import statistics
seed(20190101)
def random_walk(n):
x = 0
y = 0
for i in range(n):
step = random.choice(["N","S","E","W"])
if step == 'N':
y = y + 1
elif step == "S":
y = y - 1
elif step == "E":
x = x + 1
else:
x = x - 1
return (x,y)
for i in range(100):
walk = random_walk(50)
y = abs(walk[0]) + abs(walk[1])
x = statistics.mean(y)
print(x)
請忽略未使用的模塊
所以我試圖從 function random_walk 中獲取數字的平均值,但我遇到了幾個問題。
首先,我應該在 0,0 網格上啟動程序。 我試圖從提供的種子中找到 100 個步驟和 50 個不同 styles 的試驗平均值。 但它不能正常工作,我不知道為什么..
您正在計算循環內從random_walk()
返回的每個值的mean
,覆蓋前一個值。 將循環更改為:
all_steps = []
for i in range(100):
walk = random_walk(50)
all_steps.append(abs(walk[0]) + abs(walk[1]))
steps_mean = statistics.mean(all_steps) #Only after the loop
print(steps_mean)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.