concept sum in category data

This is an excerpt from Manning's book Data Science Bookcamp: Five Python Projects.
There are 4332 ways for 6 rolled dice to sum to 21. For example, we could roll 4 fours, followed by a three and then a two. Or we could roll 3 fours followed by a five, a three, and a one. Thousands of other combinations are possible. This is why a sum of 21 is much more probable than a sum of 6.
Listing 1.24. Exploring different ways of summing to 21
assert sum([4, 4, 4, 4, 3, 2]) == 21 assert sum([4, 4, 4, 5, 3, 1]) == 21Please note that the observed count of 4332 is equal to the length of an unweighted event whose dice-rolls add up to 21. Also, the sum of values in
weighted_sampleis equal to the length ofsample_space. Hence, there exists a direct link between unweighted and weighted event probability computation.
Listing 5.5. Computing an interval probability using
stats.binom.pmfinterval_all_counts = range(21) probabilities = stats.binom.pmf(interval_all_counts, num_flips, prob_head) total_prob = probabilities.sum() print(f"Total sum of probabilities equals {total_prob:.14f}")
Our plot displays the penalty across a range of possible centers based on their distance to our minimum. As the centers shift towards the 61, the penalty will drop, but their distance to the remaining 6 measurements will increase. Thus, we ought to penalize each potential center based on its squared distance to all 7 recorded measurements. We’ll do so by defining a sum of squared distances function, which will add up the squared distances between some value and the measurement array. That function will serve as our new penalty. Plotting the possible centers against their penalties will allow us to find the center whose penalty is minimized.
Listing 5.14. Penalizing centers using total sum of squared distances
def sum_of_squared_distances(value, measurements): return sum(squared_distance(value, m) for m in measurements) penalties = [sum_of_squared_distances(center, measurements) for center in possible_centers] plt.plot(possible_centers, penalties) plt.scatter(possible_centers, penalties) plt.xlabel('Possible Centers') plt.ylabel('Penalty') plt.show()Figure 5.7. A plot of possible centers penalized based on the sum of their squared distance relative to all recorded temperatures. The minimum penalty occurs at 75 degrees.
![]()
California’s sum of squares is 974. We expect Kentucky’s sum of squares to be noticeably lower. Let’s confirm.

This is an excerpt from Manning's book Mastering Large Datasets with Python: Parallelize and Distribute Your Python Code.
Often, we’ll want to use map and reduce together, one right after another. This pattern gives rise to the MapReduce programming pattern. The MapReduce programming pattern relies on the map function to transform some data into another type of data and then uses the reduce function to combine that data. A mathematical example might be taking the sum of the greatest prime factor of a sequence of numbers. We can use map to transform each number into its greatest prime factor and then use reduce to take their sum. A more practical example may be finding the longest word on a sequence of web pages, when all we have is the URLs. We can use map to turn the URLs into text and reduce to find the longest word (figure 1.7).
An example of an N-to-X transformation that you’re already familiar with, and that we’ll take a look at in more detail in section 5.2, is the summation function. In math, this is typically represented with a Σ. In Python, we have access to the sum function from the base library. The summation function takes a sequence of numbers (integers, floats, imaginary numbers) and returns a single number that is the total of all the numbers in the sequence added together (figure 5.1).