{"id":21455163,"url":"https://github.com/nicomignoni/tab2img","last_synced_at":"2025-07-14T23:32:40.773Z","repository":{"id":49767555,"uuid":"312357962","full_name":"nicomignoni/tab2img","owner":"nicomignoni","description":"A tool to convert tabular data into images, in order to be used by CNNs Inspired by the \"DeepInsight\" paper.","archived":false,"fork":false,"pushed_at":"2021-02-11T11:54:56.000Z","size":507,"stargazers_count":25,"open_issues_count":3,"forks_count":6,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-09T02:18:45.592Z","etag":null,"topics":["cnn","data-preprocessing","deepinsight","tabular-data"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nicomignoni.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-11-12T18:08:15.000Z","updated_at":"2024-06-17T09:09:32.000Z","dependencies_parsed_at":"2022-08-30T15:40:28.053Z","dependency_job_id":null,"html_url":"https://github.com/nicomignoni/tab2img","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nicomignoni%2Ftab2img","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nicomignoni%2Ftab2img/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nicomignoni%2Ftab2img/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nicomignoni%2Ftab2img/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nicomignoni","download_url":"https://codeload.github.com/nicomignoni/tab2img/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":226003010,"owners_count":17558157,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cnn","data-preprocessing","deepinsight","tabular-data"],"created_at":"2024-11-23T05:10:42.690Z","updated_at":"2025-07-14T23:32:40.760Z","avatar_url":"https://github.com/nicomignoni.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# tab2img: from tabular data to images\nA tool to convert tabular data into images for CNN. Inspired by the [DeepInsight](https://www.nature.com/articles/s41598-019-47765-6) paper.\n\n## Installation \n```\npip install tab2img\n```\n\n## Background\n\nIn the [paper](https://www.nature.com/articles/s41598-019-47765-6) \"*DeepInsight: A methodology to transform a non-image data to an image for convolution neural network architecture*\" the autors propose  a method to convert tabular data into images, in order to utilize the power of convolutional neural network (CNN) for non-image structured data.\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"docs/feature_mapping.png\"/\u003e\n\u003c/p\u003e\n\nThe Figure illustrates the main idea: given a training dataset $X \\in \\mathbb{R}^{m \\times n}$ with $m$ samples and $n$ features, we are required to find a function $M \\in \\mathbb{R}^{m \\times n} \\to \\mathbb{R}^{m \\times d \\times d}$, where $d = \\lceil \\sqrt{n} \\rceil$. \n\nThere are numerous ways to choose $M$. In this implementation, the features are organized with respect to the correlation vector $\\rho(X,Y)$, where $Y \\in \\mathbb{R}^{1 \\times m}$ is the target vector.\nGiven $X$ and $Y$ as\n\n$$\nX = \\begin{bmatrix} x^{(1)}_1 \u0026 \\cdots \u0026 x^{(1)}_n \\\\\\ \\vdots \u0026 \\ddots \u0026 \\vdots \\\\\\ x^{(m)}_1 \u0026 \\cdots \u0026 x^{(m)}_n \\end{bmatrix}, \\quad Y = \\begin{bmatrix} y_1 \\\\\\ \\vdots \\\\\\ y_m \\end{bmatrix}\n$$ \n\nVector $\\rho_i$ express the [Pearson correlation coefficient](https://en.wikipedia.org/wiki/Pearson_correlation_coefficient) for the $i$-th feature, i.e., \n\n$$\n\\rho_i = \\rho(X_i, Y), \\quad X_i = \\begin{bmatrix} x^{(1)}_i \\\\\\ \\vdots \\\\\\ x^{(m)}_i \\end{bmatrix}\n$$\n\nIn this case, being $X$ a sample, the correlation coefficient is implemented as \n\n$$\n\\rho(x,y) = \\frac{\\sum_{i=1}^{n}(x_{i}-{\\bar {x}})(y_{i}-{\\bar{y}})}{{\\sqrt{\\sum_{i=1}^{n}(x_{i}-{\\bar{x}})^{2}}}{\\sqrt{\\sum_{i=1}^{n}(y_{i}-{\\bar{y}})^{2}}}}\n$$\n\nAt this point, $\\rho_1, \\dots, \\rho_n$ are sorted from the greatest to the smallest, generating the vector of indices \n\n$$\nJ = \\left[ J_k \\in \\mathbb{N}: \\ \\rho(X_{J_k}, Y) \u003e \\rho(X_{J_{k-1}}, Y), \\ k = 2,\\dots,n \\right]\n$$\n\nEventually, the final tensor $M$ is\n\n$$\nM = \\begin{bmatrix} X_{J_1} \u0026 X_{J_2} \u0026 X_{J_5} \u0026 \\cdots \\\\\\ X_{J_3} \u0026 X_{J_4} \u0026 X_{J_7} \u0026 \\cdots \\\\\\ X_{J_6} \u0026 X_{J_8} \u0026 X_{J_9} \u0026 \\cdots \\\\\\ \\vdots \u0026 \\vdots \u0026 \\vdots \u0026 \\ddots \\end{bmatrix}\n$$\n\nThe mapping from $J_k$ to the right row and column $(r,c)_k$ of $M$ is \n\n$$\n(r, c)_ k = \\begin{cases} (\\sqrt{k}, \\sqrt{k}) \u0026 \\text{if} \\sqrt{k} \\in \\mathbb{N} \\\\\\ (\\lceil\\sqrt{k}\\rceil, \\lceil\\sqrt{k}\\rceil - \\frac{1}{2}(\\lceil\\sqrt{k}\\rceil^2 - k)) \u0026 \\text{if} \\sqrt{k} \\notin \\mathbb{N} \\ \\text{and} \\ \\lceil\\sqrt{k}\\rceil^2 - k = 0 \\mod{2} \\\\\\ (\\lceil\\sqrt{k}\\rceil - \\frac{1}{2}(\\lceil\\sqrt{k}\\rceil^2 - k), \\lceil\\sqrt{k}\\rceil) \u0026 \\text{if} \\sqrt{k} \\notin \\mathbb{N} \\ \\text{and} \\ \\lceil\\sqrt{k}\\rceil^2 - k \\neq 0 \\mod{2} \\end{cases}\n$$\n\n## Example\n\n```python\nfrom sklearn.datasets import fetch_covtype\nfrom tab2img.converter import Tab2Img\n\ndataset = fetch_covtype()\n\ntrain = dataset.data\ntarget = dataset.target\n\nmodel = Tab2Img()\nimages = model.fit_transform(train, target)\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnicomignoni%2Ftab2img","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnicomignoni%2Ftab2img","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnicomignoni%2Ftab2img/lists"}