0Pricing
Pandas & NumPy Academy · 강의

이변량 및 상관 분석

산점도, 상자 그림, 상관 히트맵을 사용해 열 쌍 사이의 관계를 탐색합니다.

이변량 및 상관 분석은(는) CoddyKit의 무료 Pandas & NumPy Academy 강의입니다. 이것은 4개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Pandas & NumPy Academy 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Pandas & NumPy Academy 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

From One Variable to Two

Bivariate analysis examines the relationship between exactly two columns at a time. After profiling each column individually (univariate analysis), you ask: how do these two variables relate? The analysis method depends on the combination of variable types: numeric vs. numeric (scatter plot + correlation), categorical vs. numeric (box/violin plot), or categorical vs. categorical (crosstab + chi-squared). Each combination requires a different technique.

import pandas as pd
import seaborn as sns

df = sns.load_dataset('titanic')

# Relationship types present in the dataset
print('Numeric columns:', df.select_dtypes('number').columns.tolist())
print('Categorical columns:', df.select_dtypes('object').columns.tolist())
print('Boolean columns:', df.select_dtypes('bool').columns.tolist())

Numeric vs. Numeric: Scatter Plot

For two numeric variables, a scatter plot is the primary bivariate tool. It shows each observation as a point at (x, y) coordinates, revealing the direction, strength, and form of the relationship. Always look at the plot before computing a correlation coefficient — a correlation of 0 can still show a strong non-linear (curved) relationship that the coefficient misses.

import seaborn as sns
import matplotlib.pyplot as plt

df = sns.load_dataset('titanic')

sns.scatterplot(data=df, x='age', y='fare', alpha=0.4)
plt.title('Age vs. Fare — Is There a Linear Relationship?')
plt.xlabel('Age')
plt.ylabel('Fare ($)')
plt.show()

Pearson Correlation Coefficient

The Pearson r quantifies the strength and direction of the linear relationship between two numeric variables. Compute it with df[['col1', 'col2']].corr() or scipy.stats.pearsonr(x, y) which also returns a p-value for significance testing. Always pair r with a scatter plot — high r can be driven by a few outliers, and the same r can describe very different scatter shapes (Anscombe's Quartet famously illustrates this).

import pandas as pd
import seaborn as sns
from scipy import stats

df = sns.load_dataset('titanic').dropna(subset=['age', 'fare'])

# Pearson correlation
r, p = stats.pearsonr(df['age'], df['fare'])
print(f'Pearson r = {r:.3f}, p-value = {p:.4f}')

# Spearman for robustness check
rho, p_sp = stats.spearmanr(df['age'], df['fare'])
print(f'Spearman rho = {rho:.3f}, p-value = {p_sp:.4f}')

Categorical vs. Numeric: Box and Violin Plots

When one variable is categorical and the other is numeric, the question is: does the numeric distribution differ across categories? Use a box plot or violin plot to compare. For example, does survival status affect the fare paid? Does the passenger class affect age? These plots immediately reveal whether category membership is associated with higher or lower values of the numeric variable.

import seaborn as sns
import matplotlib.pyplot as plt

df = sns.load_dataset('titanic')

fig, axes = plt.subplots(1, 2, figsize=(12, 5))

sns.boxplot(data=df, x='class', y='fare',
            order=['First', 'Second', 'Third'], ax=axes[0])
axes[0].set_title('Fare by Passenger Class')

sns.violinplot(data=df, x='survived', y='age',
               inner='quartile', ax=axes[1])
axes[1].set_title('Age Distribution by Survival')

plt.tight_layout()
plt.show()

GroupBy Statistics for Categorical vs. Numeric

Complement the visual comparison with numeric group statistics using groupby. Compute the mean, median, and count for each category to quantify the differences seen in plots. Look for categories where the mean and median diverge (indicating outliers within a group), and check whether group sizes are balanced — very small groups (<5 observations) make comparisons unreliable.

import pandas as pd
import seaborn as sns

df = sns.load_dataset('titanic')

stats = df.groupby('class')['fare'].agg(['mean', 'median', 'std', 'count'])
stats.columns = ['Mean Fare', 'Median Fare', 'Std Fare', 'Count']
print(stats.round(2))

print('\nSurvival rate by class:')
print(df.groupby('class')['survived'].mean().round(3))

Categorical vs. Categorical: Crosstabs

For two categorical variables, use pd.crosstab(df['var1'], df['var2']) to create a frequency table showing how many observations fall in each combination of categories. Normalise by row or column with normalize='index' or normalize='columns' to get proportions instead of counts. This is the foundation for studying whether two categorical variables are independent or associated.

import pandas as pd
import seaborn as sns

df = sns.load_dataset('titanic')

# Frequency table
counts = pd.crosstab(df['class'], df['survived'])
counts.columns = ['Died', 'Survived']
print('Counts:')
print(counts)

# Row-normalised proportions (survival rate per class)
props = pd.crosstab(df['class'], df['survived'], normalize='index')
props.columns = ['Died', 'Survived']
print('\nSurvival Rate per Class:')
print(props.round(3))

Visualising Crosstabs as Heatmaps

Turn a crosstab into a heatmap for instant visual comparison of cell frequencies. This is more scannable than a printed table when there are many category combinations. Annotate each cell with its value using annot=True and choose a sequential colour map (like 'YlOrRd') since all values are non-negative. Heatmaps of row-normalised proportions effectively show the conditional distribution of one category given another.

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

df = sns.load_dataset('titanic')
props = pd.crosstab(df['class'], df['sex'], normalize='index')

sns.heatmap(props, annot=True, fmt='.2f', cmap='YlOrRd')
plt.title('Gender Mix by Passenger Class (Row %)')
plt.xlabel('Sex')
plt.ylabel('Class')
plt.show()

Chi-Squared Test for Independence

The chi-squared test of independence checks whether two categorical variables are statistically independent. scipy.stats.chi2_contingency(crosstab) returns the chi-squared statistic, p-value, degrees of freedom, and expected frequencies. A small p-value (typically < 0.05) means there is a statistically significant association between the two variables — the distribution of one varies systematically with the other.

import pandas as pd
import seaborn as sns
from scipy.stats import chi2_contingency

df = sns.load_dataset('titanic')

# Test if passenger class and survival are independent
crosstab = pd.crosstab(df['class'], df['survived'])
chi2, p, dof, expected = chi2_contingency(crosstab)

print(f'Chi-squared: {chi2:.2f}')
print(f'P-value: {p:.6f}')
print(f'Degrees of freedom: {dof}')
print(f'\nConclusion: {"Significant association" if p < 0.05 else "No significant association"}')

Full Correlation Matrix for Numeric Columns

Rather than examining pairs one at a time, compute the full correlation matrix for all numeric columns and visualise it as a heatmap. This reveals which pairs of variables are strongly correlated in one glance. High correlations between independent features (multicollinearity) can cause problems in regression models — knowing about them early allows you to remove or combine redundant features before modelling.

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np

df = sns.load_dataset('titanic')
corr = df.select_dtypes('number').corr()
mask = np.triu(np.ones_like(corr, dtype=bool))

sns.heatmap(corr, mask=mask, annot=True, fmt='.2f',
            cmap='coolwarm', vmin=-1, vmax=1,
            linewidths=0.5, square=True)
plt.title('Correlation Matrix — Titanic Numeric Columns')
plt.tight_layout()
plt.show()

Interactions Across Subgroups

Sometimes a relationship between two variables differs dramatically across subgroups — a phenomenon known as Simpson's Paradox. For example, the correlation between age and fare might be positive for first-class passengers but negative for third-class. Always segment your bivariate analysis by key categorical variables (using hue in Seaborn or separate groupby aggregations) to check whether the relationship is consistent or reverses across subgroups.

import seaborn as sns
import matplotlib.pyplot as plt

df = sns.load_dataset('titanic')

# Scatter coloured by passenger class
sns.scatterplot(
    data=df.dropna(subset=['age', 'fare']),
    x='age',
    y='fare',
    hue='class',
    alpha=0.5,
    palette='deep'
)
plt.title('Age vs Fare — Does Class Change the Relationship?')
plt.legend(title='Class')
plt.yscale('log')  # log scale due to fare skewness
plt.show()

Documenting Bivariate Findings

After completing bivariate analysis, document key findings: which pairs of features are correlated (and how strongly), whether categorical variables show meaningful group differences in numeric outcomes, and any subgroup interactions or paradoxes found. These insights should drive feature selection decisions — highly correlated feature pairs may need one dropped, and strong categorical predictors of the target variable should be flagged as high-priority inputs for modelling.

Quick Check

Test your understanding of bivariate and correlation analysis from this lesson.

Lesson Recap

In this lesson you learned: scatter plots and Pearson r analyse numeric-numeric relationships, box/violin plots and groupby compare numeric variables across categories, and crosstabs with chi-squared tests measure association between categorical variables. Next up we cover how to compile EDA findings into a structured summary report.

자주 묻는 질문

“이변량 및 상관 분석” 강의는 무료인가요?

네 — “이변량 및 상관 분석” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Pandas & NumPy Academy 강의 전체를 잠금 해제할 수 있습니다. Pandas & NumPy Academy 강의에는 총 4개의 강의가 포함되어 있습니다.

“이변량 및 상관 분석”에서 뭘 배우나요?

산점도, 상자 그림, 상관 히트맵을 사용해 열 쌍 사이의 관계를 탐색합니다. 브라우저에서 직접 실행하는 실습 코드로 Pandas & NumPy Academy을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Pandas & NumPy Academy을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Pandas & NumPy Academy은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 3번째 강의입니다.

“이변량 및 상관 분석” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Pandas & NumPy Academy 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Pandas & NumPy Academy 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. 데이터 세트 프로파일링 점검표
  2. 단변량 분석
  3. 이변량 및 상관 분석
  4. 보고서에서 결과 요약하기
← Pandas & NumPy Academy(으)로 돌아가기