liveBook · Manning

Probability distribution

As explained in Starting Data Analytics with Generative AI and Python

The pattern of values and their frequencies for a variable or measurement.

Which products receive mostly high review scores, and which receive a mix? A distribution shows how often each score occurs. Here, it is an observed pattern of counts, not a fitted probability model or a single satisfaction number. section 3.3.3

To compare categories and sellers, bring review scores, orders, order items, products, and category names together. Then count records for each category–seller–score combination. If an AI assistant suggests the workflow, first check that it has the DataFrame structure right. section 3.3.3

# Merge the DataFrames
merged_data = (
    df_orders.merge(df_order_items, on='order_id')
    .merge(df_products, on='product_id')
    .merge(df_order_reviews, on='order_id')
    .merge(df_product_category_translation, on='product_category_name')
)

# Group by product_category_name_english, seller_id, and review_score
review_score_distribution = 
merged_data.groupby(['product_category_name_english', 'seller_id', 
'review_score']).size().reset_index(name='count')

The pandas listing shows the joins and groupby operation that produce score counts by category and seller. section 3.3.3

A grouped bar chart makes category-level counts easier to inspect. If too many score bars or product categories obscure the pattern, scores can be grouped as positive (4–5) or negative (below 4), and product categories can be combined into broader groups. Check that these choices preserve the distinction needed for the business question. section 3.3.3

Listing 3.1 Generating a grouped bar plot
import seaborn as sns
import matplotlib.pyplot as plt

def categorize_review_score(score):
    if score >= 4:
        return 'Positive'
    else:
        return 'Negative'

review_score_distribution['review_category'] = 
review_score_distribution['review_score'].apply(categorize_review_score)

# Set a larger figure size
plt.figure(figsize=(20, 10))

# Create the grouped bar chart
sns.barplot(data=review_score_distribution,
    x='product_category_name_english',
    y='count',
    hue='review_category',
    ci=None)

# Rotate the x-axis labels for better readability
plt.xticks(rotation=90)

# Set labels for the axes and the title
plt.xlabel('Product Categories')
plt.ylabel('Review Count')
plt.title('Simplified Review Score Distribution by Product Category')

# Show the plot
plt.show()

The listing turns individual scores into positive and negative labels for a simpler grouped bar chart. section 3.3.3

The same idea offers a quick check on keyword-extracted positive reviews: plot their scores from 1 to 5 and look for a concentration at 4 or 5. Do not treat that pattern as proof. Positive reviews may lack the chosen keywords, and a negative review may contain one; sensitivity and specificity provide another way to assess the extraction. section 5.5.7

How Starting Data Analytics with Generative AI and Python covers it

How deep it goes
Works through grouped counts, bar charts, and score aggregation without developing a theoretical probability model.
Examples
Uses product-review scores by category and seller, plus a score plot for reviews extracted using positive keywords.
What it assumes
Assumes access to review-score data and related DataFrames while showing how to join, group, and plot them.
What you can do after
Apply it via pandas and seaborn to count and plot review scores, then cautiously check a positive-review extraction.
How it connects
Connects distributions to DataFrame joins, groupby, data visualization, keyword extraction, sensitivity, and specificity.

Also explained in

Compare how 27 books explain probability distribution →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Starting Data Analytics with Generative AI and Python. The section links point to the parts of the book it draws on.