Programming in Python

Chapter 12
Scientific Computing, Analytics and Visualization

Bachelors Program in AI & ML
Programming in Python   •   Bachelor

Scientific Computing, Analytics and Visualization

Introduction and Context

Scientific computing is the use of computational methods to represent, manipulate, analyze, and visualize data arising from science, engineering, business, and public decision-making. In Python, the central ecosystem for this work includes NumPy for numerical arrays and vectorized computation, pandas for labeled tabular data, and visualization libraries such as Matplotlib and Seaborn. Together, these tools support a complete analytical workflow: obtaining data, inspecting its structure, cleaning and transforming it, exploring patterns, summarizing evidence, and communicating conclusions.

The importance of this workflow extends beyond writing code. A data analyst or AI practitioner must decide whether data are suitable for a question, whether transformations preserve meaning, whether a visualization is honest, and whether a conclusion is sufficiently supported by evidence. Efficient syntax cannot compensate for poor measurement, biased samples, incorrect assumptions, or misleading graphics.

This chapter develops the conceptual and practical foundations required to use NumPy, pandas, and visualization libraries effectively. Examples use Python-style code and small datasets. The code assumes that NumPy, pandas, Matplotlib, and Seaborn have been installed and imported as follows:

```python

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

import seaborn as sns

```

Learning Objectives

After studying this chapter, you should be able to:

1. Explain the difference between Python sequences, NumPy arrays, pandas Series, and pandas DataFrames.

2. Apply vectorized operations, broadcasting, indexing, aggregation, and reshaping.

3. Inspect, clean, validate, and transform real-world tabular data.

4. Conduct exploratory data analysis using numerical summaries and appropriate plots.

5. Select visual encodings that support accurate interpretation.

6. Compute and interpret descriptive statistics while recognizing their limitations.

7. Build an evidence-based analytical recommendation.

8. Identify reproducibility, privacy, fairness, and communication risks in analytical work.

The Computational Data Model

Arrays and vectorized computation

A NumPy array is a homogeneous, multidimensional data structure. “Homogeneous” means that its elements generally share a common data type, such as integer, floating-point, or Boolean. Arrays are useful because many operations are implemented in optimized compiled code rather than through explicit Python loops.

```python

x = np.array([2, 4, 6, 8])

y = x * 3

```

The result is `array([6, 12, 18, 24])`. The multiplication is vectorized: one operation is applied to every element. This is usually clearer and faster than manually iterating over the elements.

For an array containing values \(x_1, x_2, \ldots, x_n\), multiplying by a scalar \(c\) produces

\[

y_i = cx_i, \quad i=1,2,\ldots,n.

\]

Here, \(c\) and every \(x_i\) must be numerically compatible. Vectorization does not mean that a calculation is automatically statistically correct; it only describes how the computation is expressed and executed.

Important array attributes include:

```python

x.shape # dimensions

x.ndim # number of dimensions

x.size # number of elements

x.dtype # element data type

```

A two-dimensional array can represent a matrix:

```python

A = np.array([[1, 2, 3],

[4, 5, 6]])

```

Its shape is `(2, 3)`, meaning two rows and three columns. In scientific computing, consistent shape interpretation is essential. A row may represent an observation and a column a variable, but this convention should be documented rather than assumed.

Broadcasting

Broadcasting permits arithmetic between arrays of compatible shapes. For example:

```python

scores = np.array([[70, 80, 90],

[60, 75, 85]])

bonus = np.array([5, 0, 2])

adjusted = scores + bonus

```

The one-dimensional array is added to each row. In simplified terms, NumPy compares dimensions from right to left. Two dimensions are compatible when they are equal or when one of them is 1. If shapes are incompatible, NumPy raises an error rather than silently guessing.

Broadcasting is powerful for unit conversions, calibration, normalization, and applying feature-specific parameters. However, it can also produce a valid-looking but conceptually wrong result if the dimensions do not represent the intended variables. Analysts should therefore verify shapes before and after transformations.

