{"spec_id":"bar-permutation-importance","library":"muix","language":"javascript","code":"// anyplot.ai\n// bar-permutation-importance: Permutation Feature Importance Plot\n// Library: muix 9.16.0 | JavaScript 22.23.3\n// Quality: 87/100 | Updated: 2026-10-24\nimport { ChartsContainer } from \"@mui/x-charts/ChartsContainer\";\nimport { BarPlot } from \"@mui/x-charts/BarChart\";\nimport { ChartsXAxis } from \"@mui/x-charts/ChartsXAxis\";\nimport { ChartsYAxis } from \"@mui/x-charts/ChartsYAxis\";\nimport { ChartsGrid } from \"@mui/x-charts/ChartsGrid\";\nimport { ChartsReferenceLine } from \"@mui/x-charts/ChartsReferenceLine\";\nimport { useXScale, useYScale } from \"@mui/x-charts/hooks\";\nimport Box from \"@mui/material/Box\";\nimport Typography from \"@mui/material/Typography\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst ZERO_REF = 0;\nconst X_PAD_SHARE = 0.08;\nconst SEQ_MIN_MIX = 0.22;\n\n// Columns: set these to your data's column names\nconst FEATURE = \"Model feature\"; // role: feature\nconst IMPORTANCE_MEAN = \"Mean decrease in accuracy\"; // role: importance_mean\nconst IMPORTANCE_STD = \"Shuffle SD\"; // role: importance_std\n\n// Data: replace this block with your own records, one object per row\nconst data = [\n  { [FEATURE]: \"Contract Type\", [IMPORTANCE_MEAN]: 0.182, [IMPORTANCE_STD]: 0.021 },\n  { [FEATURE]: \"Tenure (Months)\", [IMPORTANCE_MEAN]: 0.146, [IMPORTANCE_STD]: 0.018 },\n  { [FEATURE]: \"Monthly Charges\", [IMPORTANCE_MEAN]: 0.098, [IMPORTANCE_STD]: 0.015 },\n  { [FEATURE]: \"Total Charges\", [IMPORTANCE_MEAN]: 0.071, [IMPORTANCE_STD]: 0.014 },\n  { [FEATURE]: \"Tech Support\", [IMPORTANCE_MEAN]: 0.058, [IMPORTANCE_STD]: 0.012 },\n  { [FEATURE]: \"Online Security\", [IMPORTANCE_MEAN]: 0.049, [IMPORTANCE_STD]: 0.011 },\n  { [FEATURE]: \"Support Calls\", [IMPORTANCE_MEAN]: 0.041, [IMPORTANCE_STD]: 0.010 },\n  { [FEATURE]: \"Internet Service\", [IMPORTANCE_MEAN]: 0.033, [IMPORTANCE_STD]: 0.009 },\n  { [FEATURE]: \"Payment Method\", [IMPORTANCE_MEAN]: 0.026, [IMPORTANCE_STD]: 0.008 },\n  { [FEATURE]: \"Paperless Billing\", [IMPORTANCE_MEAN]: 0.019, [IMPORTANCE_STD]: 0.007 },\n  { [FEATURE]: \"Dependents\", [IMPORTANCE_MEAN]: 0.014, [IMPORTANCE_STD]: 0.006 },\n  { [FEATURE]: \"Streaming TV\", [IMPORTANCE_MEAN]: 0.009, [IMPORTANCE_STD]: 0.006 },\n  { [FEATURE]: \"Partner\", [IMPORTANCE_MEAN]: 0.006, [IMPORTANCE_STD]: 0.005 },\n  { [FEATURE]: \"Multiple Lines\", [IMPORTANCE_MEAN]: 0.003, [IMPORTANCE_STD]: 0.005 },\n  { [FEATURE]: \"Senior Citizen\", [IMPORTANCE_MEAN]: -0.004, [IMPORTANCE_STD]: 0.004 },\n];\n\n// Plot: reads only data and the constants above\nconst sorted = [...data].sort((a, b) => b[IMPORTANCE_MEAN] - a[IMPORTANCE_MEAN]);\nconst features = sorted.map((d) => d[FEATURE]);\nconst means = sorted.map((d) => d[IMPORTANCE_MEAN]);\nconst stds = sorted.map((d) => d[IMPORTANCE_STD]);\n\nconst minMean = Math.min(...means);\nconst maxMean = Math.max(...means);\nconst meanSpan = maxMean - minMean || 1;\n// Start the gradient below the weakest mean so the lowest bar keeps SEQ_MIN_MIX of the ramp\nconst colorMin = minMean - (meanSpan * SEQ_MIN_MIX) / (1 - SEQ_MIN_MIX);\n\nconst rawMin = Math.min(\n  ZERO_REF,\n  ...sorted.map((d) => d[IMPORTANCE_MEAN] - d[IMPORTANCE_STD]),\n);\nconst rawMax = Math.max(\n  ZERO_REF,\n  ...sorted.map((d) => d[IMPORTANCE_MEAN] + d[IMPORTANCE_STD]),\n);\nconst xSpan = rawMax - rawMin || 1;\nconst xMin = rawMin - xSpan * X_PAD_SHARE;\nconst xMax = rawMax + xSpan * X_PAD_SHARE;\n\nconst TITLE = \"Churn Prediction Model · bar-permutation-importance · javascript · muix · anyplot.ai\";\nconst SUBTITLE = `${FEATURE}: ${features.length} · Whiskers: ±1 ${IMPORTANCE_STD} · Top: ${features[0]}, ${means[0].toFixed(3)} ± ${stds[0].toFixed(3)}`;\n\nfunction ErrorBars() {\n  const xScale = useXScale(\"x\");\n  const yScale = useYScale(\"y\");\n  const bw = yScale.bandwidth();\n  const cap = bw * 0.34;\n\n  return (\n    <g>\n      {features.map((feature, i) => {\n        const cy = yScale(feature) + bw / 2;\n        const xLo = xScale(means[i] - stds[i]);\n        const xHi = xScale(means[i] + stds[i]);\n        return (\n          <g\n            key={feature}\n            stroke={t.ink}\n            strokeWidth={2.5}\n            strokeOpacity={0.85}\n            strokeLinecap=\"round\"\n          >\n            <line x1={xLo} y1={cy} x2={xHi} y2={cy} />\n            <line x1={xLo} y1={cy - cap} x2={xLo} y2={cy + cap} />\n            <line x1={xHi} y1={cy - cap} x2={xHi} y2={cy + cap} />\n          </g>\n        );\n      })}\n    </g>\n  );\n}\n\nexport default function Chart() {\n  const W = window.ANYPLOT_SIZE.width;\n  const H = window.ANYPLOT_SIZE.height;\n  const CHART_TOP = 84;\n\n  return (\n    <Box sx={{ position: \"relative\", width: W, height: H, bgcolor: t.pageBg }}>\n      <Box sx={{ position: \"absolute\", top: 20, left: 56, right: 56 }}>\n        <Typography sx={{ color: t.ink, fontSize: 22, fontWeight: 600, lineHeight: 1.25 }}>\n          {TITLE}\n        </Typography>\n        <Typography sx={{ color: t.inkSoft, fontSize: 15, mt: 0.5 }}>\n          {SUBTITLE}\n        </Typography>\n      </Box>\n\n      <Box sx={{ position: \"absolute\", top: CHART_TOP, left: 0, right: 0, bottom: 0 }}>\n        <ChartsContainer\n          width={W}\n          height={H - CHART_TOP}\n          series={[\n            {\n              type: \"bar\",\n              id: \"importance\",\n              data: means,\n              layout: \"horizontal\",\n              xAxisId: \"x\",\n              yAxisId: \"y\",\n              label: IMPORTANCE_MEAN,\n              valueFormatter: (v: number, { dataIndex }: { dataIndex: number }) =>\n                `${v.toFixed(3)} ± ${stds[dataIndex].toFixed(3)}`,\n            },\n          ]}\n          xAxis={[\n            {\n              id: \"x\",\n              scaleType: \"linear\",\n              min: xMin,\n              max: xMax,\n              tickNumber: 8,\n              colorMap: { type: \"continuous\", min: colorMin, max: maxMean, color: [t.seq[0], t.seq[1]] },\n              height: 64,\n              label: IMPORTANCE_MEAN,\n              labelStyle: { fontSize: 16, fill: t.ink, fontWeight: 500 },\n              tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n            },\n          ]}\n          yAxis={[\n            {\n              id: \"y\",\n              scaleType: \"band\",\n              data: features,\n              width: 175,\n              categoryGapRatio: 0.3,\n              label: FEATURE,\n              labelStyle: { fontSize: 16, fill: t.ink, fontWeight: 500 },\n              tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n              disableTicks: true,\n            },\n          ]}\n          margin={{ top: 14, right: 48, bottom: 20, left: 36 }}\n          skipAnimation\n          sx={{\n            \"& .MuiChartsAxis-line\": { stroke: t.inkSoft },\n            \"& .MuiChartsGrid-line\": { stroke: t.grid },\n            \"& .MuiBarChart-element\": { stroke: t.inkSoft, strokeOpacity: 0.25, strokeWidth: 1 },\n          }}\n        >\n          <ChartsGrid vertical />\n          <BarPlot skipAnimation borderRadius={3} />\n          <ErrorBars />\n          <ChartsReferenceLine\n            x={ZERO_REF}\n            axisId=\"x\"\n            lineStyle={{ stroke: t.ink, strokeWidth: 2, strokeOpacity: 0.6 }}\n          />\n          <ChartsXAxis axisId=\"x\" />\n          <ChartsYAxis axisId=\"y\" />\n        </ChartsContainer>\n      </Box>\n    </Box>\n  );\n}\n"}