Skip to content

Instantly share code, notes, and snippets.

@jailop
Last active March 16, 2025 13:20
Show Gist options
  • Select an option

  • Save jailop/cb458a22800c2bce1e55289e9a2836fa to your computer and use it in GitHub Desktop.

Select an option

Save jailop/cb458a22800c2bce1e55289e9a2836fa to your computer and use it in GitHub Desktop.
Cronbach's Alpha Calculation
import numpy as np
# Rows are respondents
# Columns are survey items
data = np.array([
[4.0, 4.0, 4.0, 5.0],
[3.0, 4.0, 3.0, 4.0],
[5.0, 4.0, 5.0, 5.0],
[2.0, 3.0, 2.0, 3.0],
[4.0, 4.0, 4.0, 4.0],
])
# ddof means degrees of freedom
var_items = np.var(data, axis=0, ddof=1)
total_scores = np.sum(data, axis=1)
var_total = np.var(total_scores, ddof=1)
n = data.shape[0]
correction_factor = n / (n - 1)
alpha = correction_factor * (1 - np.sum(var_items) / var_total)
print(f"Cronbach's Alpha: {alpha:0.4f}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment