Bird
Raised Fist0
NumPydata~20 mins

Correlation coefficient with np.corrcoef() in NumPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Correlation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of np.corrcoef() with simple arrays
What is the output of this code snippet?
import numpy as np
x = np.array([1, 2, 3])
y = np.array([4, 5, 6])
result = np.corrcoef(x, y)
print(result)
NumPy
import numpy as np
x = np.array([1, 2, 3])
y = np.array([4, 5, 6])
result = np.corrcoef(x, y)
print(result)
A
[[1. 1.]
 [1. 1.]]
B
[[1. 0.]
 [0. 1.]]
C
[[0. 1.]
 [1. 0.]]
D
[[1. -1.]
 [-1. 1.]]
Attempts:
2 left
💡 Hint
Think about how correlation measures linear relationship between two arrays.
❓ data_output
intermediate
2:00remaining
Correlation matrix shape for multiple variables
Given three variables stored as arrays, what is the shape of the correlation matrix returned by np.corrcoef() when called as np.corrcoef(a, b, c)?
NumPy
import numpy as np
a = np.array([1, 2, 3])
b = np.array([3, 2, 1])
c = np.array([2, 2, 2])
result = np.corrcoef(a, b, c)
print(result.shape)
A(3,)
B(3, 3)
C(1, 3)
D(2, 2)
Attempts:
2 left
💡 Hint
The correlation matrix compares each variable with every other variable.
🔧 Debug
advanced
2:00remaining
Identify the error in np.corrcoef usage
What error will this code raise?
import numpy as np
x = [1, 2, 3]
y = [4, 5]
np.corrcoef(x, y)
NumPy
import numpy as np
x = [1, 2, 3]
y = [4, 5]
np.corrcoef(x, y)
ATypeError: unsupported operand type(s) for -: 'list' and 'list'
BNo error, returns correlation matrix
CIndexError: index out of range
DValueError: all the input arrays must have same length
Attempts:
2 left
💡 Hint
Think about the length of arrays passed to np.corrcoef.
🚀 Application
advanced
2:00remaining
Using np.corrcoef to find strongest correlation
You have three arrays representing different features. Which pair has the strongest positive correlation?
import numpy as np
f1 = np.array([1, 2, 3, 4])
f2 = np.array([4, 3, 2, 1])
f3 = np.array([2, 4, 6, 8])
cor_matrix = np.corrcoef(f1, f2, f3)
print(cor_matrix)
NumPy
import numpy as np
f1 = np.array([1, 2, 3, 4])
f2 = np.array([4, 3, 2, 1])
f3 = np.array([2, 4, 6, 8])
cor_matrix = np.corrcoef(f1, f2, f3)
print(cor_matrix)
Af1 and f3
BAll pairs have equal correlation
Cf2 and f3
Df1 and f2
Attempts:
2 left
💡 Hint
Look for pairs with correlation close to 1.
🧠 Conceptual
expert
2:00remaining
Interpretation of np.corrcoef output matrix
Given the correlation matrix below from np.corrcoef for variables A, B, and C:
[[ 1.   0.5 -0.3]
 [ 0.5  1.   0.1]
 [-0.3  0.1  1. ]]
Which statement is true?
AVariable B and C have a strong negative correlation.
BVariable A and C have a strong positive correlation.
CVariable A and B have a moderate positive correlation, and A and C have a weak negative correlation.
DAll variables are uncorrelated.
Attempts:
2 left
💡 Hint
Correlation values near 0 mean weak correlation; near 1 or -1 mean strong.

Practice

(1/5)
1. What does the function np.corrcoef() calculate in numpy?
easy
A. The correlation coefficient between two or more data sets
B. The sum of all elements in an array
C. The mean value of a data set
D. The standard deviation of a data set

Solution

  1. Step 1: Understand the purpose of np.corrcoef()

    This function calculates how two or more data sets move together, which is called the correlation coefficient.
  2. Step 2: Identify the correct description

    Among the options, only The correlation coefficient between two or more data sets describes the correlation coefficient calculation.
  3. Final Answer:

    The correlation coefficient between two or more data sets -> Option A
  4. Quick Check:

    np.corrcoef() = correlation coefficient [OK]
Hint: Correlation means how two sets move together [OK]
Common Mistakes:
  • Confusing correlation with sum or mean
  • Thinking it calculates standard deviation
  • Assuming it returns a single number always
2. Which of the following is the correct syntax to calculate the correlation coefficient matrix between arrays a and b using numpy?
easy
A. np.corrcoef(a, b)
B. np.corrcoef[a, b]
C. np.corrcoef(a & b)
D. np.corrcoef(a + b)

