<jupyter_start><jupyter_text>Function to add correlation circle<jupyter_code>from sklearn.preprocessing import normalize

def add_correlation_circle(figure, coeffs, texts, normalization=True, add_circle=True):
    if add_circle:
        figure.add_shape(type="circle",
                         xref="x", yref="y",
                         x0=-1, y0=-1, x1=1, y1=1,
                         line_color="blue"
                        )
    if normalization:
        coeffs = normalize(coeffs, axis=0)
    for i in range(coeffs.shape[1]):
        figure.add_annotation(
            x=coeffs[0,i],  # arrows' head
            y=coeffs[1,i],  # arrows' head
            ax=0,  # arrows' tail
            ay=0,  # arrows' tail
            xref='x',
            yref='y',
            axref='x',
            ayref='y',
            text='',  # if you want only the arrow
            showarrow=True,
            arrowhead=3,
            arrowsize=1,
            arrowwidth=3,
            arrowcolor='red'
        )

        figure.add_annotation(
            x=coeffs[0,i]*1.25, 
            y=coeffs[1,i]*1.25,
            text=texts[i],
            showarrow=False,
            font=dict(size=20,color="red")
        )
    return figure<jupyter_output><empty_output><jupyter_text>Load data<jupyter_code>iris = datasets.load_iris()
X = iris.data
y = iris.target<jupyter_output><empty_output><jupyter_text>Perfrom PCA<jupyter_code>scaler = StandardScaler()
scaler.fit(X)
X=scaler.transform(X)
pca = PCA(n_components=2)
pca.fit(X)
x_new = pd.DataFrame(pca.transform(X))
x_new['species'] = y
x_new['species'] = x_new['species'].astype('object')<jupyter_output><empty_output><jupyter_text>Visualization<jupyter_code>fig = px.scatter(x_new, x=0, y=1, color='species',width=800, height=600)
add_correlation_circle(fig, 
                       coeffs=pca.components_, 
                       texts=[f"Var {i}" for i in range(1,5)], 
                       normalization=True,
                       add_circle=True)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup