0
0
Data Analysis Pythondata~30 mins

Correlation analysis (Pearson, Spearman) in Data Analysis Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Correlation analysis (Pearson, Spearman)
📖 Scenario: You are a data analyst working with a small dataset of students' study hours and their exam scores. You want to find out how strongly these two variables are related.
🎯 Goal: Build a Python program that calculates both Pearson and Spearman correlation coefficients between study hours and exam scores.
📋 What You'll Learn
Create a dictionary with exact study hours and exam scores
Create a list of tuples from the dictionary
Calculate Pearson and Spearman correlation coefficients using scipy.stats
Print the correlation results exactly as specified
💡 Why This Matters
🌍 Real World
Correlation analysis helps understand relationships between variables, like how study time affects exam scores.
💼 Career
Data analysts and scientists use correlation to find patterns and insights in data for decision making.
Progress0 / 4 steps
1
Create the dataset dictionary
Create a dictionary called student_data with these exact entries: 'hours': [2, 3, 5, 8, 13] and 'scores': [50, 55, 65, 80, 95].
Data Analysis Python
Hint

Use curly braces to create a dictionary with keys 'hours' and 'scores'. Assign the exact lists as values.

2
Prepare data for correlation
Create a list of tuples called data_pairs by pairing each element from student_data['hours'] with the corresponding element from student_data['scores'] using the zip function.
Data Analysis Python
Hint

Use zip(student_data['hours'], student_data['scores']) and convert it to a list.

3
Calculate Pearson and Spearman correlations
Import pearsonr and spearmanr from scipy.stats. Then calculate the Pearson correlation coefficient and Spearman correlation coefficient between student_data['hours'] and student_data['scores']. Store the coefficients in variables pearson_corr and spearman_corr respectively.
Data Analysis Python
Hint

Use pearsonr(x, y) and spearmanr(x, y) to get correlation coefficients. Assign only the first returned value to variables.

4
Print the correlation results
Print the Pearson correlation coefficient with the exact text: "Pearson correlation: " followed by pearson_corr rounded to 3 decimal places. Then print the Spearman correlation coefficient with the exact text: "Spearman correlation: " followed by spearman_corr rounded to 3 decimal places.
Data Analysis Python
Hint

Use print(f"Pearson correlation: {pearson_corr:.3f}") and similarly for Spearman.