Indexing, masking, and missing numerical values

NumPy supports positional indexing and Boolean masking:

```python

x[0] # first element

x[1:3] # elements at positions 1 and 2

x[x > 5] # elements satisfying a condition

```

A Boolean mask has one Boolean value per selected dimension. Masks are useful for filtering observations, but the analyst should record the rule used. A filter such as `temperature > 30` has a different meaning from replacing values above 30.

Numerical missing values are commonly represented by `np.nan`, a floating-point “not a number” value. Comparisons involving `np.nan` often return false, and ordinary arithmetic may propagate it. Functions such as `np.nanmean` calculate means while ignoring NaN values, but omission is appropriate only when the missingness mechanism and analytical purpose justify it.

Series and DataFrames

A pandas Series is a one-dimensional labeled array. A DataFrame is a two-dimensional labeled table whose columns may have different data types.

```python

sales = pd.DataFrame({

"region": ["North", "South", "North"],

"units": [10, 7, 12],

"revenue": [250.0, 180.0, 310.0]

})

```

Labels make pandas particularly suitable for business and research data. Unlike a bare matrix, a DataFrame can preserve variable names and an index. This improves readability but does not eliminate the need to understand data types and units.

Common inspection commands are:

```python

sales.head()

sales.shape

sales.info()

sales.describe(include="all")

sales.isna().sum()

sales.nunique()

```

`info()` reveals column types and non-null counts. `describe()` provides numerical summaries for numeric columns and, with `include="all"`, summaries for other types where applicable.

Data Cleaning and Preparation

Data cleaning is not merely a preliminary programming task. It is a reasoning process that connects recorded values to the real-world phenomena they are intended to represent.

Data types and parsing

Dates should be parsed as dates rather than retained as arbitrary strings:

```python

df["date"] = pd.to_datetime(df["date"], errors="coerce")

```

The option `errors="coerce"` converts unparseable values to missing values. This prevents a program from failing, but it also hides the original error unless invalid records are inspected. A robust workflow counts and reviews coerced values.

Numeric fields may contain currency symbols, commas, or text:

```python

df["amount"] = (

df["amount"].astype("string")

.str.replace(",", "", regex=False)

.str.replace("$", "", regex=False)

.pipe(pd.to_numeric, errors="coerce")

)

```

The resulting unit should be documented. A column called `amount` is less informative than `amount_usd` or `amount_thousands_usd`.

Missing data

Missingness can arise because a value was not measured, a respondent refused to answer, a sensor failed, or a value was not applicable. These causes are analytically different.

Useful strategies include:

  • deleting rows or columns when the loss is small and defensible;
  • replacing values with a constant only when that constant has substantive meaning;
  • imputing with a median or other model-based estimate;
  • retaining a missingness indicator;
  • reporting missingness as part of the result.

Mean imputation reduces visible variation and can distort relationships. Deleting all incomplete rows can introduce bias if missingness is related to the outcome or to a subgroup. No method is universally correct; the choice must be tied to the data-generating process and documented.

Duplicates, invalid values, and consistency checks

Duplicates may be legitimate repeated events or accidental repeated records. Before using `drop_duplicates()`, identify the key that defines a unique observation:

```python

duplicates = df[df.duplicated(subset=["customer_id", "date"], keep=False)]

df = df.drop_duplicates(subset=["customer_id", "date"])

```

Range and logical checks can expose errors:

```python

invalid = df[(df["age"] < 0) | (df["age"] > 120)]

logical_error = df[df["end_time"] < df["start_time"]]

```

A value outside an expected range is not necessarily false. It may indicate a special population, a unit error, or a genuine exceptional event. Flagging is often preferable to automatic deletion.

Categorical data and standardized labels

Text categories frequently differ only in capitalization or whitespace:

```python

df["department"] = df["department"].str.strip().str.title()

```

A controlled vocabulary is better than unconstrained text entry. For example, `"North"`, `"north "`, and `"NORTH"` should not become three analytical groups. Category consolidation must be guided by domain knowledge, especially when labels encode legally or socially important identities.

Descriptive Analytics

Descriptive analytics summarizes observed data; it does not, by itself, establish causation or predict future outcomes.

For observations \(x_1, x_2, \ldots, x_n\), the arithmetic mean is

\[

\bar{x} = \frac{1}{n}\sum_{i=1}^{n} x_i.

\]

The mean uses every observed value and is sensitive to extreme values. The median is the middle value after sorting, or the average of the two middle values when there is an even number of observations. It is more resistant to outliers.

The population variance is

\[

\sigma^2 = \frac{1}{N}\sum_{i=1}^{N}(x_i-\mu)^2,

\]

where \(N\) is the population size and \(\mu\) is the population mean. When estimating a population variance from a sample, the commonly used unbiased sample variance is

\[

s^2 = \frac{1}{n-1}\sum_{i=1}^{n}(x_i-\bar{x})^2,

\]

where \(n>1\). The corresponding sample standard deviation is \(s=\sqrt{s^2}\), in the same units as the observations. The distinction between denominators matters: pandas' default sample standard deviation uses `ddof=1`, while some NumPy operations default to `ddof=0`.

The interquartile range is

\[

IQR=Q_3-Q_1,

\]

where \(Q_1\) and \(Q_3\) are the first and third quartiles. A common exploratory rule flags values below \(Q_1-1.5IQR\) or above \(Q_3+1.5IQR\). This is a screening rule, not proof that a value is erroneous.

Pandas supports grouped summaries:

```python

summary = (

df.groupby("region", as_index=False)

.agg(mean_sales=("sales", "mean"),

median_sales=("sales", "median"),

observations=("sales", "size"))

)

```

Group sizes should accompany group means. A mean based on three observations should not be presented with the same confidence as a mean based on three thousand observations.

A weighted mean is appropriate when observations contribute unequal weights:

\[

\bar{x}_w = \frac{\sum_{i=1}^{n}w_i x_i}{\sum_{i=1}^{n}w_i},

\]

where weights \(w_i\) are nonnegative and their meaning is known. Weights may represent sampling expansion factors, exposure, or importance, but they should not be added merely to make a desired conclusion appear stronger.

Correlation summarizes linear association. The Pearson correlation coefficient is

\[

r = \frac{\sum_i(x_i-\bar{x})(y_i-\bar{y})}

{\sqrt{\sum_i(x_i-\bar{x})^2}\sqrt{\sum_i(y_i-\bar{y})^2}},

\]

provided both variables have nonzero variation. It lies between -1 and 1. Correlation does not establish causation, can be distorted by outliers, and may conceal nonlinear relationships or subgroup differences.

Exploratory Data Analysis

Exploratory data analysis (EDA) combines computation and visualization to understand distributions, relationships, unusual observations, and data quality. EDA is iterative rather than a fixed sequence.

A practical cycle is:

1. Clarify the decision or question.

2. Inspect dimensions, types, units, and missingness.

3. Examine univariate distributions.

4. Compare relevant groups.

5. Investigate relationships among variables.

6. Check unusual observations and alternative explanations.

7. Record findings and revise the question if necessary.

Univariate analysis

Histograms show the distribution of a numerical variable. Box plots display the median, quartiles, and potential outliers. For categorical variables, count plots show the frequency of each category.

```python

sns.histplot(data=df, x="delivery_days", bins=15, kde=True)

plt.xlabel("Delivery time (days)")

plt.ylabel("Number of orders")

plt.show()

```

The number of bins affects the visual story. Too few bins hide structure; too many create visual noise. Bins should be selected deliberately and reported when the analysis is important.

Bivariate and multivariate analysis

