Skip to content

Commit

Permalink
Merge pull request #3964 from StanislawSwierc/patch-1
Browse files Browse the repository at this point in the history
Draw PCA loadings as arrows
  • Loading branch information
nicolaskruchten authored Jan 23, 2023
2 parents edbcd34 + fc248d2 commit edb8de2
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions doc/python/ml-pca.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,16 @@ loadings = pca.components_.T * np.sqrt(pca.explained_variance_)
fig = px.scatter(components, x=0, y=1, color=df['species'])

for i, feature in enumerate(features):
fig.add_shape(
type='line',
x0=0, y0=0,
x1=loadings[i, 0],
y1=loadings[i, 1]
fig.add_annotation(
ax=0, ay=0,
axref="x", ayref="y",
x=loadings[i, 0],
y=loadings[i, 1],
showarrow=True,
arrowsize=2,
arrowhead=2,
xanchor="right",
yanchor="top"
)
fig.add_annotation(
x=loadings[i, 0],
Expand All @@ -263,6 +268,7 @@ for i, feature in enumerate(features):
xanchor="center",
yanchor="bottom",
text=feature,
yshift=5,
)
fig.show()
```
Expand Down

0 comments on commit edb8de2

Please sign in to comment.