File size: 5,939 Bytes
e1c192a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | import dash
from dash import html, dcc, Input, Output
import dash_bootstrap_components as dbc
import plotly.express as px
import pandas as pd
from sklearn.datasets import load_breast_cancer
dash.register_page(__name__, path="/data-analysis", name="Data Analysis")
# --------------------------------------------
# Load dataset
# --------------------------------------------
data = load_breast_cancer()
df = pd.DataFrame(data.data, columns=data.feature_names)
df["target"] = data.target
df["target_label"] = df["target"].map({0: "Malignant", 1: "Benign"})
# Important features
important_features = [
"texture error", "area error", "smoothness error",
"concavity error", "symmetry error",
"fractal dimension error", "worst concavity"
]
top4_features = important_features[:4] # For pairplots
# --------------------------------------------
# Layout
# --------------------------------------------
layout = html.Div(
style={"padding": "30px"},
children=[
html.H2("Data Analysis", className="page-title"),
# Filters row
dbc.Row(
[
dbc.Col(
dcc.Dropdown(
id="diagnosis-filter",
options=[
{"label": "All", "value": "all"},
{"label": "Benign", "value": 1},
{"label": "Malignant", "value": 0},
],
value="all",
clearable=False,
placeholder="Filter by Diagnosis",
style={
"background": "linear-gradient(135deg, #ff77b4, #e61227)",
"color": "#fff",
"border-radius": "8px",
"padding": "5px"
}
),
width=3
),
dbc.Col(
dcc.Dropdown(
id="feature-select",
options=[{"label": f, "value": f} for f in important_features],
value=important_features[0],
clearable=False,
placeholder="Select Feature",
style={
"background": "linear-gradient(135deg, #ff77b4, #e61227)",
"color": "#fff",
"border-radius": "8px",
"padding": "5px"
}
),
width=3
)
],
className="mb-4",
align="center",
justify="start",
style={"gap": "20px"}
),
# Heatmap Card
dbc.Card(
dbc.CardBody(dcc.Graph(id="heatmap-plot")),
className="custom-card shadow mb-4"
),
# Pairplot Cards for top 4 features (2x2 grid)
dbc.Row(
[
dbc.Col(dcc.Graph(id=f"pairplot-{i}"), md=6)
for i in range(6) # 6 combinations for 4 features
],
className="mb-4",
),
# Box Plot Card
dbc.Card(
dbc.CardBody(dcc.Graph(id="box-plot")),
className="custom-card shadow mb-4"
),
# Violin Plot Card
dbc.Card(
dbc.CardBody(dcc.Graph(id="violin-plot")),
className="custom-card shadow mb-4"
),
# Histogram Card
dbc.Card(
dbc.CardBody(dcc.Graph(id="histogram-plot")),
className="custom-card shadow mb-4"
),
]
)
# --------------------------------------------
# Callbacks
# --------------------------------------------
from dash import callback
@callback(
Output("heatmap-plot", "figure"),
[Output(f"pairplot-{i}", "figure") for i in range(6)],
Output("box-plot", "figure"),
Output("violin-plot", "figure"),
Output("histogram-plot", "figure"),
Input("diagnosis-filter", "value"),
Input("feature-select", "value")
)
def update_plots(filter_value, selected_feature):
# Filter data
filtered = df.copy()
if filter_value != "all":
filtered = filtered[filtered["target"] == filter_value]
# Heatmap
corr = filtered[important_features].corr()
fig_heatmap = px.imshow(corr, color_continuous_scale="RdPu")
# Pairplots (all pairs of top 4 features)
from itertools import combinations
pair_figs = []
for f1, f2 in combinations(top4_features, 2):
fig = px.scatter(
filtered,
x=f1,
y=f2,
color="target_label",
color_discrete_map={"Benign": "#ff77b4", "Malignant": "#9c005d"},
)
fig.update_layout(
xaxis_tickangle=45,
yaxis_tickangle=0,
margin=dict(l=40, r=20, t=20, b=40),
)
pair_figs.append(fig)
# Box plot
fig_box = px.box(
filtered,
y=selected_feature,
color="target_label",
color_discrete_map={"Benign": "#ff77b4", "Malignant": "#9c005d"},
points="all"
)
# Violin plot
fig_violin = px.violin(
filtered,
y=selected_feature,
color="target_label",
color_discrete_map={"Benign": "#ff77b4", "Malignant": "#9c005d"},
box=True,
points="all"
)
# Histogram plot
fig_hist = px.histogram(
filtered,
x=selected_feature,
color="target_label",
barmode="overlay",
opacity=0.7,
color_discrete_map={"Benign": "#ff77b4", "Malignant": "#9c005d"}
)
return fig_heatmap, *pair_figs, fig_box, fig_violin, fig_hist
|