← K-means Clustering
Your token

K-means Clustering API

Reading the CSV, grouping, the chart and every export run free in the browser and need no API. The API is for the one metered step: send one summary per group (its size, its column averages next to the overall averages, and any category value it has much more of), get back a short plain name for each group and the columns that set it apart.

Base URL: https://api.skillsafe.ai/v1/app-api. Every response is {"data": ...} or {"error": {"code", "message"}}; read error.code and error.message rather than the HTTP status alone.

The task field

This app has one task. Always send "task": "name_groups".

taskWhat it doesReply
name_groupsNames every group in 1 to 5 plain words and lists up to three columns where its average is clearly high or low (at least half a standard deviation from the overall average). A group with no such column is named as the typical group. Never repeats your numbers.One JSON object

Input fields

FieldTypeMeaning
taskstring"name_groups"
groupsstringJSON text: a list of {"group", "rows", "share_pct", "columns", "categories"}, groups numbered from 1, largest first. columns holds {"column", "average", "overall", "z"} per number column (at most 12, those that differ most between groups); z is the group average minus the overall average, in standard deviations. categories holds {"column", "value", "share_pct", "overall_pct"} for category values the group has much more of.
factsstringJSON text: rows_grouped, groups, columns (the names a reply may use), columns_sent, columns_total, fit_score (silhouette, −1 to 1), fit (clear, some, weak), scaling.

The reply

job.output.output is JSON text: {"task": "name_groups", "groups": [{"group", "name", "traits": [{"column", "dir"}]}]}, one entry per group. dir is high or low. Names have no digits and are all different. The page checks each trait against the averages it sent: a trait is backed when the group's z is at least 0.3 in the stated direction. If the balance ran low, truncated is true and the object may stop early; the page keeps the groups that arrived complete.

Worked example

Input (input.json), for the page's Shop customers example:

{
  "task": "name_groups",
  "groups": "[{\"group\":1,\"rows\":69,\"share_pct\":28.8,\"columns\":[{\"column\":\"age\",\"average\":24.55,\"overall\":40.1,\"z\":-1.17},{\"column\":\"annual_income_k\",\"average\":38.01,\"overall\":65.69,\"z\":-0.95},{\"column\":\"spending_score\",\"average\":77.09,\"overall\":55.52,\"z\":0.83},{\"column\":\"visits_per_month\",\"average\":8.88,\"overall\":5.38,\"z\":1.15}],\"categories\":[{\"column\":\"region\",\"value\":\"City centre\",\"share_pct\":71,\"overall_pct\":35}]},{\"group\":2,\"rows\":60,\"share_pct\":25,\"columns\":[{\"column\":\"age\",\"average\":44.95,\"overall\":40.1,\"z\":0.37},{\"column\":\"annual_income_k\",\"average\":98.03,\"overall\":65.69,\"z\":1.11},{\"column\":\"spending_score\",\"average\":23.98,\"overall\":55.52,\"z\":-1.22},{\"column\":\"visits_per_month\",\"average\":1.9,\"overall\":5.38,\"z\":-1.14}],\"categories\":[{\"column\":\"region\",\"value\":\"Suburbs\",\"share_pct\":58,\"overall_pct\":40}]},{\"group\":3,\"rows\":56,\"share_pct\":23.3,\"columns\":[{\"column\":\"age\",\"average\":37.45,\"overall\":40.1,\"z\":-0.2},{\"column\":\"annual_income_k\",\"average\":88.82,\"overall\":65.69,\"z\":0.8},{\"column\":\"spending_score\",\"average\":80.75,\"overall\":55.52,\"z\":0.97},{\"column\":\"visits_per_month\",\"average\":5.95,\"overall\":5.38,\"z\":0.19}],\"categories\":[{\"column\":\"region\",\"value\":\"City centre\",\"share_pct\":61,\"overall_pct\":35}]},{\"group\":4,\"rows\":55,\"share_pct\":22.9,\"columns\":[{\"column\":\"age\",\"average\":57.04,\"overall\":40.1,\"z\":1.28},{\"column\":\"annual_income_k\",\"average\":41.56,\"overall\":65.69,\"z\":-0.83},{\"column\":\"spending_score\",\"average\":37.16,\"overall\":55.52,\"z\":-0.71},{\"column\":\"visits_per_month\",\"average\":4.18,\"overall\":5.38,\"z\":-0.39}],\"categories\":[{\"column\":\"region\",\"value\":\"Out of town\",\"share_pct\":64,\"overall_pct\":25}]}]",
  "facts": "{\"rows_grouped\":240,\"groups\":4,\"columns\":[\"age\",\"annual_income_k\",\"spending_score\",\"visits_per_month\"],\"columns_sent\":4,\"columns_total\":4,\"fit_score\":0.56,\"fit\":\"clear\",\"scaling\":\"standard\"}"
}

