Skip to content

Instantly share code, notes, and snippets.

@GeorgeSeif
Last active November 14, 2018 23:48
Show Gist options
  • Select an option

  • Save GeorgeSeif/48e7d74fb5fd57b96c068fbfffdd616a to your computer and use it in GitHub Desktop.

Select an option

Save GeorgeSeif/48e7d74fb5fd57b96c068fbfffdd616a to your computer and use it in GitHub Desktop.
# Only keep a certain number of eigen vectors based on
# the "explained variance percentage" which tells us how
# much information (variance) can be attributed to each
# of the principal components
exp_var_percentage = 0.97 # Threshold of 97% explained variance
tot = sum(eig_vals)
var_exp = [(i / tot)*100 for i in sorted(eig_vals, reverse=True)]
cum_var_exp = np.cumsum(var_exp)
num_vec_to_keep = 0
for index, percentage in enumerate(cum_var_exp):
if percentage > exp_var_percentage:
num_vec_to_keep = index + 1
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment