Bird
Raised Fist0
Data Analysis Pythondata~20 mins

Reproducible analysis patterns in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Reproducible Analysis Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this code using a fixed random seed?
This code sets a random seed before generating random numbers. What will be the printed output?
Data Analysis Python
import numpy as np
np.random.seed(42)
random_numbers = np.random.rand(3)
print(random_numbers)
A[0.37454012 0.95071431 0.73199394]
B[0.37454012 0.95071431 0.59865848]
C[0.5488135 0.71518937 0.60276338]
D[0.64589411 0.43758721 0.891773 ]
Attempts:
2 left
💡 Hint
Setting a random seed fixes the sequence of random numbers generated.
❓ data_output
intermediate
2:00remaining
What is the shape of the DataFrame after filtering?
Given this DataFrame and filtering code, what is the shape of the resulting DataFrame?
Data Analysis Python
import pandas as pd
data = {'A': [1, 2, 3, 4], 'B': [5, 6, 7, 8]}
df = pd.DataFrame(data)
filtered_df = df[df['A'] > 2]
A(3, 1)
B(3, 2)
C(2, 2)
D(2, 1)
Attempts:
2 left
💡 Hint
Count how many rows have 'A' values greater than 2.
❓ visualization
advanced
2:00remaining
Which plot shows a reproducible histogram with fixed bins?
You want to create a histogram that looks the same every time you run the code. Which option produces a reproducible histogram with fixed bins?
Data Analysis Python
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(0)
data = np.random.randn(1000)
Aplt.hist(data, bins=np.linspace(-4,4,21)); plt.show()
Bplt.hist(data, bins=20); plt.show()
Cplt.hist(data); plt.show()
Dplt.hist(data, bins='auto'); plt.show()
Attempts:
2 left
💡 Hint
Fixed bins mean the bin edges are explicitly set.
🔧 Debug
advanced
2:00remaining
What error does this code raise when saving a DataFrame without specifying index?
This code saves a DataFrame to CSV. What error or issue occurs?
Data Analysis Python
import pandas as pd
df = pd.DataFrame({'x':[1,2], 'y':[3,4]})
df.to_csv('output.csv', index=False)
with open('output.csv') as f:
    lines = f.readlines()
print(lines[0])
AIndexError
B'0,x,y\n'
CFileNotFoundError
D'x,y\n'
Attempts:
2 left
💡 Hint
Check the first line of the CSV file when index=False is used.
🚀 Application
expert
3:00remaining
Which option ensures reproducible train-test split in scikit-learn?
You want to split your dataset into training and testing sets reproducibly. Which code snippet guarantees the same split every time?
Data Analysis Python
from sklearn.model_selection import train_test_split
X = [[i] for i in range(10)]
y = [0,1,0,1,0,1,0,1,0,1]
Atrain_test_split(X, y, test_size=0.3, shuffle=False)
Btrain_test_split(X, y, test_size=0.3, random_state=42)
Ctrain_test_split(X, y, test_size=0.3)
Dtrain_test_split(X, y, test_size=0.3, random_state=None)
Attempts:
2 left
💡 Hint
Setting random_state fixes the randomness of the split.

Practice

(1/5)
1. What is the main goal of reproducible analysis in data science?
easy
A. To ensure the analysis can be repeated with the same results
B. To make the code run faster
C. To use the latest software versions
D. To create complex visualizations

Solution

  1. Step 1: Understand reproducible analysis meaning

    Reproducible analysis means you can run your work again and get the same results.
  2. Step 2: Identify the main goal

    The main goal is to make sure the analysis is repeatable and reliable.
  3. Final Answer:

    To ensure the analysis can be repeated with the same results -> Option A
  4. Quick Check:

    Reproducible analysis = repeatable results [OK]
Hint: Focus on repeatability and consistent results [OK]
Common Mistakes:
  • Thinking speed or software version is the main goal
  • Confusing reproducibility with visualization quality
2. Which of the following is the correct way to define a function in Python for reproducible analysis?
easy
A. def load_data(): return pd.read_csv('data.csv')
B. function load_data() { return read.csv('data.csv') }
C. load_data() => pd.read_csv('data.csv')
D. def load_data: return pd.read_csv('data.csv')

