2014-02-08 1 views
0

몬티 홀 문제에 대한 시뮬레이션을 작성 중이며,이 오류의 원인을 이해할 수 없습니다. 몬티 홀 문제에 익숙하지 않은 경우 3 개의 문이 있고 1 개의 문 뒤에는 상품이없고 1 개의 문에는 2 개의 문이있는 가상의 게임 쇼입니다. 참가자가 문을 뽑으면 주최자는 우승하지 않은 문을 열고 참가자에게 원래 선택을 전환하거나 유지할 수있는 옵션을 제공합니다. 원래 픽은 1/3의 확률로 스위치 전략은 2/3의 확률로 맞습니다.python 오류 : ''TypeError : 길이가 1 인 배열 만 파이썬 스칼라로 변환 될 수 있습니다. '

내 첫 기능이 임의로 문 선택되는 2 개 배열을 얻어 다음 문

import numpy as np 
import pandas as pd 


def reveal_and_switch(win_door,first_pick): 
    '''Create arrays for the door to be revealed by the host and the switch door''' 
    #Take in arrays for the winning door and the contestant's first pick 
    doors = [1,2,3] 
    switch_door = np.array([0]*len(win_door)) 
    for i in range(len(switch_door)): 
     if first_pick[i] != win_door[i]: 
      switch_door[i] = win_door[i] 
     else: 
      del doors[np.searchsorted(doors,first_pick[i])] 
      switch_door[i] = np.random.choice(doors) 

    #print switch_door 
    return switch_door 


def create_doors(iterations): 
    '''Create a DataFrame with columns representing the winning doors, 
    the picked doors and the doors picked if the player switches and the 
    accumulating probabilities''' 
    win_door = np.random.random_integers(1,3,iterations) 
    first_pick = np.random.random_integers(1,3,iterations) 
    switch_door = reveal_and_switch(win_door,first_pick) 
    #allocate memory for 
    denom = np.array([0]*len(win_door)) 
    first_win = np.array([0]*len(win_door)) 
    switch_win = np.array([0]*len(win_door)) 
    switch_prob = np.array([0]*len(win_door)) 
    stay_prob = np.array([0]*len(win_door)) 

    for i in len(range(switch_door)): 
     denom[i] = i + 1 
     if switch_door[i] == win_door[i]: 
      switch_win[i] = 1 
      first_win[i] = 0 
     elif first_pick[i] == win_door[i]: 
      switch_win[i] = 0 
      first_win[i] = 1 



    switch_prob = np.cumsum(switch_win)/denom 
    stay_prob = np.cumsum(first_win)/denom 
    df = pd.DataFrame({'iterations': iterations, 
        'Stubborn Win': first_win, 
        'Switch Win': switch_win, 
        'stubborn probability': stay_prob, 
        'switch probability': switch_prob}) 
    print df 
    return df 

인 제 3 배열을 만들고 난 (10) create_doors를 호출 할 때,이 얻을 :

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "<stdin>", line 14, in create_doors 
TypeError: only length-1 arrays can be converted to Python scalars 

답변

1

이와 같은 오류가 재현 : 코드 range(switch_door)에서

In [32]: a 
Out[32]: array([0, 1, 2]) 

In [33]: range(a) 
--------------------------------------------------------------------------- 
TypeError         Traceback (most recent call last) 
<ipython-input-33-5515275ab580> in <module>() 
----> 1 range(a) 

TypeError: only length-1 arrays can be converted to Python scalars 

을, 그건 내 range(a)처럼.

BTW, 코드에서,

denom = np.array([0]*len(win_door)) 
first_win = np.array([0]*len(win_door)) 

그냥 간단하게 할 수 :

denom=np.zeros_like(win_door) 
first_win = denom.copy() 
+0

감사합니다 순전히! 너 락! – panterasBox

관련 문제