Skip to content

Instantly share code, notes, and snippets.

@trycycle
Last active December 5, 2017 01:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trycycle/731c34d8919eca27bdc4e97fc02df500 to your computer and use it in GitHub Desktop.
Save trycycle/731c34d8919eca27bdc4e97fc02df500 to your computer and use it in GitHub Desktop.
Cronbach alpha信頼性係数
import pandas as pd
def cronbach_alpha(dataframe, columns):
''' dataframe: pandas dataframe
columns: list of the dataframe column names
'''
var_sum = 0
tmp_df = pd.DataFrame(index=dataframe.index, columns=['total'])
tmp_df = tmp_df.fillna(0)
for col in columns:
var_sum += dataframe[col].var(ddof=False)
tmp_df['total'] += dataframe[col]
total_var = tmp_df['total'].var(ddof=False)
alpha = (len(columns) / (len(columns) - 1)) * (1 - (var_sum / total_var))
return alpha
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment