Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
plotly expression problem
#1
I use following code to generate graph, but nothing display, can anyone help? Thank you!
# Visualize all the original dimensions
import plotly.express as px
df=px.data.iris()
features=["sepal_width", "sepal_length", "petal_width", "petal_length"]

fig=px.scatter_matrix(df, dimensions=features, color="species")
fig.update_traces(diagonal_visible=False)
fig.show()

import plotly.express as px
from sklearn.decomposition import PCA

df = px.data.iris()
features = ["sepal_width", "sepal_length", "petal_width", "petal_length"]

import sklearn.decomposition as PCA
pca = PCA()
components = pca.fit_transform(df[features])
labels = {
    str(i): f"PC {i+1} ({var:.1f}%)"
    for i, var in enumerate(pca.explained_variance_ratio_ * 100)
}

fig = px.scatter_matrix(
    components,
    labels=labels,
    dimensions=range(4),
    color=df["species"]
)
fig.update_traces(diagonal_visible=False)
fig.show()


# Visualize a subset of the principal components
import pandas as pd
import plotly.express as px
from sklearn.decomposition import PCA
from sklearn.datasets import load_boston

boston = load_boston()
df = pd.DataFrame(boston.data, columns=boston.feature_names)
n_components = 4

pca = PCA(n_components=n_components)
components = pca.fit_transform(df)

total_var = pca.explained_variance_ratio_.sum() * 100

labels = {str(i): f"PC {i+1}" for i in range(n_components)}
labels['color'] = 'Median Price'

fig = px.scatter_matrix(
    components,
    color=boston.target,
    dimensions=range(n_components),
    labels=labels,
    title=f'Total Explained Variance: {total_var:.2f}%',
)
fig.update_traces(diagonal_visible=False)
fig.show()
Yoriz write May-15-2021, 10:32 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
I use following code to generate graph, but nothing display, can anyone help? Thank you!
# Visualize all the original dimensions
import plotly.express as px
df=px.data.iris()
features=["sepal_width", "sepal_length", "petal_width", "petal_length"]

fig=px.scatter_matrix(df, dimensions=features, color="species")
fig.update_traces(diagonal_visible=False)
fig.show()

import plotly.express as px
from sklearn.decomposition import PCA

df = px.data.iris()
features = ["sepal_width", "sepal_length", "petal_width", "petal_length"]

import sklearn.decomposition as PCA
pca = PCA()
components = pca.fit_transform(df[features])
labels = {
    str(i): f"PC {i+1} ({var:.1f}%)"
    for i, var in enumerate(pca.explained_variance_ratio_ * 100)
}

fig = px.scatter_matrix(
    components,
    labels=labels,
    dimensions=range(4),
    color=df["species"]
)
fig.update_traces(diagonal_visible=False)
fig.show()


# Visualize a subset of the principal components
import pandas as pd
import plotly.express as px
from sklearn.decomposition import PCA
from sklearn.datasets import load_boston

boston = load_boston()
df = pd.DataFrame(boston.data, columns=boston.feature_names)
n_components = 4

pca = PCA(n_components=n_components)
components = pca.fit_transform(df)

total_var = pca.explained_variance_ratio_.sum() * 100

labels = {str(i): f"PC {i+1}" for i in range(n_components)}
labels['color'] = 'Median Price'

fig = px.scatter_matrix(
    components,
    color=boston.target,
    dimensions=range(n_components),
    labels=labels,
    title=f'Total Explained Variance: {total_var:.2f}%',
)
fig.update_traces(diagonal_visible=False)
fig.show()
I have added all code, similar structure code was applied to visualize data, no error message came up, but graph didn't show up.
Larz60+ write May-16-2021, 12:22 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button
Fixed for you this time. Pleae use bbcode tags on future posts.
Reply
#3
The code I posted is almost complete (I stopped at the beginning since no outcome obtained), it is from here: https://plotly.com/python/pca-visualization/

When I tried the first run, error message reminded "no plotly module", after installed, no error message, but the graph still didn't show up.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  binning_endpoints ->plotly Luis_liverpool 0 833 Aug-09-2022, 10:13 AM
Last Post: Luis_liverpool
  Problem in Regex Expression shantanu97 2 1,706 Sep-28-2021, 03:40 AM
Last Post: shantanu97
  Pass results of expression to another expression cmdr_eggplant 2 2,279 Mar-26-2020, 06:59 AM
Last Post: ndc85430
  CGI in python, problem with pandas, plotly.express and export html HK2432 0 2,123 Jan-19-2020, 01:30 PM
Last Post: HK2432
  Plotly library with AIX 6.X / 7.X ? HK2432 0 1,453 Jan-14-2020, 06:04 PM
Last Post: HK2432
  Plotly error - AttributeError: module 'plotly.plotly' has no attribute 'iplot' fernando_santos 0 5,591 Jul-24-2019, 02:35 PM
Last Post: fernando_santos

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020