0

I'm trying to calculate the np.sum of the elements within each array. I tried in stead of np.sum(outcome_list[0] == 'H' to just leave it as np.sum(outcome_list[j] == 'H' so that each "list" would have its own data set on the total number of heads, but it didn't like it. The bigger question is, how would I construct an array with a given base list and the action to be done in each element of that list?

enter image description here

EDIT:

the throw_a_coin definition

def throw_a_coin(N):
    return np.random.choice(['H','T'], size=N)
N =40

trials (as shown above) is the set to be acted upon

for i in trials:
    throws = throw_a_coin(i)
    outcome_list.append(throws)

for j in outcome_list:
    print("Number of Heads:", np.sum(outcome_list[0] == 'H'))
    print (j)

EDIT 2:

problem resolved with the one shown below, however I'm getting more than 13 numbers for "probabilities" - it seems that the system is running through the trials list more than once.

def throw_a_coin(N):
    return np.random.choice(['H','T'], size=N)

trials = [10, 30, 50, 70, 100, 130, 170, 200, 500, 1000, 2000, 5000, 10000]

for i in trials:
    throws = throw_a_coin(i)
    outcome_list.append(throws)

probabilities = []

for j in outcome_list:
    print("Number of Heads:", np.sum(j == 'H'))
    print("Number of Throws:", len(j))
    print("p = Number of Heads/Total Throws:", (np.sum(j == 'H'))/len(j))
    probabilities.append((np.sum(j =='H'))/len(j))
    print (j)
    print("\n")

print(probabilities)
4
  • Are you trying to count the number of heads? Commented Sep 3, 2017 at 5:08
  • 2
    Can you please attach the code instead of the photo of the code? Commented Sep 3, 2017 at 5:08
  • @Rishav - yes, count the number of heads per each of the trials Commented Sep 3, 2017 at 5:16
  • @Sohum Sachdev - edited post with the code added Commented Sep 3, 2017 at 5:17

4 Answers 4

1

You were nearly there! You just needed to replace

print("Number of Heads:", np.sum(outcome_list[0] == 'H'))

with

print("Number of Heads:", np.sum(j == 'H'))

Here is the complete answer:

trials = [10, 30, 50, 70, 100, 130, 170, 200, 500, 1000, 2000, 5000, 10000]

N =40
def throw_a_coin(N):
    return np.random.choice(['H','T'], size=N)

outcome_list = []
for i in trials:
    throws = throw_a_coin(i)
    outcome_list.append(throws)

for j in outcome_list:
    print("Number of Heads:", np.sum(j == 'H'))
    print (j)
Sign up to request clarification or add additional context in comments.

1 Comment

I've also added "EDIT 2" to the question - it seems that it's running through "trials" more than once?
1

Try this:

print("Number of Heads:", (j == 'H').sum())

Comments

0

If output_list would be a numpy array and if each row in the matrix represent each trial, you can get the sum in an efficient way as follows:

head_sum = output_list.sum(axis=1)

Comments

0

I would use a list comprehension followed by a len call.

for j in outcome_list:
    print("Number of Heads:", len([x for x in j if x == 'H'])
    print (j)

DISCLAIMER: I have zero experience with Numpy. However, this would be the general Pythonic way to do it without resorting to count.

1 Comment

This is pretty good, but I would recommend sum(1 for x in j if x == 'H') By using a generator comprehension, we avoid actually building the list of Hs. It's a tiny difference, but I just thought I would mention it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.