Solution

  1. Step 1: Recall the function call syntax

    Functions in numpy use parentheses () to pass arguments, so np.corrcoef(a, b) is correct.
  2. Step 2: Identify incorrect syntax

    Options B uses square brackets which is invalid for function calls; C uses bitwise AND which is wrong; D adds arrays which changes data meaning.
  3. Final Answer:

    np.corrcoef(a, b) -> Option A
  4. Quick Check:

    Use parentheses for function calls [OK]
Hint: Use parentheses () to call functions in numpy [OK]
Common Mistakes:
  • Using square brackets instead of parentheses
  • Using operators inside function arguments incorrectly
  • Passing combined arrays instead of separate arguments
3. What is the output of the following code?
import numpy as np
x = np.array([1, 2, 3])
y = np.array([4, 5, 6])
result = np.corrcoef(x, y)
print(result)
medium
A. [[0. 1.] [1. 0.]]
B. [[1. 1.] [1. 1.]]
C. [[1. 0.] [0. 1.]]
D. [[1. -1.] [-1. 1.]]

Solution

  1. Step 1: Calculate correlation between x and y

    Arrays x and y increase together perfectly, so their correlation coefficient is 1.
  2. Step 2: Understand np.corrcoef output matrix

    The diagonal is correlation of each array with itself (1), off-diagonal is correlation between x and y (1).
  3. Final Answer:

    [[1. 1.] [1. 1.]] is incorrect because the matrix is symmetric with 1 on diagonal and off-diagonal representing correlation between x and y.
  4. Correction:

    The actual output is
    [[1. 1.]
     [1. 1.]]
    which matches [[1. 1.] [1. 1.]], but numpy returns correlation matrix with 1 on diagonals and correlation coefficient off-diagonal.
  5. Quick Check:

    Perfect positive correlation = 1 [OK]
Hint: Perfectly increasing arrays have correlation 1 [OK]
Common Mistakes:
  • Confusing correlation with covariance
  • Misreading the matrix layout
  • Expecting zeros off-diagonal for correlated arrays
4. The code below is intended to calculate the correlation coefficient between two arrays a and b. What is the error?
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
result = np.corrcoef(a & b)
print(result)
medium
A. np.corrcoef() cannot handle numpy arrays
B. Using bitwise AND (&) instead of passing two arrays separately
C. Arrays a and b have different lengths
D. Missing import statement for numpy

Solution

  1. Step 1: Identify the incorrect operator usage

    The code uses bitwise AND (&) between arrays, which produces a new array, not two separate arrays.
  2. Step 2: Correct usage of np.corrcoef()

    np.corrcoef() expects separate arrays as arguments, like np.corrcoef(a, b), not combined with &.
  3. Final Answer:

    Using bitwise AND (&) instead of passing two arrays separately -> Option B
  4. Quick Check:

    Pass arrays separately, no & operator [OK]
Hint: Pass arrays as separate arguments, not combined with & [OK]
Common Mistakes:
  • Using & instead of comma to separate arrays
  • Assuming & merges arrays correctly
  • Ignoring error messages about argument types
5. Given two arrays representing daily temperatures in Celsius for two cities over a week:
city1 = np.array([20, 22, 21, 19, 18, 20, 23])
city2 = np.array([30, 32, 31, 29, 28, 30, 33])

How would you use np.corrcoef() to find if the temperatures in the two cities move together, and what does the off-diagonal value represent?
hard
A. Use np.corrcoef(city1); off-diagonal is correlation with itself
B. Use np.corrcoef(city1 + city2); off-diagonal is sum of temperatures
C. Use np.corrcoef(city1 * city2); off-diagonal is product correlation
D. Use np.corrcoef(city1, city2); off-diagonal is correlation between city1 and city2 temperatures

Solution

  1. Step 1: Apply np.corrcoef to two arrays

    Pass both city1 and city2 arrays as separate arguments: np.corrcoef(city1, city2).
  2. Step 2: Interpret the off-diagonal value

    The off-diagonal value in the resulting matrix shows the correlation coefficient between the two cities' temperatures, indicating how they move together.
  3. Final Answer:

    Use np.corrcoef(city1, city2); off-diagonal is correlation between city1 and city2 temperatures -> Option D
  4. Quick Check:

    Off-diagonal = correlation between two arrays [OK]
Hint: Off-diagonal in np.corrcoef matrix shows correlation between inputs [OK]
Common Mistakes:
  • Passing combined arrays instead of separate ones
  • Misunderstanding off-diagonal meaning
  • Using single array input expecting two-variable correlation
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