Runtime error chisquared mission

Hello, I tried running the chi-squared generating a distribution mission but I am getting run time error and not detecting any code ( this specifically Your code run has timed out.
This could be caused by writing an infinite loop, or an issue with our system.)

 chi_squared_values = []
import numpy as np
import matplotlib.pyplot as plt

for i in range(1000):
    male_count = 0
    female_count = 0
    value = np.random.random((32561,))
    for value_random in value:
        if value_random <= 0.5:
            male_count += 1
        else:
            female_count += 1
        male_diff = (male_count-16280.5)**2/16280.5
        female_diff =(female_count-16280.5)**2/16280.5
        chi_squared_values.append(male_diff+female_diff)
plt.hist(chi_squared_values)  

 [mission link](https://app.dataquest.io/m/99/chi-squared-tests/4/generating-a-distribution)

It would be better if you -

  • Shared the link to the Mission
  • Formatted your code properly using the </> button in the editor
  • Shared the error you get when you run the code.

Some general suggestions in this post as well that I would recommend going through - Introducing new guidelines for all technical questions in our Community

Hi @the_doctor I have done that

It seems, DataQuest has a limit on how long a piece of code can run on their platform. And you are currently exceeding that limit.

That’s because you have two for loops in your code.

for i in range(1000):
    value = np.random.random((32561,))
    for value_random in value:

For every i, your inner for loop will run 32561 times. Therefore, in total, both of your loops will run for a total of 32561*1000 times. That’s a lot, and DataQuest doesn’t want us to be running code that takes such a long time to execute.

So, you will have to find a way of not using two loops.

By now, if you are comfortable with working with numpy arrays you might be able to start figuring this out. But if you are facing difficulties, then feel free to check the provided solution and learn from it.

Being able to avoid such loops which significantly increase the execution time of your code is an important skill to learn. So, go through their provided solution and see how they do this differently.

Thanks, @the_doctor, I figured it out, used NumPy indexing and len() to get the count and it worked. The code that i shared was from the solution with little modifications, when i ran dataquest solution it gave same problem, maybe you can alert them to use this so others won’t face same issues
This is the code that I used

import numpy as np
import matplotlib.pyplot as plt

for i in range(1000):
    value = np.random.random((32561,))
    male_count = len(value[value<0.5])
    female_count = len(value[value>=0.5])
    male_diff = (male_count-16280.5)**2/16280.5
    female_diff =(female_count-16280.5)**2/16280.5
    chi_squared_values.append(male_diff+female_diff)
plt.hist(chi_squared_values)