Use boolean masks to turn a file of survey answers into one number.
Find the Net Promoter Score of this company.
import numpy as np
url = ("https://raw.githubusercontent.com/hamzamohdzubair/"
"small-data/refs/heads/main/nps.txt")
data = np.loadtxt(url)
total = data.shape[0]
detractors = data[data<7].shape[0]
promoters = data[data>8].shape[0]
perc_promoters = (promoters/total)*100
perc_detractors = (detractors/total)*100
nps = perc_promoters - perc_detractors
data < 7A comparison makes a boolean mask, one True/False per answer.
data[mask]Keeps only the True positions.
.shape[0]Counts what is left. ((data < 7).sum() also works: True counts as 1.)
Back to NumPy · see Array Fundamentals for masking.