Skip to content

Instantly share code, notes, and snippets.

@erichare
Created July 7, 2022 20:26
Show Gist options
  • Save erichare/d468b8b72d70e008b4a702e9776b25c0 to your computer and use it in GitHub Desktop.
Save erichare/d468b8b72d70e008b4a702e9776b25c0 to your computer and use it in GitHub Desktop.
Fit a PCA using scikit-learn
def fit_pca(pca_data, x_component=1, y_component=2, split_by=None):
...
pca_df = df[vars]
pca = PCA(n_components=n_components)
pca.fit(pca_df)
...
pca_data = pd.DataFrame(pca.fit_transform(pca_df))
return pca_data
def plot_pca(pca_data, x_component=1, y_component=2, split_by=None):
fig = figure(plot_width=1000, plot_height=1000)
for pop in pca_data["split"].unique().tolist():
...
return fig
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment