Bird
0
0

What will be the output of this code snippet?

medium📝 Predict Output Q4 of 15
SciPy - Integration with Scientific Ecosystem
What will be the output of this code snippet?
import pandas as pd
from scipy import stats

df = pd.DataFrame({'A': [1, 2, 3, 4, 5], 'B': [5, 4, 3, 2, 1]})
correlation, p_value = stats.pearsonr(df['A'], df['B'])
print(round(correlation, 2))
A-1.00
B1.00
C0.00
D0.50
Step-by-Step Solution
Solution:
  1. Step 1: Understand data columns

    Column 'A' increases from 1 to 5, 'B' decreases from 5 to 1, so they are perfectly negatively correlated.
  2. Step 2: Calculate Pearson correlation

    stats.pearsonr returns correlation coefficient; here it will be -1.0 due to perfect negative correlation.
  3. Final Answer:

    -1.00 -> Option A
  4. Quick Check:

    Pearson correlation = -1.00 [OK]
Quick Trick: Opposite trends give -1 correlation [OK]
Common Mistakes:
  • Assuming positive correlation
  • Confusing correlation with p-value
  • Rounding errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes