Reputation: 1355
I am writing a simulation of the monty hall problem and I can't for the life of me understand what is causing this error. If you are not familiar with the monty hall problem, it is a hypothetical game show where there are 3 doors, there is a prize behind one door and 2 doors with nothing. A contestant picks a door and then the host opens a non-winning door and gives the contestant the option to switch or stay with their original pick. The original pick has a 1/3 chance of being right and the switch strategy has a 2/3 chance of being right.
My first function there takes 2 arrays which are randomly chosen doors and then creates a third array which is the door
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
and when I call create_doors(10), I get this:
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
Upvotes: 0
Views: 5190
Reputation: 34017
reproduce such an error:
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
In your code range(switch_door)
, it's just like my range(a)
.
BTW, in your code,
denom = np.array([0]*len(win_door))
first_win = np.array([0]*len(win_door))
could just be simplified:
denom=np.zeros_like(win_door)
first_win = denom.copy()
Upvotes: 1