Data Analysis with Python: From Raw Data to Real-World Insights
BOOK 2 · MATHEMATICS & STATISTICS · CHAPTER 5

Coding Challenge: Exploring Cultural Production

On this page

Coding Challenge: Exploring Cultural Production

This project simulates a small dataset of literary production across countries and decades using NumPy. The goal is to practice the main array operations learned in this chapter — from array creation and slicing to matrix manipulation, mathematical functions, and random data visualization.

Background

Imagine a researcher studying how many books were published in three fictional countries — Aland, Bretoria, and Caldara — across five decades (1970–2010). The researcher wants to analyze patterns, compute statistics, and visualize variation using NumPy.

Task 1: Create Arrays and Matrices

Requirements: - Create a NumPy array decades representing the years [1970, 1980, 1990, 2000, 2010]. - Create a 3×5 matrix books_published showing the number of books published in each decade for the three countries below:

Country 1970 1980 1990 2000 2010
Aland 120 150 180 210 190
Bretoria 80 100 120 160 220
Caldara 90 130 170 200 250

Starter Code:

import numpy as np

decades = np.array([1970, 1980, 1990, 2000, 2010])
books_published = np.array([[120,150,180,210,190],[80,100,120,160,220],[90,130,170,200,250]])

print("Decades:", decades)
print("Books Published:\n", books_published)
print("Matrix shape:", books_published.shape)

Expected Output:

Decades: [1970 1980 1990 2000 2010]
Books Published:
 [[120 150 180 210 190]
  [ 80 100 120 160 220]
  [ 90 130 170 200 250]]
Matrix shape: (3, 5)

Task 2: Perform Basic Operations

Requirement:

  • Compute the average number of books published by each country (hint: row mean).
  • Compute the average number of books for each decade (hint: column mean).
  • Find the total number of books published overall.

Expected Output:

Average by country: [170. 136. 168.]
Average by decade: [96.67 126.67 156.67 190.  220.]
Total books published: 2370

Task 3: Indexing and Slicing

Requirement:

  • Extract the publication data for Bretoria only.
  • Select the data for the 1980–2000 decades for all countries.
  • Find how many books Caldara published in 2010.

Expected Output:

Bretoria: [ 80 100 120 160 220]
1980–2000 Data:
 [[150 180 210]
  [100 120 160]
  [130 170 200]]
Caldara (2010): 250

Task 4: Boolean Selection

Requirement:

  • Find all values greater than 200 in the dataset.
  • Count how many such values exist.

Expected Output:

Values > 200: [210 220 250]
Count: 3

Task 5: Apply Universal Functions

Requirement:

  • Compute the logarithm of all publication counts (using np.log()).
  • Compute the standard deviation of all values.
  • Compute the square root of each country’s average number of books.

Expected Output:

Logarithms:
 [[4.79 5.01 5.19 5.35 5.25]
  [4.38 4.61 4.79 5.08 5.39]
  [4.5  4.87 5.14 5.3  5.52]]
Standard deviation: 49.15
Square roots of averages: [13.04 11.66 12.96]

Task 6: Generate Random Data (Simulation)

Requirement:

  • Create a new 3×5 matrix books_random representing simulated book production values using random integers between 50 and 300.
  • Compute and print the following:
    • The random dataset.
    • The maximum and minimum values in the matrix.
    • The mean of all values.
  • Compare the simulated averages with the specified teaching data to discuss potential variability.

Reference output using np.random.default_rng(23).integers(50, 301, size=(3, 5)):

Randomly Simulated Books Published:
[[ 59 224 156 211 116]
 [ 82 202  78  80 213]
 [215 264 159 100 100]]
Max: 264
Min: 59
Mean: 150.6

Conclusion

In this challenge, NumPy was used to create and analyze structured numerical data representing cultural production. Through deterministic and random examples, the exercises demonstrated how arrays, matrices, and universal functions turn Python into a flexible environment for quantitative exploration.

In the next chapter, we move from raw numbers to structured datasets with Pandas, where data gains labels, meaning, and relationships — the bridge between numerical analysis and human interpretation.

Teaching edition · verified experiments · PDF and EPUB editions not generated.