Skip to content

Instantly share code, notes, and snippets.

@leliel12
Last active May 8, 2022 00:52
Show Gist options
  • Select an option

  • Save leliel12/5a2f530e4e6e7225a9b29e061171bb54 to your computer and use it in GitHub Desktop.

Select an option

Save leliel12/5a2f530e4e6e7225a9b29e061171bb54 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "cebdbf10-2c78-4b60-a4ea-c282e79fda08",
"metadata": {},
"source": [
"# Pandas DataFrame to Scikit-Criteria DecisionMatrix\n",
"\n",
"First lets create a dataframe with 6 columns and 3 rows"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "99748378-2b70-49c8-9211-07075bfbfddd",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "927e59f1-d58f-419d-9e8e-26b4956f69dc",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>no-criteria-0</th>\n",
" <th>no-criteria-1</th>\n",
" <th>no-criteria-2</th>\n",
" <th>criteria-0</th>\n",
" <th>criteria-1</th>\n",
" <th>criteria-2</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1</td>\n",
" <td>10</td>\n",
" <td>100</td>\n",
" <td>4</td>\n",
" <td>40</td>\n",
" <td>400</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2</td>\n",
" <td>20</td>\n",
" <td>200</td>\n",
" <td>5</td>\n",
" <td>50</td>\n",
" <td>500</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>3</td>\n",
" <td>30</td>\n",
" <td>300</td>\n",
" <td>6</td>\n",
" <td>60</td>\n",
" <td>600</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" no-criteria-0 no-criteria-1 no-criteria-2 criteria-0 criteria-1 \\\n",
"0 1 10 100 4 40 \n",
"1 2 20 200 5 50 \n",
"2 3 30 300 6 60 \n",
"\n",
" criteria-2 \n",
"0 400 \n",
"1 500 \n",
"2 600 "
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = pd.DataFrame({\n",
" \"no-criteria-0\": [1, 2, 3],\n",
" \"no-criteria-1\": [10, 20, 30],\n",
" \"no-criteria-2\": [100, 200, 300],\n",
" \"criteria-0\": [4, 5, 6],\n",
" \"criteria-1\": [40, 50, 60],\n",
" \"criteria-2\": [400, 500, 600],\n",
"})\n",
"df"
]
},
{
"cell_type": "markdown",
"id": "660424c4-1afd-466b-bdf8-a22aa26fc077",
"metadata": {},
"source": [
"Now whe can remove the \"non-criteria\" columns"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "892d5c8d-eea7-4627-af7e-238c0cc82f3c",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>criteria-0</th>\n",
" <th>criteria-1</th>\n",
" <th>criteria-2</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>4</td>\n",
" <td>40</td>\n",
" <td>400</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>5</td>\n",
" <td>50</td>\n",
" <td>500</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>6</td>\n",
" <td>60</td>\n",
" <td>600</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" criteria-0 criteria-1 criteria-2\n",
"0 4 40 400\n",
"1 5 50 500\n",
"2 6 60 600"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"new_df = df[[\"criteria-0\", \"criteria-1\", \"criteria-2\"]]\n",
"new_df"
]
},
{
"cell_type": "markdown",
"id": "67813932-394f-40d6-84b3-0b3fb7b4464d",
"metadata": {},
"source": [
"Now we can use the new df to create a decision matrix"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "f3e76353-ace8-4363-a4bc-c9c5f526f6c5",
"metadata": {},
"outputs": [],
"source": [
"import skcriteria as skc"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "f5a42d37-c338-4e09-8c18-18cdbbc58594",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div class=\"decisionmatrix\">\n",
"<div>\n",
"<style scoped=\"\">\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th/>\n",
" <th>criteria-0[▼ 1.0]</th>\n",
" <th>criteria-1[▲ 1.0]</th>\n",
" <th>criteria-2[▼ 1.0]</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>A0</th>\n",
" <td>4</td>\n",
" <td>40</td>\n",
" <td>400</td>\n",
" </tr>\n",
" <tr>\n",
" <th>A1</th>\n",
" <td>5</td>\n",
" <td>50</td>\n",
" <td>500</td>\n",
" </tr>\n",
" <tr>\n",
" <th>A2</th>\n",
" <td>6</td>\n",
" <td>60</td>\n",
" <td>600</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div><em class=\"decisionmatrix-dim\">3 Alternatives x 3 Criteria</em>\n",
"</div>"
],
"text/plain": [
" criteria-0[▼ 1.0] criteria-1[▲ 1.0] criteria-2[▼ 1.0]\n",
"A0 4 40 400\n",
"A1 5 50 500\n",
"A2 6 60 600\n",
"[3 Alternatives x 3 Criteria]"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dm = skc.mkdm(\n",
" matrix=new_df.to_numpy(), \n",
" objectives=[min, max, min],\n",
" criteria=new_df.columns\n",
")\n",
"\n",
"dm"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bd19323a-0264-4412-ace1-b5a8e485390f",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment