{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Ciencia de Datos 2022\n", "\n", "### Alan Reyes-Figueroa" ] }, { "cell_type": "markdown", "metadata": { "jupyter": { "source_hidden": true }, "tags": [] }, "source": [ "Ejemplo tomado de la documentación de la librerÃa *MiniSom*, https://github.com/JustGlowing/minisom.\n", "\n", "In this example we will see how to use MiniSom to draw some insights from the Democracy Index data from Wikipedia.\n", "\n", "First, let's load the dataset:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "from matplotlib.patches import Patch\n", "%matplotlib inline\n", "\n", "from minisom import MiniSom\n", "from sklearn.preprocessing import minmax_scale, scale\n", "\n", "# download from wikipedia and reorganization\n", "import os.path\n", "if not os.path.isfile('democracy_index.csv'):\n", " wikitables = pd.read_html('https://en.wikipedia.org/wiki/Democracy_Index',\n", " attrs={\"class\":\"sortable\"}, header=0)\n", " democracy_index = wikitables[0]\n", " democracy_index.columns = [c.lower().replace(' ', '_') for c in democracy_index.columns]\n", " democracy_index.rename(columns={'score': 'democracy_index', \n", " 'functioning_ofgovernment': 'functioning_of_government',\n", " 'politicalparticipation': 'political_participation',\n", " 'politicalculture': 'political_culture',\n", " 'civilliberties': 'civil_liberties'}, inplace=True)\n", " democracy_index.category = democracy_index.category.replace('Flawed democracy[a]', 'Flawed democracy')\n", " democracy_index = democracy_index[:-1]\n", " democracy_index.to_csv('democracy_index.csv')\n", " print('data downloaded from Wikipedia')\n", "else:\n", " # pre-downloaded file\n", " democracy_index = pd.read_csv('democracy_index.csv')" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | Unnamed: 0 | \n", "rank | \n", "country | \n", "democracy_index | \n", "electoral_processand_pluralism | \n", "functioning_of_government | \n", "political_participation | \n", "political_culture | \n", "civil_liberties | \n", "category | \n", "
---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "0 | \n", "1 | \n", "Norway | \n", "9.87 | \n", "10.00 | \n", "9.64 | \n", "10.00 | \n", "10.00 | \n", "9.71 | \n", "Full democracy | \n", "
1 | \n", "1 | \n", "2 | \n", "Iceland | \n", "9.58 | \n", "10.00 | \n", "9.29 | \n", "8.89 | \n", "10.00 | \n", "9.71 | \n", "Full democracy | \n", "
2 | \n", "2 | \n", "3 | \n", "Sweden | \n", "9.39 | \n", "9.58 | \n", "9.64 | \n", "8.33 | \n", "10.00 | \n", "9.41 | \n", "Full democracy | \n", "
3 | \n", "3 | \n", "4 | \n", "New Zealand | \n", "9.26 | \n", "10.00 | \n", "9.29 | \n", "8.89 | \n", "8.13 | \n", "10.00 | \n", "Full democracy | \n", "
4 | \n", "4 | \n", "5 | \n", "Denmark | \n", "9.22 | \n", "10.00 | \n", "9.29 | \n", "8.33 | \n", "9.38 | \n", "9.12 | \n", "Full democracy | \n", "