Scatter plots are useful for two numerical variables. A trend line may summarize association but should not be interpreted as proof of a causal mechanism. Grouped box plots compare distributions across categories:

```python

sns.boxplot(data=df, x="region", y="delivery_days")

```

A correlation heatmap can help identify redundant numerical variables, but color scales must be labeled and the matrix must not be mistaken for a causal model. Faceting, which creates separate panels by a category, can reveal patterns hidden in an aggregate plot.

Visualization as Evidence and Communication

A visualization is an argument encoded through visual marks. Its design should be driven by the question, data type, audience, and decision context.

  • Position along a common scale is generally effective for comparing quantities.
  • Length is useful for bars.
  • Area and angle are less precise for quantitative comparison.
  • Color hue is appropriate for categories; color intensity can represent ordered magnitude, but only with a meaningful scale.
  • Lines imply an ordered or continuous progression and should not connect unrelated categories.

Bar charts should generally begin at zero when bar length represents magnitude. A truncated axis can exaggerate differences. Line charts should use an appropriate time order and should not imply continuity where observations are isolated categories.

A useful analytical chart contains a descriptive title, clearly labeled axes, units, readable scales, and a legend only when needed. Decorative effects such as three-dimensional bars often reduce accuracy. Missing values, excluded groups, and transformations should be disclosed.

Matplotlib provides detailed control, while Seaborn offers convenient statistical graphics built on Matplotlib. Both support reproducible code-based figures. For publication or reporting, specify figure dimensions and resolution:

```python

fig, ax = plt.subplots(figsize=(8, 4.5))

sns.barplot(data=summary, x="region", y="mean_sales", ax=ax)

ax.set_title("Mean sales by region")

ax.set_ylabel("Mean sales (currency units)")

fig.tight_layout()

```

Color accessibility matters. Use palettes that remain distinguishable for people with color-vision deficiencies, and do not rely on color alone to communicate a critical distinction. A plot should remain interpretable in grayscale where feasible.

Worked Example: Regional Service Performance

Consider an illustrative dataset of service requests. Each row represents one completed request, `region` identifies the service area, `response_hours` records elapsed hours from request to first response, and `satisfaction` is a customer rating from 1 to 5.

```python

service = pd.DataFrame({

"region": ["North", "North", "South", "South", "East", "East", "East"],

"response_hours": [8, 12, 20, np.nan, 10, 11, 40],

"satisfaction": [5, 4, 3, 3, 4, 5, 2]

})

```

First inspect missingness and group sizes:

```python

service.isna().sum()

service.groupby("region").size()

```

The South region has one missing response time. Its satisfaction value is still available, so deleting the entire row would discard information. For a response-time summary, one defensible descriptive choice is to exclude only the missing response time and report that exclusion.

The observed North response times are 8 and 12 hours, giving a mean of

\[

(8+12)/2=10\text{ hours}.

\]

The East values are 10, 11, and 40 hours, giving

\[

(10+11+40)/3=61/3\approx20.33\text{ hours}.

\]

The East mean is substantially influenced by the 40-hour observation. Its median is 11 hours, illustrating why both mean and median should be reported when skew or extreme observations are plausible. The 40-hour case should be investigated rather than automatically removed: it might represent a genuine service failure, an unusual request, or a recording error.

A suitable exploratory plot is:

```python

sns.boxplot(data=service, x="region", y="response_hours")

plt.ylabel("Response time (hours)")

plt.title("Response-time distributions by region")

plt.show()

```

An evidence-based operational recommendation would not be “East is always poor.” A more defensible statement is: “In this small illustrative sample, East has a higher mean response time than North, while its median is close to North’s values because one 40-hour case is influential. Investigate the exceptional case and collect a larger, representative sample before allocating permanent resources.” This conclusion appropriately distinguishes observation from generalization.

Meaningful Applied Case: Retail Inventory Decision

Background

A fictional mid-sized retailer, Horizon Home, experiences frequent stockouts for some products while holding excess inventory for others. Management asks an analyst to identify whether replenishment priorities should change across stores.

Problem

The company has twelve months of transaction data containing store, product category, units sold, selling price, inventory on hand, and transaction date. The data contain duplicated transaction identifiers, missing prices, inconsistent category labels, and several negative inventory values.

Analysis

The analyst first defines the unit of observation: a transaction line, not a product-day. This prevents accidental aggregation of already aggregated records. Transaction identifiers are checked for duplicates. Negative inventory is not immediately deleted because it may represent backorders or timing differences between sales and inventory updates.

Dates are parsed and transactions are grouped by store, category, and month. Missing prices are examined by source system and category. Rather than replacing all missing prices with a global mean, the analyst separates records with unresolved prices and reports their proportion. Category labels are standardized using a controlled mapping.

The analyst computes monthly units sold, average inventory, stockout frequency, and revenue. A line chart displays monthly stockouts; a heatmap shows category-by-store patterns; and a scatter plot compares average inventory with units sold. Group sizes and data coverage are included in the report.

Approach and solution

The team identifies two distinct issues. Several high-demand categories have low inventory and repeated stockouts, while some slow-moving categories have high inventory. The analyst recommends a pilot policy rather than an immediate company-wide change:

1. increase reorder review frequency for high-demand categories with repeated stockouts;

2. set a separate clearance or transfer review for slow-moving excess stock;

3. investigate negative inventory records with the inventory-system owner;

4. monitor service level, stockout rate, holding cost, and customer substitution during the pilot;

5. compare pilot stores with similar non-pilot stores where feasible.

The analysis does not claim that the policy caused improvement until post-pilot evidence is collected. Seasonal effects, promotions, supplier delays, and store differences are recorded as possible confounders.

Outcome

In this illustrative case, the initial analysis produces a prioritized investigation list rather than a single universal reorder rule. Management gains a transparent basis for action and a measurement plan for evaluating the pilot.

Learning points

The case demonstrates that data cleaning, aggregation, visualization, and decision-making are inseparable. A technically elegant dashboard can still mislead if the unit of analysis is unclear, missing prices are hidden, or stockout definitions change across stores. Evidence-based decisions require explicit definitions, documented assumptions, and follow-up measurement.

Reproducibility, Ethics, and Governance

Reproducibility requires that another qualified person can understand and rerun the analysis. Use version-controlled scripts or notebooks, fixed random seeds when randomness is used, documented package versions, clear input files, and preserved transformation steps. Avoid changing source data manually without an audit trail.

Privacy must be considered before loading data into notebooks or visualization systems. Remove unnecessary identifiers, restrict access, and avoid publishing small groups whose identities could be inferred. Aggregation does not guarantee anonymity when external information can be combined with the output.

Fairness concerns arise when descriptive patterns are used to allocate resources or evaluate people. A group with lower recorded performance may have poorer measurement coverage rather than poorer underlying performance. Analysts should examine whether data collection, missingness, and definitions differ across groups. Automated decision support should provide traceable evidence and meaningful human review, especially in high-impact settings.

AI systems depend on the same foundations. A machine-learning model trained on inconsistent types, duplicated records, leakage from future information, or biased labels may produce highly accurate-looking but unreliable results. NumPy and pandas remain important for feature construction, validation, and error analysis even when a specialized AI framework is used.

Challenges and Limitations

First, numerical precision can matter. Floating-point representations are approximations, so equality comparisons involving calculated decimal values may fail. Use tolerances where appropriate and use decimal arithmetic for applications requiring exact decimal accounting.

Second, performance depends on data size and operation design. Vectorization often improves performance, but a fully vectorized expression may use substantial memory. Chunked processing, categorical data types, or database operations may be preferable for very large datasets.

Third, descriptive statistics are not causal explanations. A higher sales figure after an advertising campaign may reflect seasonality, pricing, store expansion, or selection effects. Causal claims require an appropriate design, such as a randomized experiment or a carefully justified observational method.

Fourth, visual summaries can hide uncertainty. Small samples, missing observations, and measurement error should be visible in the report. Confidence intervals and formal inferential methods may be required when the question concerns generalization beyond the observed data.

Best Practices

  • Define the unit of observation and the analytical question before transforming data.
  • Preserve raw data and create documented cleaned versions.
  • Inspect shape, data types, missingness, duplicates, and ranges before analysis.
  • Prefer explicit column names containing units where relevant.
  • Use vectorized operations while checking array shapes and alignment.
  • Report group sizes alongside group statistics.
  • Examine both robust and non-robust summaries when outliers are plausible.
  • Choose charts according to the comparison or relationship being investigated.
  • Label axes, units, dates, denominators, and exclusions.
  • Validate conclusions against domain knowledge and alternative explanations.
  • Separate exploratory findings from confirmatory claims.
  • Make analytical code reproducible and protect sensitive data.

Contemporary Developments

Modern pandas workflows increasingly use efficient nullable data types, method chaining, and interoperability with columnar formats such as Parquet. NumPy continues to provide the numerical foundation for scientific Python, while alternative array systems can support parallel or accelerator-based computation. Interactive tools can help users filter and explore results, but interactivity should not replace documentation or statistical discipline.

Data contracts and automated validation are also becoming important. A data contract specifies expected columns, types, ranges, and update behavior. Validation tools can detect schema changes before they silently affect a report. In production AI and analytics systems, such controls support monitoring for data drift, missingness changes, and unexpected category values.

Chapter Summary

Scientific computing and analytics in Python combine efficient numerical computation with structured data management and visual reasoning. NumPy arrays support homogeneous data, vectorization, masking, broadcasting, and multidimensional computation. pandas Series and DataFrames add labels, heterogeneous columns, grouping, parsing, cleaning, and tabular transformations.

Reliable analysis begins with a clear unit of observation and careful inspection of types, missing values, duplicates, ranges, and category definitions. Descriptive statistics such as the mean, median, variance, standard deviation, interquartile range, weighted mean, and correlation summarize observed data, but each has assumptions and limitations. Exploratory data analysis uses numerical and visual methods to reveal distributions, group differences, relationships, and unusual observations.

Visualization is a form of evidence communication. Appropriate scales, labels, units, accessible color, honest axes, and disclosure of exclusions help audiences interpret results accurately. The retail inventory case illustrates how cleaning and aggregation support decisions without turning descriptive patterns into unsupported causal claims. Reproducibility, privacy, fairness, validation, and human review are essential when analytics informs organizational or AI-enabled decisions.

Key Terms

NumPy array: A homogeneous, multidimensional numerical data structure.

Vectorization: Applying an operation to an entire array or column without an explicit element-by-element Python loop.

Broadcasting: NumPy’s method for performing operations on arrays with compatible but different shapes.

Boolean mask: An array of Boolean conditions used to select or filter values.

Series: A one-dimensional pandas labeled data structure.

DataFrame: A two-dimensional pandas table with labeled rows and columns.

Data type: The representation and permitted kind of values in a data field.

Missing data: Observations for which a value is unavailable, unrecorded, or not applicable.

Imputation: Replacing missing values using a defined rule or model.

Outlier: An observation that is unusually distant from other observations in a dataset.

Mean: The arithmetic average of numerical observations.

Median: The middle value of an ordered dataset.

Variance: A measure based on squared deviations from a mean.

Interquartile range: The difference between the third and first quartiles.

Correlation: A numerical summary of association, commonly linear association, between variables.

Exploratory data analysis: Iterative examination of data using summaries, transformations, and visualizations.

Vectorized aggregation: Computing summaries over arrays or groups using library operations.

Data visualization: Visual representation of data to support analysis and communication.

Reproducibility: The ability to repeat an analysis using documented data, code, and computational conditions.

Data contract: A documented agreement about expected data structure, types, quality, and delivery behavior.

Review Questions

Short Answer Questions

1. What is vectorization, and why is it useful in NumPy?

2. Distinguish a pandas Series from a pandas DataFrame.

3. Why should group sizes be reported with group means?

4. What does the interquartile range measure?

5. State two practices that improve visualization accessibility.

Long Answer Questions

1. Explain a complete data-cleaning workflow for a dataset containing missing values, duplicate records, inconsistent categories, and improperly formatted dates.

2. Compare the mean, median, standard deviation, and interquartile range, including situations in which each is useful.

3. Explain broadcasting in NumPy and discuss how shape errors can produce conceptually incorrect analysis.

4. Discuss why correlation does not establish causation, using an applied example.

5. Describe how reproducibility, privacy, and fairness should influence an analytics project.

Analytical / Application Questions

1. A dataset contains response times of 4, 5, 6, and 45 hours. Calculate the mean and median, identify which is more affected by the extreme value, and propose an appropriate visualization.

2. Design a pandas and visualization workflow to determine whether a retailer’s stockouts differ by store and product category. State the checks you would perform before making a recommendation.

3. A dashboard shows that one demographic group has lower average performance, but that group also has twice the missing-data rate. Analyze why the dashboard may be misleading and propose additional evidence to collect.

Multiple Choice Questions with Answers

Question 1

Which NumPy operation is vectorized?

A. Applying a scalar multiplication to every element of an array

B. Writing a separate print statement for every value

C. Manually entering each array result

D. Converting every value to a string before addition

Correct Answer: A. Applying a scalar multiplication to every element of an array

Question 2

What does the shape (2, 3) indicate for a NumPy array?

A. Three rows and two columns

B. Two rows and three columns

C. Six rows and zero columns

D. Two dimensions containing three arrays each

Correct Answer: B. Two rows and three columns

Question 3

Which pandas method is commonly used to inspect column types and non-null counts?

A. head()

B. unique()

C. info()

D. sort_values()

Correct Answer: C. info()

Question 4

For the values 8, 12, and 40, what is the arithmetic mean?

A. 12

B. 18

C. 20

D. 60

Correct Answer: C. 20

Question 5

Why can median be preferable to mean for a highly skewed distribution?

A. Median always uses more observations

B. Median is less affected by extreme values

C. Median measures causal relationships

D. Median requires normally distributed data

Correct Answer: B. Median is less affected by extreme values

Question 6

Which visualization is generally most suitable for examining the distribution of one numerical variable?

A. Histogram

B. Pie chart

C. Network diagram

D. Treemap

Correct Answer: A. Histogram

Question 7

What is a necessary condition for Pearson correlation to be calculable in the usual formula?

A. Both variables must have nonzero variation

B. Both variables must be categorical

C. The sample must contain exactly ten observations

D. The variables must have identical units

Correct Answer: A. Both variables must have nonzero variation

Question 8

Why should an analyst investigate an outlier before deleting it?

A. Every outlier is automatically a data-entry error

B. It may represent a genuine and important event

C. Deleting it always increases sample bias

D. Outliers cannot affect descriptive statistics

Correct Answer: B. It may represent a genuine and important event

Question 9

Which practice best supports reproducibility?

A. Changing source data manually without a record

B. Using undocumented spreadsheet adjustments

C. Preserving code, inputs, transformations, and package information

D. Removing all unusual observations without explanation

Correct Answer: C. Preserving code, inputs, transformations, and package information

Question 10

What is the main limitation of interpreting a correlation as a causal effect?

A. Correlation cannot be negative

B. Correlation summarizes association and does not by itself rule out confounding

C. Correlation is only used for categorical data

D. Correlation cannot be computed from numerical data

Correct Answer: B. Correlation summarizes association and does not by itself rule out confounding

Programming in Python
Chapter 12 of 15