{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Dummy dataset" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Find this notebook at `EpyNN/epynnlive/dummy_string/prepare_dataset.ipynb`. \n", "* Regular python code at `EpyNN/epynnlive/dummy_string/prepare_dataset.py`.\n", "\n", "Run the notebook online with [Google Colab](https://colab.research.google.com/github/Synthaze/EpyNN/blob/main/epynnlive/dummy_string/prepare_dataset.ipynb).\n", "\n", "**Level: Beginner**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook is part of the series on preparing data for Neural Network regression with EpyNN.\n", "\n", "In addition to the topic-specific content, it contains several explanations about basics or general concepts in programming that are important in the context.\n", "\n", "Note that elements developed in the following notebooks may not be reviewed herein:\n", "\n", "* [Boolean dataset](../dummy_boolean/prepare_dataset.ipynb)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## What is a string data-type?\n", "\n", "String data type is a form of data which corresponds to sequences of character data." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Before preparing a dummy dataset of sample string features, let's explore some properties of Python built-in data-types." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 1\n", " 1\n", " 1.0\n" ] } ], "source": [ "my_str = '1'\n", "my_int = int(my_str)\n", "my_float = float(my_str)\n", "\n", "print(type(my_str), my_str)\n", "print(type(my_int), my_int)\n", "print(type(my_float), my_float)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that data objects inherit from a class.\n", "\n", "Note that ``my_str`` and ``my_int`` looks the same. Does it mean they really are the same, in a practical perspective?" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "11 - This is called string concatenation\n", "2 - This is the arithmetical addition\n", "can only concatenate str (not \"int\") to str - Concatenation does not exist for int\n", "unsupported operand type(s) for +: 'int' and 'str' - Addition does not exist for str\n" ] } ], "source": [ "print(my_str + my_str, '- This is called string concatenation')\n", "\n", "print(my_int + my_int, '- This is the arithmetical addition')\n", "\n", "try:\n", " print(my_str + my_int)\n", "except TypeError as error:\n", " print(error, '- Concatenation does not exist for int')\n", " \n", "try:\n", " print(my_int + my_str)\n", "except TypeError as error:\n", " print(error, '- Addition does not exist for str')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The conclusion is: **we cannot do maths with the string data type**\n", "\n", "Although this problem will not be fixed at the stage of preparing a dummy dataset with string sample features, we will still extend the notebook to show the principle of it." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Why preparing a dummy dataset with string features?\n", "\n", "In addition to the general interest of dummy dataset explained in [dummy dataset with boolean sample features](../dummy_boolean/prepare_dataset.ipynb#Why-preparing-a-dummy-dataset-with-Boolean-features), we will take the opportunity to review the principle which translates string data into something which can be mathematically processed in the context of a Neural Network. The principle is called *encoding*, and one of the methods is called *one-hot encoding*." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Prepare a set of string sample features and related label" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Imports" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# EpyNN/epynnlive/dummy_string/prepare_dataset.ipynb\n", "# Standard library imports\n", "import random\n", "\n", "# Related third party imports\n", "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Seeding" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "random.seed(1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For reproducibility." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Generate features" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We need to define a function which will generate pseudo-random string features." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def features_string(N_FEATURES=12):\n", " \"\"\"Generate dummy string features.\n", "\n", " :param N_FEATURES: Number of features, defaults to 12.\n", " :type N_FEATURES: int\n", "\n", " :return: random string features of length N_FEATURES.\n", " :rtype: list[str]\n", " \"\"\"\n", " # List of words\n", " WORDS = ['A', 'T', 'G', 'C']\n", "\n", " # Random choice of words for N_FEATURES iterations\n", " features = [random.choice(WORDS) for j in range(N_FEATURES)]\n", "\n", " return features" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The code is commented and quite self-explaining. Let's proceed with a call." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['T', 'A', 'G', 'A', 'C', 'C', 'C', 'C', 'T', 'A', 'C', 'A'] 12\n" ] } ], "source": [ "features = features_string()\n", "print(features, len(features))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Did we get a string, strictly speaking?" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(features))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The answer is no: we got a list. But did we expect to get a string, after all?\n", "\n", "We are talking about sample features which are **each** a string. But because sample features represent a **list of features**, then we got a list.\n", "\n", "Does this list really contain strings? This is important, because if you ignore your data type, you will mess up your day of work." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "T 1 \n", "A 1 \n", "G 1 \n", "A 1 \n", "C 1 \n", "C 1 \n", "C 1 \n", "C 1 \n", "T 1 \n", "A 1 \n", "C 1 \n", "A 1 \n" ] } ], "source": [ "for feature in features:\n", " print(feature, len(feature), type(feature))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Looks good! The list contains elements of type ``str``, rigourously of ``class 'str'``.\n", "\n", "Let's play around to see which relationships between sequential data type ``str`` and ``list``." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Original list of string features of length N_FEATURES\n", " ['T', 'A', 'G', 'A', 'C', 'C', 'C', 'C', 'T', 'A', 'C', 'A']\n", "\n", "Join the original list and get a single string feature of length N_FEATURES\n", " TAGACCCCTACA\n", "\n" ] } ], "source": [ "print('Original list of string features of length N_FEATURES')\n", "print(type(features), features, end='\\n\\n')\n", "\n", "print('Join the original list and get a single string feature of length N_FEATURES')\n", "print(type(''.join(features)), ''.join(features), end='\\n\\n')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's see the classical mistake:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['T', 'A', 'G', 'A', 'C', 'C', 'C', 'C', 'T', 'A', 'C', 'A']\n", "True\n" ] } ], "source": [ "# Because each string feature has length 1, the mistake is not seen\n", "str_feature = ''.join(features)\n", "\n", "print(list(str_feature))\n", "print((list(str_feature) == features))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That's cool, it seems we can reverse the ``join()`` with the ``list()``.\n", "\n", "Really?" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['M', 'y', ' ', 'm', 'i', 's', 't', 'a', 'k', 'e']\n", "False\n" ] } ], "source": [ "# At least one feature has length greater than one\n", "test_features = ['My', ' mistake'] # Features as string with 2 elements\n", "\n", "str_test_features = ''.join(test_features)\n", "\n", "print(list(str_test_features))\n", "print((list(str_test_features) == features))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**When applying list() with a string as argument, it separates all characters in the string**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now that we have seen some traps about the string data type and we have built our function to generate dummy sample features of string data type, we will go ahead with label assignment." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "But best practices first, let's clean the namespace out of the test content." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "del test_features\n", "del str_test_features" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you want to increase the level of complexity in your life, don't follow best practices. Otherwise, do follow them." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Generate label" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As previously mentioned, in this dummy example we have the *a priori* knowledge of the law we want to model.\n", "\n", "Herein, the law which associates samples features and label should be simple, because this is a sort of positive control: if the further regression using our Neural Network cannot model this simple law, it possibly means we did a big mistake." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "def label_features(features):\n", " \"\"\"Prepare label associated with features.\n", "\n", " The dummy law is:\n", "\n", " First and last elements are equal = positive.\n", " First and last elements are NOT equal = negative.\n", "\n", " :param features: random string features of length N_FEATURES.\n", " :type features: list[str]\n", "\n", " :return: Single-digit label with respect to features.\n", " :rtype: int\n", " \"\"\"\n", " # Single-digit positive and negative labels\n", " p_label = 0\n", " n_label = 1\n", "\n", " # Pattern associated with positive label (0)\n", " if features[0] == features[-1]:\n", " label = p_label\n", "\n", " # Other pattern associated with negative label (1)\n", " elif features[0] != features[-1]:\n", " label = n_label\n", "\n", " return label" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The code above is commented and self explaining.\n", "\n", "Let's check the function we made for a few iterations." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 ['C', 'C', 'A', 'C', 'G', 'T', 'A', 'G', 'A', 'A', 'A', 'A']\n", "0 ['C', 'T', 'C', 'A', 'T', 'C', 'C', 'T', 'G', 'T', 'T', 'C']\n", "0 ['G', 'A', 'C', 'A', 'T', 'G', 'A', 'G', 'C', 'T', 'G', 'G']\n", "0 ['C', 'C', 'A', 'C', 'T', 'C', 'C', 'T', 'G', 'G', 'A', 'C']\n", "1 ['A', 'T', 'C', 'G', 'C', 'A', 'C', 'A', 'G', 'C', 'T', 'T']\n" ] } ], "source": [ "for i in range(5):\n", " features = features_string()\n", " label = label_features(features)\n", "\n", " print(label, features)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This seems to be fully consistent with what we expect. No red flag.\n", "\n", "Not that to not make things too complicated, we came to accept that the dummy dataset will be imbalanced in term of label representation. This question is important, and we will have the opportunity to discuss it when feeding the Network with the data.\n", "\n", "This is what I’m talking about." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 22\n", "1 78\n" ] } ], "source": [ "labels = [label_features(features_string()) for i in range(100)]\n", "\n", "print(0, labels.count(0))\n", "print(1, labels.count(1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By the way, we have just performed a *list comprehension* operation which compute ``label_features()`` from features returned by the argument ``features_string()``, for 100 iterations.\n", "\n", "This is a *Pythonic* way to do things." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Prepare dataset" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's go on and write the function which will iterate to generate a set of sample string features and label." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "def prepare_dataset(N_SAMPLES=100):\n", " \"\"\"Prepare a set of dummy string sample features and label.\n", "\n", " :param N_SAMPLES: Number of samples to generate, defaults to 100.\n", " :type N_SAMPLES: int\n", "\n", " :return: Set of sample features.\n", " :rtype: tuple[list[str]]\n", "\n", " :return: Set of single-digit sample label.\n", " :rtype: tuple[int]\n", " \"\"\"\n", " # Initialize X and Y datasets\n", " X_features = []\n", " Y_label = []\n", "\n", " # Iterate over N_SAMPLES\n", " for i in range(N_SAMPLES):\n", "\n", " # Compute random string features\n", " features = features_string()\n", "\n", " # Retrieve label associated with features\n", " label = label_features(features)\n", "\n", " # Append sample features to X_features\n", " X_features.append(features)\n", "\n", " # Append sample label to Y_label\n", " Y_label.append(label)\n", "\n", " # Prepare X-Y pairwise dataset\n", " dataset = list(zip(X_features, Y_label))\n", "\n", " # Shuffle dataset\n", " random.shuffle(dataset)\n", "\n", " # Separate X-Y pairs\n", " X_features, Y_label = zip(*dataset)\n", "\n", " return X_features, Y_label" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that this function is identical to the one discussed in [dummy dataset with boolean sample features](../dummy_boolean/prepare_dataset.ipynb#Why-preparing-a-dummy-dataset-with-Boolean-features) because the variable content with respect to data type is generated by the functions ``features_string()`` and ``label_features()``.\n", "\n", "Let's check the function." ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 ['G', 'A', 'G', 'C', 'G', 'A', 'A', 'G', 'T', 'T', 'A', 'A']\n", "1 ['C', 'C', 'T', 'T', 'A', 'T', 'C', 'T', 'G', 'T', 'C', 'G']\n", "0 ['C', 'T', 'T', 'C', 'C', 'T', 'A', 'A', 'T', 'A', 'A', 'C']\n", "1 ['A', 'A', 'A', 'C', 'C', 'T', 'T', 'T', 'C', 'G', 'T', 'T']\n", "1 ['G', 'A', 'G', 'A', 'C', 'A', 'T', 'T', 'G', 'C', 'A', 'A']\n", "1 ['C', 'A', 'T', 'T', 'A', 'C', 'A', 'C', 'T', 'C', 'G', 'T']\n", "0 ['C', 'A', 'C', 'G', 'C', 'G', 'G', 'C', 'A', 'C', 'A', 'C']\n", "1 ['T', 'T', 'G', 'T', 'G', 'C', 'G', 'A', 'G', 'T', 'C', 'A']\n", "1 ['C', 'C', 'T', 'G', 'A', 'T', 'T', 'C', 'G', 'C', 'C', 'G']\n", "1 ['C', 'T', 'T', 'C', 'A', 'T', 'C', 'T', 'G', 'A', 'A', 'T']\n" ] } ], "source": [ "X_features, Y_label = prepare_dataset(N_SAMPLES=10)\n", "\n", "for sample in zip(X_features, Y_label):\n", " features, label = sample\n", " print(label, features)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We are now done with the code required to generate a dummy dataset of sample string features and associated labels." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## One-hot encoding of string features" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As previously stated in the introductory comments of this notebook, there are things you can do and others you cannot, with respect to the type of variable you are handling." ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "11\n", "\n", "11\n", "2\n" ] } ], "source": [ "print(1 + 1) # Arithmetics\n", "print('1' + '1') # String concatenation\n", "print()\n", "print(str(1) + str(1)) # String concatenation\n", "print(int('1') + int('1')) # Arithmetics" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are things you can do with either integers or strings, and ways you can convert one data type into the other.\n", "\n", "But obviously, you cannot convert a non-digit string to integer data type." ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A\n", "invalid literal for int() with base 10: 'A'\n" ] } ], "source": [ "print('A')\n", "\n", "try:\n", " print(int('A'))\n", "except ValueError as error:\n", " print(error)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Given our features, which are string features in a list, how can we proceed with arithmetic on them?\n", "\n", "In EpyNN this is made automatically at a later stage. See [Embedding (Input)](https://epynn.net/Embedding.html) for full documentation.\n", "\n", "For now, we will do it by hand. We have:" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['C', 'T', 'T', 'C', 'A', 'T', 'C', 'T', 'G', 'A', 'A', 'T']\n", "['T', 'C', 'G', 'A']\n" ] } ], "source": [ "print(features)\n", "print(list(set(features)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A list of string features, composed of four possible characters - or single-character words - for a length of N_FEATURES.\n", "\n", "Since our sample of features contains all four characters, it is fine to proceed with encoding." ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "words = list(set(features))\n", "\n", "words_to_idx = {word: idx for idx, word in enumerate(words)}\n", "# The just above line is equivalent to\n", "# words_to_idx = {words[idx]:idx for idx in range(len(words))}\n", "\n", "idx_to_words = {idx: words for words, idx in words_to_idx.items()}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Check it." ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['T', 'C', 'G', 'A']\n", "{'T': 0, 'C': 1, 'G': 2, 'A': 3}\n", "{0: 'T', 1: 'C', 2: 'G', 3: 'A'}\n" ] } ], "source": [ "print(words)\n", "print(words_to_idx)\n", "print(idx_to_words)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You may have gotten where we are heading now.\n", "\n", "By the way, we instantiated dictionary objects here, ``{}`` and such data type works like ``{key: value}``.\n", "\n", "Let's proceed." ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "T 0\n", "C 1\n", "G 2\n", "A 3\n" ] } ], "source": [ "for word in words:\n", " print(word, words_to_idx[word])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Could this be the conversion table? Not really. Because numbers are quantities. Every character should be represented by the same quantity: one." ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "T 0 [0. 0. 0. 0.]\n", "C 1 [0. 0. 0. 0.]\n", "G 2 [0. 0. 0. 0.]\n", "A 3 [0. 0. 0. 0.]\n" ] } ], "source": [ "for word in words:\n", " print(word, words_to_idx[word], np.zeros(len(words)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Almost there." ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "T 0 [1. 0. 0. 0.]\n", "C 1 [0. 1. 0. 0.]\n", "G 2 [0. 0. 1. 0.]\n", "A 3 [0. 0. 0. 1.]\n" ] } ], "source": [ "for word in words:\n", " encoded_word = np.zeros(len(words))\n", " encoded_word[words_to_idx[word]] = 1\n", " \n", " print(word, words_to_idx[word], encoded_word)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is **One-hot encoding**.\n", "\n", "With string features." ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C 1 [0. 1. 0. 0.]\n", "T 0 [1. 0. 0. 0.]\n", "T 0 [1. 0. 0. 0.]\n", "C 1 [0. 1. 0. 0.]\n", "A 3 [0. 0. 0. 1.]\n", "T 0 [1. 0. 0. 0.]\n", "C 1 [0. 1. 0. 0.]\n", "T 0 [1. 0. 0. 0.]\n", "G 2 [0. 0. 1. 0.]\n", "A 3 [0. 0. 0. 1.]\n", "A 3 [0. 0. 0. 1.]\n", "T 0 [1. 0. 0. 0.]\n" ] } ], "source": [ "for feature in features:\n", " encoded_feature = np.zeros(len(words))\n", " encoded_feature[words_to_idx[feature]] = 1\n", " \n", " print(feature, words_to_idx[feature], encoded_feature)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's make a proper NumPy array from this." ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0 1 0 0]\n", " [1 0 0 0]\n", " [1 0 0 0]\n", " [0 1 0 0]\n", " [0 0 0 1]\n", " [1 0 0 0]\n", " [0 1 0 0]\n", " [1 0 0 0]\n", " [0 0 1 0]\n", " [0 0 0 1]\n", " [0 0 0 1]\n", " [1 0 0 0]]\n", "(12, 4)\n" ] } ], "source": [ "# Just to show the principle of nested list comprehension. Not necessarily a best idea, but still :)\n", "encoded_features = np.array([[1 if idx == words_to_idx[feature] else 0 for idx in range(len(words))] for feature in features])\n", "\n", "print(encoded_features)\n", "print(encoded_features.shape)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Decoding one-hot encoded data." ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['C', 'T', 'T', 'C', 'A', 'T', 'C', 'T', 'G', 'A', 'A', 'T']\n", "['C', 'T', 'T', 'C', 'A', 'T', 'C', 'T', 'G', 'A', 'A', 'T']\n" ] } ], "source": [ "print(features)\n", "print([idx_to_words[np.argmax(encoded_feature)] for encoded_feature in encoded_features])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And we will finish on the purpose of this ``np.argmax()`` function." ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "2\n", "\n", "2 with respect to 3\n", "[1 0 0]\n", "\n", "3 with respect to 2\n", "[2 0]\n" ] } ], "source": [ "test = np.array([10, 0, 0])\n", "print(np.argmax(test))\n", "\n", "test = np.array([0, 0, 10])\n", "print(np.argmax(test), end='\\n\\n')\n", "\n", "test = np.array(\n", " [\n", " [0, 0, 10],\n", " [10, 0, 0]\n", " ]\n", ")\n", "\n", "print(test.shape[0], 'with respect to', test.shape[1])\n", "print(np.argmax(test, axis=0), end='\\n\\n')\n", "\n", "print(test.shape[1], 'with respect to', test.shape[0])\n", "print(np.argmax(test, axis=1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In words, ``np.argmax()`` returns the index at which is located the maximal value. It returns the argument which return the maximum when provided as index of the array.\n", "\n", "Built-in Python on **list**." ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n" ] } ], "source": [ "lst = [0, 0, 10]\n", "\n", "print(lst.index(max(lst)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We are now done with the code required to generate a dummy dataset of sample string features and associated labels.\n", "\n", "And we extensively reviewed the practice of one-hot encoding, as well as some insights into Python data types and NumPy array manipulation!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Live examples" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The function ``prepare_dataset()`` presented herein is used in the following live examples:\n", "\n", "* Notebook at`EpyNN/epynnlive/dummy_string/train.ipynb` or following [this link](train.ipynb). \n", "* Regular python code at `EpyNN/epynnlive/dummy_string/train.py`." ] } ], "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.2" } }, "nbformat": 4, "nbformat_minor": 4 }