Solution

  1. Step 1: Identify Python function syntax

    Python functions start with 'def', followed by name and parentheses, then a colon.
  2. Step 2: Check each option

    def load_data(): return pd.read_csv('data.csv') uses correct Python syntax. function load_data() { return read.csv('data.csv') } is JavaScript style. load_data() => pd.read_csv('data.csv') is incorrect Python syntax. def load_data: return pd.read_csv('data.csv') misses parentheses after function name.
  3. Final Answer:

    def load_data():\n return pd.read_csv('data.csv') -> Option A
  4. Quick Check:

    Python function = def name(): [OK]
Hint: Remember Python functions use def name(): [OK]
Common Mistakes:
  • Using JavaScript or other language syntax
  • Omitting parentheses after function name
  • Missing colon at the end of function definition
3. Given the code below, what will be the output of print(cleaned_data.shape)?
import pandas as pd

def load_data():
    return pd.DataFrame({'A': [1, 2, None], 'B': [4, None, 6]})

def clean_data(df):
    return df.dropna()

data = load_data()
cleaned_data = clean_data(data)
print(cleaned_data.shape)
medium
A. (2, 2)
B. (0, 2)
C. (3, 2)
D. (1, 2)

Solution

  1. Step 1: Understand the data and cleaning

    Original data has 3 rows and 2 columns. Rows with null are dropped by dropna(). Only the first row has no null values.
  2. Step 2: Count remaining rows and columns

    After dropna(), only 1 row remains with 2 columns, so shape is (1, 2).
  3. Final Answer:

    (1, 2) -> Option D
  4. Quick Check:

    dropna() removes rows with null = (1, 2) [OK]
Hint: dropna() removes rows with missing values [OK]
Common Mistakes:
  • Counting rows with null as valid
  • Confusing columns count
  • Assuming dropna() removes columns
4. Identify the error in the following reproducible analysis script:
def analyze_data():
    data = pd.read_csv('data.csv')
    cleaned = data.dropna
    return cleaned.describe()

result = analyze_data()
print(result)
medium
A. Incorrect file path in read_csv
B. Missing parentheses after dropna method call
C. Missing return statement in function
D. print statement syntax error

Solution

  1. Step 1: Check method usage in code

    dropna is a method and must be called with parentheses: dropna()
  2. Step 2: Identify error effect

    Without parentheses, cleaned is a method object, not a DataFrame, causing error on describe() call.
  3. Final Answer:

    Missing parentheses after dropna method call -> Option B
  4. Quick Check:

    Method call needs () = error if missing [OK]
Hint: Always add () when calling methods [OK]
Common Mistakes:
  • Forgetting parentheses on method calls
  • Assuming dropna is a property, not a method
  • Ignoring error messages about method objects
5. You want to create a reproducible analysis script that loads data, cleans it by removing rows with missing values, and then calculates the mean of column 'score'. Which of the following code snippets correctly implements this pattern?
hard
A. def load_data: return pd.read_csv('data.csv') clean_data = lambda df: df.dropna() mean_score = clean_data(data)['score'].mean() print(mean_score)
B. data = pd.read_csv('data.csv') clean = data.dropna mean_score = clean['score'].mean() print(mean_score)
C. def load_data(): return pd.read_csv('data.csv') def clean_data(df): return df.dropna() def analyze(df): return df['score'].mean() # Run full analysis raw = load_data() clean = clean_data(raw) result = analyze(clean) print(result)
D. def load_data(): return pd.read_csv('data.csv') clean_data = data.dropna() mean_score = clean_data['score'].mean() print(mean_score)

Solution

  1. Step 1: Check function definitions and calls

    def load_data(): return pd.read_csv('data.csv') def clean_data(df): return df.dropna() def analyze(df): return df['score'].mean() # Run full analysis raw = load_data() clean = clean_data(raw) result = analyze(clean) print(result) defines functions for loading, cleaning, and analyzing, then runs them in order, ensuring reproducibility.
  2. Step 2: Verify correctness of each step

    Functions use correct syntax, dropna() is called properly, and mean is calculated on cleaned data.
  3. Final Answer:

    The code that defines functions for loading, cleaning, and analyzing, then calls them sequentially -> Option C
  4. Quick Check:

    Functions + proper calls = reproducible script [OK]
Hint: Use functions and call them in sequence [OK]
Common Mistakes:
  • Forgetting parentheses on dropna()
  • Not defining functions properly
  • Using variables before assignment
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard