0
0
Data Analysis Pythondata~10 mins

Correlation analysis (Pearson, Spearman) in Data Analysis Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to calculate the Pearson correlation coefficient between two lists.

Data Analysis Python
import scipy.stats as stats
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
pearson_corr, p_value = stats.[1](x, y)
print(pearson_corr)
Drag options to blanks, or click blank then click option'
Apearsonr
Bkendalltau
Clinregress
Dspearmanr
Attempts:
3 left
💡 Hint
Common Mistakes
Using Spearman correlation function instead of Pearson.
Confusing the function name with linear regression.
2fill in blank
medium

Complete the code to calculate the Spearman correlation coefficient between two lists.

Data Analysis Python
import scipy.stats as stats
x = [10, 20, 30, 40, 50]
y = [5, 15, 25, 35, 45]
spearman_corr, p_value = stats.[1](x, y)
print(spearman_corr)
Drag options to blanks, or click blank then click option'
Apearsonr
Bspearmanr
Ckendalltau
Dlinregress
Attempts:
3 left
💡 Hint
Common Mistakes
Using Pearson correlation function instead of Spearman.
Trying to use linear regression function.
3fill in blank
hard

Fix the error in the code to compute Pearson correlation correctly.

Data Analysis Python
import scipy.stats as stats
x = [1, 3, 5, 7, 9]
y = [2, 4, 6, 8, 10]
correlation, p = stats.pearsonr([1], y)
print(correlation)
Drag options to blanks, or click blank then click option'
Apearsonr
By
Cstats
Dx
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the wrong variable as the first argument.
Passing the function name instead of data.
4fill in blank
hard

Fill both blanks to create a dictionary of Spearman correlations for each pair of columns in the DataFrame.

Data Analysis Python
import pandas as pd
import scipy.stats as stats

data = {'A': [1, 2, 3, 4], 'B': [4, 3, 2, 1], 'C': [2, 3, 4, 5]}
df = pd.DataFrame(data)

spearman_corrs = {col1 + '-' + col2: stats.[1](df[col1], df[col2])[0] for col1 in df.columns for col2 in df.columns if col1 != [2]
print(spearman_corrs)
Drag options to blanks, or click blank then click option'
Aspearmanr
Bpearsonr
Ckendalltau
Dcol2
Attempts:
3 left
💡 Hint
Common Mistakes
Using Pearson correlation instead of Spearman.
Comparing a column with itself causing redundant pairs.
5fill in blank
hard

Fill all three blanks to create a DataFrame showing Pearson correlation coefficients between columns.

Data Analysis Python
import pandas as pd
import scipy.stats as stats

data = {'X': [10, 20, 30], 'Y': [15, 25, 35], 'Z': [20, 30, 40]}
df = pd.DataFrame(data)

pearson_matrix = pd.DataFrame({
    [1]: [stats.pearsonr(df[[3]], df[[2]])[0] for col in df.columns]
    for [3] in df.columns
})
print(pearson_matrix)
Drag options to blanks, or click blank then click option'
Acol
Crow
Dcol_name
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same variable name for both loops causing confusion.
Passing wrong variables to the correlation function.