From 0adf1c81004199f50eecabb96f1cb1f4c5f0030f Mon Sep 17 00:00:00 2001 From: J08nY Date: Wed, 2 Apr 2025 22:47:01 +0200 Subject: More cleanup in scalarmult distinguish. --- epare/distinguish.ipynb | 5388 +++-------------------------------------------- 1 file changed, 272 insertions(+), 5116 deletions(-) diff --git a/epare/distinguish.ipynb b/epare/distinguish.ipynb index e55e828..fdb3f6a 100644 --- a/epare/distinguish.ipynb +++ b/epare/distinguish.ipynb @@ -10,7 +10,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "id": "bc1528b8-61cd-4219-993f-e3f1ac79e801", "metadata": {}, "outputs": [], @@ -35,7 +35,9 @@ "from pyecsca.misc.utils import TaskExecutor, silent\n", "from pyecsca.sca.re.tree import Map, Tree\n", "\n", - "from common import *" + "from common import *\n", + "\n", + "%matplotlib ipympl" ] }, { @@ -49,7 +51,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "id": "ccb00342-3c48-49c9-bedf-2341e5eae3a2", "metadata": {}, "outputs": [], @@ -61,7 +63,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "id": "3dbac9be-d098-479a-8ca2-f531f6668f7c", "metadata": {}, "outputs": [], @@ -79,7 +81,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "id": "38c81e38-a37c-4e58-ac9e-927d14dad458", "metadata": {}, "outputs": [], @@ -107,7 +109,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "id": "2307bf7c-4fac-489d-8527-7ddbf536a148", "metadata": {}, "outputs": [], @@ -118,7 +120,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "id": "0b85fad7-392f-4701-9329-d75d39736bbb", "metadata": {}, "outputs": [], @@ -167,29 +169,17 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "id": "06104104-b612-40e9-bc1d-646356a13381", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Total configs: 378, (6_048 bytes)\n", - "Rows: 378, (9_722_292 bytes)\n", - "Inputs: 3215\n", - "Codomain: 11998\n", - "None in codomain: False\n" - ] - } - ], + "outputs": [], "source": [ "print(dmap.describe())" ] }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "id": "eb8672ca-b76b-411d-b514-2387b555f184", "metadata": {}, "outputs": [], @@ -200,29 +190,17 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "id": "ccba09b0-31c3-450b-af30-efaa64329743", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Total configs: 378, (14_344 bytes)\n", - "Rows: 347, (8_924_972 bytes)\n", - "Inputs: 3215\n", - "Codomain: 11998\n", - "None in codomain: False\n" - ] - } - ], + "outputs": [], "source": [ "print(dmap.describe())" ] }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "id": "5735e7d4-149c-4184-96f7-dcfd6017fbad", "metadata": {}, "outputs": [], @@ -234,583 +212,22 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "id": "d41093df-32c4-450d-922d-5ad042539397", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Dmaps: 1\n", - "Total cfgs: 378\n", - "Height: 7\n", - "Size: 533\n", - "Leaves: 347\n", - "Precise: False\n", - "Leaf sizes: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 4, 4, 4, 4, 5]\n", - "Leaf depths: [1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7]\n", - "Average leaf depth: 4.118\n", - "Average leaf size: 1.089\n", - "Random walk leaf depth: 2.642\n", - "Random walk leaf size: 1.041\n", - "Mean result depth: 4.153\n", - "Mean result size: 1.270\n" - ] - } - ], + "outputs": [], "source": [ "print(tree.describe())" ] }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "id": "de577429-d87c-4967-be17-75cbb378860c", "metadata": { "scrolled": true }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "378\n", - "├── 1\n", - "├── 10\n", - "│ ├── 1\n", - "│ ├── 1\n", - "│ ├── 1\n", - "│ ├── 1\n", - "│ ├── 4\n", - "│ │ ├── 1\n", - "│ │ └── 3\n", - "│ │ ├── 1\n", - "│ │ └── 2\n", - "│ │ ├── 1\n", - "│ │ └── 1\n", - "│ ├── 1\n", - "│ └── 1\n", - "├── 10\n", - "│ ├── 1\n", - "│ ├── 2\n", - "│ │ ├── 1\n", - "│ │ └── 1\n", - "│ ├── 4\n", - "│ │ ├── 1\n", - "│ │ └── 3\n", - "│ │ ├── 1\n", - "│ │ └── 2\n", - "│ │ ├── 1\n", - "│ │ └── 1\n", - "│ └── 3\n", - "│ ├── 1\n", - "│ ├── 1\n", - "│ └── 1\n", - "├── 49\n", - "│ ├── 1\n", - "│ ├── 13\n", - "│ │ ├── 1\n", - "│ │ ├── 2\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ ├── 2\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ ├── 1\n", - "│ │ ├── 3\n", - "│ │ │ ├── 1\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ ├── 2\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ └── 2\n", - "│ │ ├── 1\n", - "│ │ └── 1\n", - "│ ├── 10\n", - "│ │ ├── 1\n", - "│ │ ├── 4\n", - "│ │ │ ├── 1\n", - "│ │ │ ├── 2\n", - "│ │ │ │ ├── 1\n", - "│ │ │ │ └── 1\n", - "│ │ │ └── 1\n", - "│ │ ├── 1\n", - "│ │ └── 4\n", - "│ │ ├── 1\n", - "│ │ ├── 2\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ └── 1\n", - "│ ├── 11\n", - "│ │ ├── 5\n", - "│ │ │ ├── 2\n", - "│ │ │ │ ├── 1\n", - "│ │ │ │ └── 1\n", - "│ │ │ └── 3\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 2\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ ├── 5\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 4\n", - "│ │ └── 1\n", - "│ ├── 4\n", - "│ │ ├── 1\n", - "│ │ └── 3\n", - "│ ├── 8\n", - "│ │ ├── 1\n", - "│ │ ├── 1\n", - "│ │ ├── 3\n", - "│ │ │ ├── 1\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ └── 3\n", - "│ │ ├── 1\n", - "│ │ ├── 1\n", - "│ │ └── 1\n", - "│ ├── 1\n", - "│ └── 1\n", - "├── 20\n", - "│ ├── 4\n", - "│ │ ├── 1\n", - "│ │ ├── 1\n", - "│ │ ├── 1\n", - "│ │ └── 1\n", - "│ ├── 1\n", - "│ ├── 1\n", - "│ ├── 1\n", - "│ ├── 3\n", - "│ │ ├── 1\n", - "│ │ ├── 1\n", - "│ │ └── 1\n", - "│ ├── 1\n", - "│ ├── 1\n", - "│ ├── 5\n", - "│ │ ├── 1\n", - "│ │ ├── 2\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ └── 2\n", - "│ │ ├── 1\n", - "│ │ └── 1\n", - "│ ├── 1\n", - "│ ├── 1\n", - "│ └── 1\n", - "├── 53\n", - "│ ├── 20\n", - "│ │ ├── 2\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ ├── 4\n", - "│ │ │ ├── 1\n", - "│ │ │ ├── 1\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ ├── 1\n", - "│ │ ├── 2\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ ├── 1\n", - "│ │ ├── 5\n", - "│ │ │ ├── 1\n", - "│ │ │ ├── 1\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 2\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ ├── 2\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ └── 3\n", - "│ │ ├── 1\n", - "│ │ ├── 1\n", - "│ │ └── 1\n", - "│ ├── 19\n", - "│ │ ├── 6\n", - "│ │ │ ├── 2\n", - "│ │ │ │ ├── 1\n", - "│ │ │ │ └── 1\n", - "│ │ │ ├── 2\n", - "│ │ │ │ ├── 1\n", - "│ │ │ │ └── 1\n", - "│ │ │ └── 2\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ ├── 7\n", - "│ │ │ ├── 1\n", - "│ │ │ ├── 3\n", - "│ │ │ │ ├── 1\n", - "│ │ │ │ ├── 1\n", - "│ │ │ │ └── 1\n", - "│ │ │ └── 3\n", - "│ │ │ ├── 2\n", - "│ │ │ │ ├── 1\n", - "│ │ │ │ └── 1\n", - "│ │ │ └── 1\n", - "│ │ ├── 3\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 2\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ └── 3\n", - "│ │ ├── 1\n", - "│ │ └── 2\n", - "│ ├── 5\n", - "│ │ ├── 2\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ ├── 1\n", - "│ │ └── 2\n", - "│ │ ├── 1\n", - "│ │ └── 1\n", - "│ ├── 7\n", - "│ │ ├── 2\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ ├── 2\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ ├── 1\n", - "│ │ └── 2\n", - "│ └── 2\n", - "│ ├── 1\n", - "│ └── 1\n", - "├── 134\n", - "│ ├── 23\n", - "│ │ ├── 5\n", - "│ │ │ ├── 1\n", - "│ │ │ ├── 1\n", - "│ │ │ ├── 1\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ ├── 6\n", - "│ │ │ ├── 2\n", - "│ │ │ │ ├── 1\n", - "│ │ │ │ └── 1\n", - "│ │ │ ├── 2\n", - "│ │ │ │ ├── 1\n", - "│ │ │ │ └── 1\n", - "│ │ │ └── 2\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ ├── 1\n", - "│ │ ├── 1\n", - "│ │ ├── 1\n", - "│ │ ├── 1\n", - "│ │ ├── 1\n", - "│ │ ├── 1\n", - "│ │ └── 6\n", - "│ │ ├── 2\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ ├── 2\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ └── 2\n", - "│ │ ├── 1\n", - "│ │ └── 1\n", - "│ ├── 6\n", - "│ │ ├── 1\n", - "│ │ ├── 2\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ ├── 1\n", - "│ │ └── 2\n", - "│ │ ├── 1\n", - "│ │ └── 1\n", - "│ ├── 7\n", - "│ │ ├── 1\n", - "│ │ ├── 2\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ ├── 1\n", - "│ │ ├── 2\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ └── 1\n", - "│ ├── 24\n", - "│ │ ├── 3\n", - "│ │ │ ├── 1\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ ├── 7\n", - "│ │ │ ├── 2\n", - "│ │ │ └── 5\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 4\n", - "│ │ ├── 2\n", - "│ │ ├── 7\n", - "│ │ │ ├── 4\n", - "│ │ │ │ ├── 1\n", - "│ │ │ │ ├── 1\n", - "│ │ │ │ └── 2\n", - "│ │ │ │ ├── 1\n", - "│ │ │ │ └── 1\n", - "│ │ │ └── 3\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 2\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ └── 5\n", - "│ │ ├── 3\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 2\n", - "│ │ └── 2\n", - "│ │ ├── 1\n", - "│ │ └── 1\n", - "│ ├── 46\n", - "│ │ ├── 11\n", - "│ │ │ ├── 3\n", - "│ │ │ │ ├── 1\n", - "│ │ │ │ └── 2\n", - "│ │ │ │ ├── 1\n", - "│ │ │ │ └── 1\n", - "│ │ │ ├── 3\n", - "│ │ │ │ ├── 1\n", - "│ │ │ │ ├── 1\n", - "│ │ │ │ └── 1\n", - "│ │ │ ├── 4\n", - "│ │ │ │ ├── 2\n", - "│ │ │ │ │ ├── 1\n", - "│ │ │ │ │ └── 1\n", - "│ │ │ │ └── 2\n", - "│ │ │ └── 1\n", - "│ │ ├── 8\n", - "│ │ │ ├── 5\n", - "│ │ │ │ ├── 1\n", - "│ │ │ │ └── 4\n", - "│ │ │ └── 3\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 2\n", - "│ │ ├── 1\n", - "│ │ ├── 10\n", - "│ │ │ ├── 3\n", - "│ │ │ │ ├── 1\n", - "│ │ │ │ └── 2\n", - "│ │ │ └── 7\n", - "│ │ │ ├── 5\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ ├── 9\n", - "│ │ │ ├── 1\n", - "│ │ │ ├── 4\n", - "│ │ │ │ ├── 2\n", - "│ │ │ │ │ ├── 1\n", - "│ │ │ │ │ └── 1\n", - "│ │ │ │ └── 2\n", - "│ │ │ │ ├── 1\n", - "│ │ │ │ └── 1\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 3\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 2\n", - "│ │ └── 7\n", - "│ │ ├── 1\n", - "│ │ ├── 2\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ ├── 1\n", - "│ │ ├── 2\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ └── 1\n", - "│ ├── 2\n", - "│ │ ├── 1\n", - "│ │ └── 1\n", - "│ ├── 6\n", - "│ │ ├── 2\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ ├── 2\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ └── 2\n", - "│ │ ├── 1\n", - "│ │ └── 1\n", - "│ ├── 1\n", - "│ ├── 12\n", - "│ │ ├── 2\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ ├── 1\n", - "│ │ ├── 2\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ ├── 2\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ ├── 1\n", - "│ │ ├── 2\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ ├── 1\n", - "│ │ └── 1\n", - "│ ├── 1\n", - "│ ├── 1\n", - "│ ├── 2\n", - "│ │ ├── 1\n", - "│ │ └── 1\n", - "│ ├── 1\n", - "│ └── 2\n", - "│ ├── 1\n", - "│ └── 1\n", - "├── 97\n", - "│ ├── 27\n", - "│ │ ├── 7\n", - "│ │ │ ├── 2\n", - "│ │ │ │ ├── 1\n", - "│ │ │ │ └── 1\n", - "│ │ │ ├── 2\n", - "│ │ │ │ ├── 1\n", - "│ │ │ │ └── 1\n", - "│ │ │ ├── 2\n", - "│ │ │ │ ├── 1\n", - "│ │ │ │ └── 1\n", - "│ │ │ └── 1\n", - "│ │ ├── 2\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ ├── 3\n", - "│ │ │ ├── 1\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ ├── 1\n", - "│ │ ├── 7\n", - "│ │ │ ├── 2\n", - "│ │ │ │ ├── 1\n", - "│ │ │ │ └── 1\n", - "│ │ │ ├── 1\n", - "│ │ │ ├── 1\n", - "│ │ │ ├── 1\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ ├── 4\n", - "│ │ │ ├── 1\n", - "│ │ │ ├── 1\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ ├── 2\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ └── 1\n", - "│ ├── 2\n", - "│ │ ├── 1\n", - "│ │ └── 1\n", - "│ ├── 11\n", - "│ │ ├── 3\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 2\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ ├── 4\n", - "│ │ │ ├── 2\n", - "│ │ │ │ ├── 1\n", - "│ │ │ │ └── 1\n", - "│ │ │ └── 2\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ └── 4\n", - "│ │ ├── 2\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ ├── 1\n", - "│ │ └── 1\n", - "│ ├── 6\n", - "│ │ ├── 2\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ ├── 3\n", - "│ │ │ ├── 2\n", - "│ │ │ │ ├── 1\n", - "│ │ │ │ └── 1\n", - "│ │ │ └── 1\n", - "│ │ └── 1\n", - "│ ├── 13\n", - "│ │ ├── 8\n", - "│ │ │ ├── 4\n", - "│ │ │ │ ├── 2\n", - "│ │ │ │ └── 2\n", - "│ │ │ │ ├── 1\n", - "│ │ │ │ └── 1\n", - "│ │ │ └── 4\n", - "│ │ │ ├── 3\n", - "│ │ │ │ ├── 2\n", - "│ │ │ │ └── 1\n", - "│ │ │ └── 1\n", - "│ │ ├── 4\n", - "│ │ │ ├── 2\n", - "│ │ │ │ ├── 1\n", - "│ │ │ │ └── 1\n", - "│ │ │ └── 2\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ └── 1\n", - "│ ├── 27\n", - "│ │ ├── 11\n", - "│ │ │ ├── 4\n", - "│ │ │ │ ├── 2\n", - "│ │ │ │ │ ├── 1\n", - "│ │ │ │ │ └── 1\n", - "│ │ │ │ └── 2\n", - "│ │ │ │ ├── 1\n", - "│ │ │ │ └── 1\n", - "│ │ │ ├── 5\n", - "│ │ │ │ ├── 1\n", - "│ │ │ │ └── 4\n", - "│ │ │ └── 2\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ ├── 9\n", - "│ │ │ ├── 4\n", - "│ │ │ │ ├── 1\n", - "│ │ │ │ └── 3\n", - "│ │ │ │ ├── 1\n", - "│ │ │ │ └── 2\n", - "│ │ │ │ ├── 1\n", - "│ │ │ │ └── 1\n", - "│ │ │ └── 5\n", - "│ │ │ ├── 2\n", - "│ │ │ │ ├── 1\n", - "│ │ │ │ └── 1\n", - "│ │ │ ├── 2\n", - "│ │ │ │ ├── 1\n", - "│ │ │ │ └── 1\n", - "│ │ │ └── 1\n", - "│ │ ├── 6\n", - "│ │ │ ├── 3\n", - "│ │ │ └── 3\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 2\n", - "│ │ │ ├── 1\n", - "│ │ │ └── 1\n", - "│ │ └── 1\n", - "│ ├── 1\n", - "│ ├── 1\n", - "│ ├── 1\n", - "│ ├── 3\n", - "│ │ ├── 1\n", - "│ │ └── 2\n", - "│ │ ├── 1\n", - "│ │ └── 1\n", - "│ └── 5\n", - "│ ├── 2\n", - "│ │ ├── 1\n", - "│ │ └── 1\n", - "│ ├── 2\n", - "│ │ ├── 1\n", - "│ │ └── 1\n", - "│ └── 1\n", - "└── 4\n", - " ├── 1\n", - " ├── 1\n", - " ├── 1\n", - " └── 1\n" - ] - } - ], + "outputs": [], "source": [ "print(tree.render_basic())" ] @@ -900,7 +317,7 @@ }, { "cell_type": "code", - "execution_count": 86, + "execution_count": null, "id": "cc1a9956-bc8c-47cf-b6ec-093c6cf85c7d", "metadata": { "editable": true, @@ -909,23 +326,7 @@ }, "tags": [] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[3]\n", - "[[-0.53851648 -0.2236068 -0. -0.50990195]\n", - " [-0.14142136 -0.42426407 -0.51961524 -0.80622577]]\n", - "[3]\n", - "[[1.04188836e-27 1.24861430e-05 9.99987514e-01 1.14579810e-34]]\n", - "1.0\n", - "1.0\n", - "1.0\n", - "1.0\n" - ] - } - ], + "outputs": [], "source": [ "from sklearn.base import BaseEstimator, ClassifierMixin\n", "from sklearn.utils.validation import validate_data, check_is_fitted\n", @@ -1013,34 +414,43 @@ " nmults = len(mults_map)\n", " classes = np.arange(nmults, dtype=np.uint32)\n", " probs = np.zeros((nmults, nfeats), dtype=np.float64)\n", + " mults = sorted(list(mults_map.keys()))\n", " for i, divisor in enumerate(feats):\n", - " for j, probmap in enumerate(mults_map.values()):\n", + " for j, mult in enumerate(mults):\n", + " probmap = mults_map[mult]\n", " probs[j, i] = probmap[divisor]\n", " return probs, classes\n", "\n", "\n", - "def evaluate_classifier(nattack: int,\n", - " simulations: int,\n", - " X,\n", - " y,\n", - " classifier,\n", - " scorer):\n", - " #X, y = to_sklearn(mults, feats)\n", + "def make_instance(nattack: int,\n", + " simulations: int,\n", + " X,\n", + " y,\n", + " progress=False):\n", " nmults, nfeats = X.shape\n", - " classifier.set_params(nattack=nattack)\n", - " classifier.fit(X, y)\n", - "\n", " X_samp = np.zeros((simulations, nfeats), dtype=np.uint32)\n", " y_samp = np.zeros(simulations, dtype=np.uint32)\n", "\n", - " for i in range(simulations):\n", + " r = trange(simulations) if progress else range(simulations)\n", + " for i in r:\n", " if i < nmults and simulations >= nmults:\n", " j = i\n", " else:\n", " j = random.randrange(nmults)\n", " X_samp[i] = binom(nattack, X[j]).rvs()\n", " y_samp[i] = j\n", + " return X_samp, y_samp\n", + "\n", "\n", + "def evaluate_classifier(nattack: int,\n", + " simulations: int,\n", + " X,\n", + " y,\n", + " classifier,\n", + " scorer):\n", + " classifier.set_params(nattack=nattack)\n", + " classifier.fit(X, y)\n", + " X_samp, y_samp = make_instance(nattack, simulations, X, y)\n", " return scorer(classifier, X_samp, y_samp)\n", "\n", "\n", @@ -1137,7 +547,7 @@ }, { "cell_type": "code", - "execution_count": 82, + "execution_count": null, "id": "a9fae775-797f-4efe-ac28-d83a8c905372", "metadata": {}, "outputs": [], @@ -1267,17 +677,22 @@ }, { "cell_type": "code", - "execution_count": 76, + "execution_count": null, "id": "f1c0bebe-c519-4241-a163-63613b929db2", "metadata": {}, "outputs": [], "source": [ - "def plot_performance(classifier, scorer, simulations, feature_map, mults):\n", + "def plot_performance(classifier, scorer, simulations, feature_map, mults, num_workers = 30):\n", " scores = {}\n", - " for (nattack, nfeats), feats in tqdm(feature_map.items(), desc=\"Evaluating\", leave=False):\n", - " X, y = to_sklearn(mults, feats)\n", - " score = evaluate_classifier(nattack, simulations, X, y, classifier, scorer)\n", - " scores[(nattack, nfeats)] = score\n", + " with TaskExecutor(max_workers=num_workers) as pool:\n", + " for (nattack, nfeats), feats in feature_map.items():\n", + " X, y = to_sklearn(mults, feats)\n", + " pool.submit_task((nattack, nfeats),\n", + " evaluate_classifier,\n", + " nattack, simulations, X, y, classifier, scorer)\n", + " for (nattack, nfeats), future in tqdm(pool.as_completed(), desc=\"Evaluating\", leave=False, total=len(pool.tasks)):\n", + " score = future.result()\n", + " scores[(nattack, nfeats)] = score\n", "\n", " x_coords = [k[0] for k in scores.keys()]\n", " y_coords = [k[1] for k in scores.keys()]\n", @@ -1305,8 +720,8 @@ " np.linspace(min(y_unique), max(y_unique), 100))\n", " z_contour = x_contour * y_contour\n", " \n", - " contour = plt.contour(x_contour, y_contour, z_contour, levels=[100, 200, 300, 400, 500], colors='white', zorder=4)\n", - " plt.clabel(contour, inline=True, fontsize=8)\n", + " contour = plt.contour(x_contour, y_contour, z_contour, levels=[100, 200, 300, 400, 500, 600, 700, 800, 900, 1000], colors='white', zorder=4)\n", + " plt.clabel(contour, inline=True, fontsize=10)\n", " \n", " plt.xticks(ticks=x_unique, labels=x_unique)\n", " plt.yticks(ticks=y_unique, labels=y_unique)\n", @@ -1335,1369 +750,10 @@ }, { "cell_type": "code", - "execution_count": 72, + "execution_count": null, "id": "6e3260c9-c0fa-4828-a749-4d34499abacf", "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "2726629e5a434a43a05103635da433eb", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "nattack: 0%| | 0/6 [00:00" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ - "plot_performance(euclid_classifier, top_5_scorer, 500, tre, distributions_mults)" + "plot_performance(euclid_classifier, top_5_scorer, 500, selected_euclid_fromtree, distributions_mults)" ] }, { @@ -2777,7 +808,7 @@ }, { "cell_type": "code", - "execution_count": 60, + "execution_count": null, "id": "1f24b323-3604-4e34-a880-9dfd611fb245", "metadata": { "scrolled": true @@ -2794,1369 +825,10 @@ }, { "cell_type": "code", - "execution_count": 61, + "execution_count": null, "id": "f1052222-ad32-4e25-97ca-851cc42bf546", "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "dd0c41b2ba854130a3e11c46e17ec1b0", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "nattack: 0%| | 0/6 [00:00" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "plot_performance(bayes_classifier, top_5_scorer, 500, bay, distributions_mults)" ] @@ -4218,247 +865,9 @@ { "cell_type": "code", "execution_count": null, - "id": "93c778a4-0855-4248-91a9-750fdd76ffa6", - "metadata": { - "jupyter": { - "source_hidden": true - } - }, - "outputs": [], - "source": [ - "def find_features_greedy(nfeats, nattack, num_workers, simulations, scorer, start_features=None):\n", - " available_feats = selected_divisors\n", - " feats = []\n", - " if start_features is not None:\n", - " if nfeats <= len(start_features):\n", - " raise ValueError(\"Features already picked.\")\n", - " feats.extend(start_features)\n", - " for feat in start_features:\n", - " available_feats.remove(feat)\n", - "\n", - " with TaskExecutor(max_workers=num_workers) as pool:\n", - " while len(feats) < nfeats:\n", - " for feat in available_feats:\n", - " pool.submit_task(feat,\n", - " many_simulations,\n", - " nattack, distributions_mults, feats + [feat], scorer, simulations)\n", - " best_feat = None\n", - " best_feat_mean_pos = None\n", - " best_successes = Noned\n", - " for feat, future in tqdm(pool.as_completed(), total=len(available_feats), desc=f\"Picking feature {len(feats)}\", smoothing=0):\n", - " mean_pos, successes = future.result()\n", - " if best_feat is None or best_feat_mean_pos > mean_pos:\n", - " best_feat = feat\n", - " best_feat_mean_pos = mean_pos\n", - " best_successes = successes\n", - " print(f\"Picked {best_feat} with mean pos: {mean_pos:.2f}\")\n", - " print(f\"top1: {best_successes[1]:.2f}, top2: {best_successes[2]:.2f}, top5: {best_successes[5]:.2f}, top10: {best_successes[10]:.2f}\")\n", - " feats.append(best_feat)\n", - " available_feats.remove(best_feat)\n", - " return feats" - ] - }, - { - "cell_type": "code", - "execution_count": 79, "id": "6e4c2313-83b0-43f8-80d6-14c39be0d9ec", "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "8ca4c6298c494e538470d7cc19357ad5", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "nattack: 0%| | 0/6 [00:00" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ - "plot_performance(bayes_classifier, top_5_scorer, 500, gre, distributions_mults)" + "plot_performance(bayes_classifier, top_5_scorer, 500, selected_bayes_greedy_fromtree, distributions_mults)" ] }, { "cell_type": "code", - "execution_count": 83, + "execution_count": null, "id": "69ce91fa-7475-41f1-a3ed-bc4dd97d44d6", - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "30adc7f9575145209097d858ff84a17f", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "nattack: 0%| | 0/6 [00:00" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "plot_performance(bayes_classifier, top_5_scorer, 500, gre, distributions_mults)" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "ea3e2f00-9bdf-4014-9c1e-85fa48304ef3", + "metadata": {}, + "outputs": [], + "source": [ + "from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay\n", + "\n", + "nattack = 50\n", + "nfeats = 5\n", + "simulations = 20000\n", + "bayes = BayesClassifier(nattack=nattack)\n", + "X, y = to_sklearn(distributions_mults, selected_bayes_greedy_fromall[(nattack, nfeats)])\n", + "bayes.fit(X, y)\n", + "\n", + "X_samp, y_samp = make_instance(nattack, simulations, X, y, progress=True)\n", + "fig, ax = plt.subplots(figsize=(12,8))\n", + "disp = ConfusionMatrixDisplay.from_predictions(y_samp, bayes.predict(X_samp), ax=ax, normalize=\"true\", include_values=False, xticks_rotation=\"vertical\")\n", + "\n", + "ticks = []\n", + "labs = []\n", + "kls = None\n", + "for i, mult in enumerate(sorted(list(distributions_mults.keys()))):\n", + " if kls is None or kls != mult.klass:\n", + " ticks.append(i)\n", + " labs.append(mult.klass.__name__)\n", + " kls = mult.klass\n", + "ax.set_xticks(ticks, labs)\n", + "ax.set_yticks(ticks, labs)\n", + "ax.set_xticks([], minor=True)\n", + "ax.set_yticks([], minor=True);" + ] + }, { "cell_type": "markdown", "id": "7c030a1c-c13d-401a-bcdb-212c064681e4", @@ -6134,52 +1170,64 @@ { "cell_type": "code", "execution_count": null, - "id": "febfb392-017f-442f-8aaa-cb48bcdb9a6b", + "id": "6a1ec802-7e8e-4ac1-beb8-72b6e14e5a6c", + "metadata": {}, + "outputs": [], + "source": [ + "def compute_priors(nmults: int):\n", + " return np.full(nmults, 1/nmults, dtype=np.float64)\n", + "\n", + "def compute_probs(feats: list[int], mults_map: dict[MultIdent, ProbMap]):\n", + " probs, _ = to_sklearn(mults_map, feats)\n", + " return probs.T\n", + "\n", + "def compute_relevance(feats: list[int], priors, probs, nattack: int):\n", + " relevance = np.zeros(nallfeats, dtype=np.float64)\n", + " for i, divisor in enumerate(tqdm(feats)):\n", + " mi = mutual_information(priors, probs[i, ], nattack)\n", + " relevance[i] = mi\n", + " return relevance\n", + "\n", + "def compute_redundancy(feats: list[int], priors, probs, nattack: int, num_workers: int = 30):\n", + " nallfeats = len(feats)\n", + " redundancy = np.zeros((nallfeats, nallfeats), dtype=np.float64)\n", + " with TaskExecutor(max_workers=num_workers) as pool:\n", + " for i in trange(nallfeats):\n", + " for j in range(nallfeats):\n", + " if i < j:\n", + " continue\n", + " pool.submit_task((i, j),\n", + " mutual_information_between_features,\n", + " priors, probs[i, ], probs[j, ], nattack)\n", + " for (i, j), future in pool.as_completed():\n", + " mi = future.result()\n", + " redundancy[i][j] = mi\n", + " redundancy[j][i] = mi\n", + " return redundancy" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4e14ed9b-56af-4b36-9615-0bcee45e4b40", "metadata": {}, "outputs": [], "source": [ - "priors = np.full(nmults, 1/nmults, dtype=np.float64)\n", - "probs = np.zeros((nallfeats, nmults), dtype=np.float64)\n", - "for i, divisor in enumerate(allfeats):\n", - " for j, (mult, probmap) in enumerate(distributions_mults.items()):\n", - " probs[i, j] = probmap[divisor]\n", - "\n", "nattack = 100\n", - "mis = []\n", - "relevance = np.zeros(nallfeats, dtype=np.float64)\n", - "for i, divisor in enumerate(allfeats):\n", - " mi = mutual_information(priors, probs[i, ], nattack)\n", - " relevance[i] = mi\n", - " mis.append((mi, divisor))\n", - "mis.sort(key=lambda item: item[0], reverse=True)\n", "\n", - "print(\"Top 10 feats\")\n", - "for mi, divisor in mis[:10]:\n", - " print(f\"{divisor} {mi:.3f}\")" + "priors = compute_priors(nmults)\n", + "probs = compute_probs(allfeats, distributions_mults)\n", + "relevance = compute_relevance(allfeats, priors, probs, nattack)" ] }, { "cell_type": "code", "execution_count": null, - "id": "8361d1a3-87d1-4d35-9a8a-1a9a4a6eb638", + "id": "7a49f7b0-f9cf-4862-8638-0b4ba5dd4f07", "metadata": {}, "outputs": [], "source": [ - "num_workers = 30\n", - "\n", - "redundancy = np.zeros((nallfeats, nallfeats), dtype=np.float64)\n", - "with TaskExecutor(max_workers=num_workers) as pool:\n", - " for i in trange(nallfeats):\n", - " for j in range(nallfeats):\n", - " if i < j:\n", - " continue\n", - " pool.submit_task((i, j),\n", - " mutual_information_between_features,\n", - " priors, probs[i, ], probs[j, ], nattack)\n", - " for (i, j), future in pool.as_completed():\n", - " mi = future.result()\n", - " redundancy[i][j] = mi\n", - " redundancy[j][i] = mi\n" + "redundancy = compute_redundancy(allfeats, priors, probs, nattack)" ] }, { @@ -6210,6 +1258,23 @@ "metadata": {}, "outputs": [], "source": [ + "class MRMRFeatures(FeatureSelector):\n", + " def __init__(self,\n", + " allfeats: list[int],\n", + " mults: dict[MultIdent, ProbMap],\n", + " num_workers: int):\n", + " self.allfeats = allfeats\n", + " self.mults = mults\n", + " self.num_workers = num_workers\n", + "\n", + " def prepare(self, nattack: int):\n", + " self.nattack = nattack\n", + "\n", + " def select(self, nfeats: int, startwith: list[int] = None) -> list[int]:\n", + " pass\n", + "\n", + "\n", + "\n", "def mrmr_selection(relevance, redundancy, nfeats):\n", " \"\"\"\n", " Select top features using mRMR.\n", @@ -6255,7 +1320,34 @@ "metadata": {}, "outputs": [], "source": [ - "selected_mrmr = [allfeats[i] for i in mrmr_selection(relevance, redundancy, nfeats=5)]" + "selected_mrmr = [allfeats[i] for i in mrmr_selection(relevance, redundancy, nfeats=15)]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "37cce925-8479-43e9-ad0c-b1d42be4991c", + "metadata": {}, + "outputs": [], + "source": [ + "mrmrs = {}\n", + "for nfeats in trange(1, 41, 1):\n", + " f = [allfeats[i] for i in mrmr_selection(relevance, redundancy, nfeats=nfeats)]\n", + " for nattack in range(5, 105, 5):\n", + " mrmrs[(nattack, nfeats)] = f\n", + "\n", + "bayes_classifier = BayesClassifier()\n", + "plot_performance(bayes_classifier, avg_rank_scorer, 2000, mrmrs, distributions_mults)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2a8bef1c-800b-453b-b239-29ea1481213b", + "metadata": {}, + "outputs": [], + "source": [ + "plt.close()" ] }, { @@ -6319,6 +1411,14 @@ " return selected_indices" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "216a22d8-f27f-4584-8769-bd575ed538b1", + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "code", "execution_count": null, @@ -6326,8 +1426,64 @@ "metadata": {}, "outputs": [], "source": [ - "selected_jmi = [allfeats[i] for i in jmi_selection(probs, priors, nattack, relevance, nfeats=5)]" + "nattack = 100\n", + "selected_jmi = [allfeats[i] for i in jmi_selection(probs, priors, nattack, relevance, nfeats=40)]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7c65b785-0131-4d41-9131-b519bf446803", + "metadata": {}, + "outputs": [], + "source": [ + "selected_jmi" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "899dd2fa-409a-4e6c-aa13-f2d6b0088627", + "metadata": {}, + "outputs": [], + "source": [ + "mrmrs[(100,5)]" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9a894db6-0e4d-49c6-b259-cc9bd28e8c8f", + "metadata": {}, + "outputs": [], + "source": [ + "jmis = {}\n", + "for nfeats in range(1, 6, 1):\n", + " f = selected_jmi[:nfeats]\n", + " for nattack in range(5, 105, 5):\n", + " jmis[(nattack, nfeats)] = f\n", + "\n", + "bayes_classifier = BayesClassifier()\n", + "plot_performance(bayes_classifier, avg_rank_scorer, 2000, jmis, distributions_mults)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4c29cc75-7bee-47d0-b0cb-c0f5a7ec5da0", + "metadata": {}, + "outputs": [], + "source": [ + "plt.close()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c5598117-4c54-4721-9fc5-68432fb8e230", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { -- cgit v1.2.3-70-g09d2