Skip to content

Instantly share code, notes, and snippets.

@catethos
Created January 20, 2022 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save catethos/32d8e44fd36a474dbb6731a49f94effc to your computer and use it in GitHub Desktop.
Save catethos/32d8e44fd36a474dbb6731a49f94effc to your computer and use it in GitHub Desktop.
js
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "1a2c9d94-d51c-4705-983c-57ad916da262",
"metadata": {
"tags": []
},
"source": [
"## Code"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "23388865-396e-4872-8d5b-feed247dd2ba",
"metadata": {},
"outputs": [],
"source": [
"function cluster(obj, key, tol=0.09){\n",
" obj = JSON.parse(JSON.stringify(obj))\n",
" obj = obj.sort((x,y)=> y[key] - x[key])\n",
" var count = 1\n",
" var cluster = 1\n",
" var centroid = obj[0][key]\n",
" obj[0][\"tag\"] = cluster\n",
"\n",
" for (o of obj.slice(1)) {\n",
" val = o[key]\n",
" if (Math.abs(val - centroid) < tol){\n",
" o[\"tag\"] = cluster\n",
" centroid = (count * centroid + val) / (count + 1)\n",
" count = count + 1\n",
" } else{\n",
" cluster = cluster + 1\n",
" count = 1\n",
" o[\"tag\"] = cluster\n",
" centroid = val\n",
" }\n",
" }\n",
" return obj\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "5db1227d-b6a4-43bd-a942-beecbb012db2",
"metadata": {},
"outputs": [],
"source": [
"function annotate(obj){\n",
" n_groups = obj.slice(-1)[0].tag\n",
" if (n_groups >= 2) {\n",
" for (x of obj){\n",
" if (x.tag == 1) {\n",
" x[\"annotate\"] = \"very important\"\n",
" } else if (n_groups >= 3 && x.tag ==2){\n",
" x[\"annotate\"] = \"important\"\n",
" } else {\n",
" x[\"annotate\"] = null\n",
" }\n",
" }\n",
" }else{\n",
" for (x of obj){\n",
" x[\"annotation\"] = \"balance\"\n",
" }\n",
" }\n",
" return obj\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "390db18e-0d48-4fe1-886b-4daad930f696",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[Function: classify]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"classify = (x, key=\"importance\") => annotate(cluster(x, key))"
]
},
{
"cell_type": "markdown",
"id": "a2d1d12e-2075-45a8-b5a7-82fbff677818",
"metadata": {},
"source": [
"## Examples"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f802682d-7d10-4d17-874b-b0c499207747",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[\n",
" { name: 'skill', importance: 1, tag: 1, annotate: 'very important' },\n",
" {\n",
" name: 'work value',\n",
" importance: 0.4,\n",
" tag: 2,\n",
" annotate: 'important'\n",
" },\n",
" {\n",
" name: 'experience',\n",
" importance: 0.4,\n",
" tag: 2,\n",
" annotate: 'important'\n",
" },\n",
" {\n",
" name: 'work interest',\n",
" importance: 0.35,\n",
" tag: 2,\n",
" annotate: 'important'\n",
" },\n",
" {\n",
" name: 'work style',\n",
" importance: 0.3,\n",
" tag: 2,\n",
" annotate: 'important'\n",
" },\n",
" { name: 'cognitive', importance: 0.2, tag: 3, annotate: null }\n",
"]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"var obj = [\n",
" {name: \"work value\", importance: 0.4},\n",
" {name: \"work style\", importance: 0.3},\n",
" {name: \"work interest\", importance: 0.35},\n",
" {name: \"cognitive\", importance: 0.2},\n",
" {name: \"experience\", importance: 0.4},\n",
" {name: \"skill\", importance: 1}\n",
"]\n",
"\n",
"classify(obj)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "01adc51f-365d-4a8a-b0ca-5b7db407dc42",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[\n",
" {\n",
" name: 'work value',\n",
" importance: 0.3,\n",
" tag: 1,\n",
" annotation: 'balance'\n",
" },\n",
" {\n",
" name: 'work style',\n",
" importance: 0.3,\n",
" tag: 1,\n",
" annotation: 'balance'\n",
" },\n",
" {\n",
" name: 'work interest',\n",
" importance: 0.3,\n",
" tag: 1,\n",
" annotation: 'balance'\n",
" },\n",
" { name: 'cognitive', importance: 0.3, tag: 1, annotation: 'balance' },\n",
" {\n",
" name: 'experience',\n",
" importance: 0.3,\n",
" tag: 1,\n",
" annotation: 'balance'\n",
" },\n",
" { name: 'skill', importance: 0.3, tag: 1, annotation: 'balance' }\n",
"]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"var obj = [\n",
" {name: \"work value\", importance: 0.3},\n",
" {name: \"work style\", importance: 0.3},\n",
" {name: \"work interest\", importance: 0.3},\n",
" {name: \"cognitive\", importance: 0.3},\n",
" {name: \"experience\", importance: 0.3},\n",
" {name: \"skill\", importance: 0.3}\n",
"]\n",
"\n",
"classify(obj)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "b95fc4c3-51ba-44b1-aa5c-aca2e76ee841",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[\n",
" {\n",
" name: 'work value',\n",
" importance: 0.9,\n",
" tag: 1,\n",
" annotate: 'very important'\n",
" },\n",
" { name: 'work style', importance: 0.3, tag: 2, annotate: null },\n",
" { name: 'work interest', importance: 0.3, tag: 2, annotate: null },\n",
" { name: 'cognitive', importance: 0.3, tag: 2, annotate: null },\n",
" { name: 'experience', importance: 0.3, tag: 2, annotate: null },\n",
" { name: 'skill', importance: 0.3, tag: 2, annotate: null }\n",
"]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"var obj = [\n",
" {name: \"work value\", importance: 0.9},\n",
" {name: \"work style\", importance: 0.3},\n",
" {name: \"work interest\", importance: 0.3},\n",
" {name: \"cognitive\", importance: 0.3},\n",
" {name: \"experience\", importance: 0.3},\n",
" {name: \"skill\", importance: 0.3}\n",
"]\n",
"\n",
"classify(obj)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "4d4f2d7e-75ba-46c9-895c-e9a0f37f4511",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[\n",
" {\n",
" name: 'work value',\n",
" importance: 0.9,\n",
" tag: 1,\n",
" annotate: 'very important'\n",
" },\n",
" {\n",
" name: 'work style',\n",
" importance: 0.3,\n",
" tag: 2,\n",
" annotate: 'important'\n",
" },\n",
" {\n",
" name: 'work interest',\n",
" importance: 0.3,\n",
" tag: 2,\n",
" annotate: 'important'\n",
" },\n",
" { name: 'cognitive', importance: 0.3, tag: 2, annotate: 'important' },\n",
" { name: 'experience', importance: 0.1, tag: 3, annotate: null },\n",
" { name: 'skill', importance: 0.1, tag: 3, annotate: null }\n",
"]"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"var obj = [\n",
" {name: \"work value\", importance: 0.9},\n",
" {name: \"work style\", importance: 0.3},\n",
" {name: \"work interest\", importance: 0.3},\n",
" {name: \"cognitive\", importance: 0.3},\n",
" {name: \"experience\", importance: 0.1},\n",
" {name: \"skill\", importance: 0.1}\n",
"]\n",
"\n",
"classify(obj)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9593dd09-9a66-49d8-acf1-4a1ab24794dc",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "JavaScript (Node.js)",
"language": "javascript",
"name": "javascript"
},
"language_info": {
"file_extension": ".js",
"mimetype": "application/javascript",
"name": "javascript",
"version": "16.10.0"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment