Cronbach’s Alpha: Theory and Application in Python | by Max Hilsdorf | Towards Data Science

Cronbach’s Alpha is the dominant measure of scale reliability in psychology and the social sciences. Its importance can’t be overstated and you’ll find it in almost every quantitative empirical study you’ll read. Surprisingly, there hasn’t been a single article published on Medium covering its application in Python. On top, none of the common data science libraries like NumPy, Pandas, or Sklearn feature Cronbach alpha measures. This article will show you exactly how to do it.
First, I’ll explain some of the theory and math behind Cronbach's alpha. Then, we’ll quickly get to hands-on coding.
Whenever we use multiple items to measure a latent (non-observable) construct, we call this set of items a scale. For example, in a business context, we might want to find out our customers’ attitude towards a product P. This would be really helpful to know for marketers. However, contrary to constructs like weight (f.e. 50kg), revenue (f.e. 1 mio. $) or attendance (True/False), we can’t measure attitude directly.
Simply asking them “What is your attitude towards product P?” seems like a good solution at first. However, since it’s up to your customers to define what attitude actually is, you won’t get reliable results this way. Instead, you should define the construct yourself and attempt to measure it using multiple, objective and observable measures. If a customer rates a product as “good”, “interesting”, “exciting”, and “useful”, that might be a measure of attitude, even tho the word itself wasn’t even used. Arguably, you can use these 4 question as a scale that measures attitude.
To make sure we did a good job at constructing the scale, we need to use some quality measures. In general, we want to test a scale for validity and reliability. A scale is valid if it actually measures the construct it’s supposed to measure. It’s reliable if it measures the same thing every time.
Cronbach’s Alpha is a measure of reliability. To be exact, it tells us how internally consistent our scale is. This is the degree to which all items in a scale measure the same construct. If “good”, “interesting”, “exciting”, and “useful” all contribute to measuring the same thing, Cronbach’s Alpha will be high. If, however, we tried to measure attitude using the adjectives “tasty”, “rich”, “depressing”, and “cold”, I assume Cronbach’s Alpha would be low. The items don’t really seem to line up. And this is exactly what Cronbach’s Alpha helps us with. It answers the question: Do the items I picked to measure a construct line up?
Cronbach’s Alpha ranges between 0 and 1. A higher value indicates a higher internal consistency. In general, a Cronbach’s Alpha of 0.7 or more is considered acceptable. Now, let’s get into how these numbers come about. Feel free to skip this part if you want to get into coding directly.
This is the fomula we need:
Here, N is the number of items (questions) and r is the mean correlation between the items. Let’s get back to the example to understand what this means. Assume we asked 6 customers for their opinion of product P. Then we calculated the correlation between the measures of “good”, “interesting”, “exciting”, and “useful”. Assume there are the results:
We can see that all correlations are high. This should lead to a high Cronbach’s Alpha. The image below illustrates how to apply the formula to the data.
We got a Cronbach’s Alpha of .965, which is exceptional!
We’re done with the math (kind of)! Let’s get into coding!
Let’s write our own function for calculating Cronbach’s Alpha. First, let’s import pandas and numpy.
import pandas as pdimport numpy as np
Now, what do we need to “teach” the function?
Transform the dataframe input into a correlation matrix.Calculate N and r.Use the formula to calculate Cronbach’s Alpha
Transform the dataframe input into a correlation matrix.
Calculate N and r.
Use the formula to calculate Cronbach’s Alpha
This is how you can do it:
def cronbach_alpha(df):    # 1. Transform the df into a correlation matrix    df_corr = df.corr()        # 2.1 Calculate N    # The number of variables equals the number of columns in the df    N = df.shape[1]        # 2.2 Calculate R    # For this, we'll loop through the columns and append every    # relevant correlation to an array calles "r_s". Then, we'll    # calculate the mean of "r_s"    rs = np.array([])    for i, col in enumerate(df_corr.columns):        sum_ = df_corr[col][i+1:].values        rs = np.append(sum_, rs)    mean_r = np.mean(rs)       # 3. Use the formula to calculate Cronbach's Alpha     cronbach_alpha = (N * mean_r) / (1 + (N - 1) * mean_r)    return cronbach_alpha
If we use the function with the sample data, we get this output:
The result is 0.966. With the manual calculation, we got 0.965. This is very likely due to rounding.
Look, I need to tell you something. There is another approach to calculating Cronbach’s Alpha that is much better suited for numpy calculations. However, there is a reason I haven’t told you yet. The approach you just learned is much more intuitive and easier to understand. On top, unless you are using very large datasets, which are uncommon in survey statistics where Cronbach’s Alpha is mainly applied, you won’t have any trouble with the function above. I won’t cover this other approach in this article. However, I encourage you to build on your new knowledge and do some further research yourself.
Anyways, I hope this article was helpful to you and taught you a new statistical concept and/or improved your workflow!
Thank you very much for reading!