を繰り返しては」という質問に答えるために、反転しNコインフリップ後のグループ数の期待値はどれくらいですか?グループは、定義上、連続した順序で同じ値の最大シーケンスです。例えばパイソン - Nコインで構成され、私は偏ったコイン投げ実験(H = 0.6、T = 0.4)のX番号をシミュレートする関数を記述しようとしている偏ったコイン投げ実験
:['H', 'H', 'H', 'H', 'H', 'H', 'T', 'H', 'H', 'H']
3基を有し、['T', 'H', 'H', 'H', 'T', 'T', 'H', 'H', 'H', 'H']
4基を有します。
# Libraries
import random
from itertools import groupby
from itertools import chain
# Function for biased coin
def flip(p):
return 'H' if random.random() < p else 'T'
# Number of coin flips
N = 10
flips = [flip(0.6) for i in range(N)]
print (len(list(groupby(flips))))
# Function to simulate X iterations of N coin flips
def simulate(X, N):
Outcome = [] # Empty list to store each experiment's result
# X Number of coin simulations
for i in range(X):
# Coin simulation of N flips
flips = [flip(0.6) for j in range(N)]
# Append experiment's result into Outcome list
Outcome.append(len(list(groupby(flips))))
# Expected Value of the number of groups
sum(Outcome)/X
これはなぜ機能しないのでしょうか?私は、次のエラーTypeError: unsupported operand type(s) for /: 'list' and 'int'
何を「動作しない」とはどういう意味? 'Outcome/X'(整数でリストを分割する - ものではない)はおそらくその一部ですが、それ以外は何ですか? – Ryan
それは私のために働く!あなたは何を期待していますか?あなたはそれがうまくいかないと感じさせています。 – Shobeir
次のエラーがあります。 'TypeError:/' list 'と' int 'のサポートされていないオペランドタイプ –