Reply:

{
  "task": "name_groups",
  "groups": [
    {
      "group": 1,
      "name": "Young frequent city centre shoppers",
      "traits": [
        {
          "column": "age",
          "dir": "low"
        },
        {
          "column": "visits_per_month",
          "dir": "high"
        },
        {
          "column": "annual_income_k",
          "dir": "low"
        }
      ]
    },
    {
      "group": 2,
      "name": "Higher income light spenders",
      "traits": [
        {
          "column": "spending_score",
          "dir": "low"
        },
        {
          "column": "annual_income_k",
          "dir": "high"
        },
        {
          "column": "visits_per_month",
          "dir": "low"
        }
      ]
    },
    {
      "group": 3,
      "name": "High income city centre spenders",
      "traits": [
        {
          "column": "spending_score",
          "dir": "high"
        },
        {
          "column": "annual_income_k",
          "dir": "high"
        }
      ]
    },
    {
      "group": 4,
      "name": "Older out of town shoppers",
      "traits": [
        {
          "column": "age",
          "dir": "high"
        },
        {
          "column": "annual_income_k",
          "dir": "low"
        },
        {
          "column": "spending_score",
          "dir": "low"
        }
      ]
    }
  ]
}

On the page, the names go on the chart, the group cards and a group_name column in the download.

Errors

HTTPWhat to do
401The token is missing, expired or a guest token on a run. Get a personal token on the token page.
402The balance is below what the run reserves. Top up at skillsafe.ai/account/billing.
400The body is not a JSON object of the fields above.
429Wait and retry with the same Idempotency-Key.

Step 1 — a tiny client helper

One function that sends JSON and unwraps the {data, error} envelope.

BASE="https://api.skillsafe.ai/v1/app-api"
TOKEN="paste-your-token-here"   # from https://kmeans-clustering.skillsafe.ai/tokens.html
api() { # api METHOD PATH [JSON-BODY]
  curl -s -X "$1" "$BASE$2" -H "Content-Type: application/json" \
    ${TOKEN:+-H "Authorization: Bearer $TOKEN"} ${3:+--data "$3"}
}

Step 2 — a token

A guest token is enough for /me and /estimate. A run is metered, so it needs your personal token: sign in on the token page and copy it.

# a guest token (enough for /me and /estimate); a run needs your personal token from /tokens.html
TOKEN=$(curl -s -X POST "$BASE/guest" -H "Content-Type: application/json" -d '{"slug":"kmeans-clustering"}' | jq -r '.data.token')

Step 3 — who am I, and my balance

subject_type is guest or user; credits is the balance (1 credit = $0.0001).

api GET /me | jq '.data | {subject_type, credits}' 

Step 4 — price a run for free

/estimate charges nothing and starts no job. hold_credits is the amount reserved while a run is in flight, not the price; the run is charged what it uses.

# input.json: the run input object - {"task": "name_groups", "groups": "[...]", "facts": "{...}"}
api POST /estimate "$(cat input.json)" | jq '.data | {hold_credits, min_credits, model, model_alias, markup_bps, warnings}' 

Step 5 — run it and wait for the job

Send an Idempotency-Key derived from the input: a retry with the same key is never billed twice.

KEY="kmeans-clustering:name_groups:$(shasum -a 256 input.json | cut -c1-16):a1"
JOB=$(curl -s -X POST "$BASE/run" -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" \
  -H "Idempotency-Key: $KEY" --data @input.json | jq -r '.data.job_id')
while :; do
  J=$(api GET "/jobs/$JOB"); S=$(echo "$J" | jq -r '.data.status')
  case "$S" in succeeded|failed|cancelled) break;; esac; sleep 2
done
echo "$J" | jq -r '.data.output.output' > reply.json   # one JSON object: task, groups
echo "$J" | jq '.data | {charged_credits, truncated}' 

Step 6 — or stream it

/run-stream sends Server-Sent Events while the job runs and the finished job at the end.

# Server-Sent Events: tick/delta events while the job runs, then a final event with the job
curl -N -s -X POST "$BASE/run-stream" -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" \
  -H "Idempotency-Key: kmeans-clustering:name_groups:$(shasum -a 256 input.json | cut -c1-16):a1" --data @input.json