X^2 Lesson 4 alternative

Hello there
I found the code used from DQ a bit “heavy”.
sequence[sequence < .5] = 0
sequence[sequence >= .5] = 1
was too much for me!!
Instead I changed it with np.where() and it worked ok.
Herewith I present thw DQ’s solution code and then my take:


DQ’s CODE:
chi_squared_values =
from numpy.random import random
import matplotlib.pyplot as plt

for i in range(1000):
sequence = random((32561,))
sequence[sequence < .5] = 0
sequence[sequence >= .5] = 1
male_count = len(sequence[sequence == 0])
female_count = len(sequence[sequence == 1])
male_diff = (male_count - 16280.5) ** 2 / 16280.5
female_diff = (female_count - 16280.5) ** 2 / 16280.5
chi_squared = male_diff + female_diff
chi_squared_values.append(chi_squared)

plt.hist(chi_squared_values)

MY TAKE:
chi_squared_values =
import numpy as np
import matplotlib.pyplot as plt

for i in range(1000):
sequence = np.random.random((32561,))
sequence=np. where(sequence>0.5, 1, 0)
male_count = len(sequence[sequence == 0])
female_count = len(sequence[sequence == 1])
male_diff = (male_count - 16280.5) ** 2 / 16280.5
female_diff = (female_count - 16280.5) ** 2 / 16280.5
chi_squared = male_diff + female_diff
chi_squared_values.append(chi_squared)

plt.hist(chi_squared_values)