This quiz is designed to test and enhance your knowledge of correlation and plotting techniques using Pandas.
Question 1
What does the .corr()
method in Pandas compute by default?
Covariance
Pearson correlation coefficient
Spearman rank correlation
Kendall Tau correlation
Question 2
Visualize the correlation matrix for the following DataFrame and interpret the strongest correlation.
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
# Generate data
np.random.seed(42)
df = pd.DataFrame({
'A': np.random.rand(100),
'B': np.random.rand(100),
'C': np.random.rand(100) * 2
})
# Compute and plot correlation
corr_matrix = df.corr()
sns.heatmap(corr_matrix, annot=True, cmap='coolwarm')
plt.title('Correlation Heatmap')
plt.show()
Column B and C
All are equally correlated
Column A and B
Column A and C
Question 3
Which type of plot is ideal for analyzing the correlation between two variables?
import pandas as pd
df = pd.DataFrame({
'A': np.random.rand(50),
'B': np.random.rand(50)
})
# Plot
plt.scatter(df['A'], df['B'], alpha=0.6)
plt.xlabel('A')
plt.ylabel('B')
plt.title('Scatter Plot of A vs. B')
plt.show()
Histogram
Boxplot
Scatter plot
Heatmap
Question 4
Given the following DataFrame, compute and display the pairwise scatter matrix.
import pandas as pd
df = pd.DataFrame({
'A': [1, 2, 3, 4, 5],
'B': [10, 9, 8, 7, 6],
'C': [2, 4, 6, 8, 10]
})
# Pairwise scatter matrix
pd.plotting.scatter_matrix(df, diagonal='hist', figsize=(8, 6))
plt.suptitle("Scatter Matrix")
plt.show()
Strong positive correlation between A and C
Strong negative correlation between A and B
No correlation between B and C
Both A and B
Question 5
What does a heatmap with high correlation values look like?
# Generate correlated data
df = pd.DataFrame({
'X': range(1, 101),
'Y': [x * 2 for x in range(1, 101)],
'Z': [x * 3 for x in range(1, 101)]
})
# Heatmap
sns.heatmap(df.corr(), annot=True, cmap='coolwarm')
plt.title('Heatmap of Strong Correlations')
plt.show()
A heatmap with values close to 0
A heatmap with values close to 1 or -1
A mix of positive and negative correlations
None of the above
Question 6
Which library can you combine with Pandas for enhanced correlation heatmaps?
Matplotlib
Seaborn
NumPy
SciPy
Question 7
What does a correlation coefficient of -1 indicate?
No correlation
Perfect positive correlation
Perfect negative correlation
No linear relationship
Question 8
Consider the following DataFrame:
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3], 'B': [3, 6, 9]})
What is the correlation coefficient between A
and B
?
0
1
-1
0.5
Question 9
What type of plot does the following code generate?
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3, 4], 'B': [10, 15, 25, 30]})
pd.plotting.scatter_matrix(df)
Pair plot
Scatter plot
Line plot
Histogram
Question 10
What does the parameter annot=True do in the Seaborn heatmap function?
Displays the correlation values in the heatmap cells
Colors the heatmap
Normalizes the correlation values
Removes axis labels
There are 10 questions to complete.