Skip to content

Instantly share code, notes, and snippets.

@mosdevly
Last active October 9, 2018 22:30
Show Gist options
  • Select an option

  • Save mosdevly/676b4097385fd964c06770d6b7b11eb6 to your computer and use it in GitHub Desktop.

Select an option

Save mosdevly/676b4097385fd964c06770d6b7b11eb6 to your computer and use it in GitHub Desktop.

Revisions

  1. Proto revised this gist Oct 9, 2018. 1 changed file with 8 additions and 84 deletions.
    92 changes: 8 additions & 84 deletions markov_chain.ipynb
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 1,
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": [
    @@ -27,61 +27,9 @@
    },
    {
    "cell_type": "code",
    "execution_count": 2,
    "execution_count": null,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "['Would',\n",
    " 'you',\n",
    " 'could',\n",
    " 'you',\n",
    " 'in',\n",
    " 'a',\n",
    " 'house?',\n",
    " 'Would',\n",
    " 'you',\n",
    " 'could',\n",
    " 'you',\n",
    " 'with',\n",
    " 'a',\n",
    " 'mouse?',\n",
    " 'Would',\n",
    " 'you',\n",
    " 'could',\n",
    " 'you',\n",
    " 'in',\n",
    " 'a',\n",
    " 'box?',\n",
    " 'Would',\n",
    " 'you',\n",
    " 'could',\n",
    " 'you',\n",
    " 'with',\n",
    " 'a',\n",
    " 'fox?',\n",
    " 'Would',\n",
    " 'you',\n",
    " 'like',\n",
    " 'green',\n",
    " 'eggs',\n",
    " 'and',\n",
    " 'ham?',\n",
    " 'Would',\n",
    " 'you',\n",
    " 'like',\n",
    " 'them,',\n",
    " 'Sam',\n",
    " 'I',\n",
    " 'am?']"
    ]
    },
    "execution_count": 2,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "outputs": [],
    "source": [
    "# Let's split the book into a list of words:\n",
    "words = book.split()\n",
    @@ -90,17 +38,9 @@
    },
    {
    "cell_type": "code",
    "execution_count": 4,
    "execution_count": null,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "{('Would', 'you'): ['could', 'could', 'could', 'could', 'like', 'like'], ('you', 'could'): ['you', 'you', 'you', 'you'], ('could', 'you'): ['in', 'with', 'in', 'with'], ('you', 'in'): ['a', 'a'], ('in', 'a'): ['house?', 'box?'], ('a', 'house?'): ['Would'], ('house?', 'Would'): ['you'], ('you', 'with'): ['a', 'a'], ('with', 'a'): ['mouse?', 'fox?'], ('a', 'mouse?'): ['Would'], ('mouse?', 'Would'): ['you'], ('a', 'box?'): ['Would'], ('box?', 'Would'): ['you'], ('a', 'fox?'): ['Would'], ('fox?', 'Would'): ['you'], ('you', 'like'): ['green', 'them,'], ('like', 'green'): ['eggs'], ('green', 'eggs'): ['and'], ('eggs', 'and'): ['ham?'], ('and', 'ham?'): ['Would'], ('ham?', 'Would'): ['you'], ('like', 'them,'): ['Sam'], ('them,', 'Sam'): ['I'], ('Sam', 'I'): ['am?']}\n"
    ]
    }
    ],
    "outputs": [],
    "source": [
    "def make_chains(text_string):\n",
    " chains = {}\n",
    @@ -120,30 +60,14 @@
    " # Finally, add the next word to our keys\n",
    " chains[key].append(value)\n",
    "\n",
    " return chains\n",
    "\n",
    "chains = make_chains(words)\n",
    "print(chains)"
    " return chains\n"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 5,
    "execution_count": null,
    "metadata": {},
    "outputs": [
    {
    "ename": "KeyError",
    "evalue": "('I', 'am?')",
    "traceback": [
    "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
    "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
    "\u001b[0;32m<ipython-input-5-4fbf1fc9582c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 24\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 25\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 26\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmake_text\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mchains\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
    "\u001b[0;32m<ipython-input-5-4fbf1fc9582c>\u001b[0m in \u001b[0;36mmake_text\u001b[0;34m(chains)\u001b[0m\n\u001b[1;32m 18\u001b[0m \u001b[0mtext\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mword\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0;31m# Get a new rand word from the new key\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 20\u001b[0;31m \u001b[0mword\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mchoice\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mchains\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 21\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 22\u001b[0m \u001b[0;31m# Finally return the new text!\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
    "\u001b[0;31mKeyError\u001b[0m: ('I', 'am?')"
    ],
    "output_type": "error"
    }
    ],
    "outputs": [],
    "source": [
    "def make_text(chains):\n",
    " \"\"\"Return text from chains.\"\"\"\n",
  2. Proto revised this gist Oct 9, 2018. No changes.
  3. Proto revised this gist Oct 9, 2018. 1 changed file with 73 additions and 60 deletions.
    133 changes: 73 additions & 60 deletions markov_chain.ipynb
    Original file line number Diff line number Diff line change
    @@ -11,10 +11,12 @@
    },
    {
    "cell_type": "code",
    "execution_count": 10,
    "execution_count": 1,
    "metadata": {},
    "outputs": [],
    "source": [
    "from random import choice\n",
    "\n",
    "book = \"\"\"Would you could you in a house?\n",
    "Would you could you with a mouse?\n",
    "Would you could you in a box?\n",
    @@ -25,7 +27,7 @@
    },
    {
    "cell_type": "code",
    "execution_count": 11,
    "execution_count": 2,
    "metadata": {},
    "outputs": [
    {
    @@ -75,7 +77,7 @@
    " 'am?']"
    ]
    },
    "execution_count": 11,
    "execution_count": 2,
    "metadata": {},
    "output_type": "execute_result"
    }
    @@ -88,76 +90,87 @@
    },
    {
    "cell_type": "code",
    "execution_count": 12,
    "metadata": {},
    "outputs": [],
    "source": [
    "# Create a dictionary named chains.\n",
    "chains = {}"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 13,
    "execution_count": 4,
    "metadata": {},
    "outputs": [],
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "{('Would', 'you'): ['could', 'could', 'could', 'could', 'like', 'like'], ('you', 'could'): ['you', 'you', 'you', 'you'], ('could', 'you'): ['in', 'with', 'in', 'with'], ('you', 'in'): ['a', 'a'], ('in', 'a'): ['house?', 'box?'], ('a', 'house?'): ['Would'], ('house?', 'Would'): ['you'], ('you', 'with'): ['a', 'a'], ('with', 'a'): ['mouse?', 'fox?'], ('a', 'mouse?'): ['Would'], ('mouse?', 'Would'): ['you'], ('a', 'box?'): ['Would'], ('box?', 'Would'): ['you'], ('a', 'fox?'): ['Would'], ('fox?', 'Would'): ['you'], ('you', 'like'): ['green', 'them,'], ('like', 'green'): ['eggs'], ('green', 'eggs'): ['and'], ('eggs', 'and'): ['ham?'], ('and', 'ham?'): ['Would'], ('ham?', 'Would'): ['you'], ('like', 'them,'): ['Sam'], ('them,', 'Sam'): ['I'], ('Sam', 'I'): ['am?']}\n"
    ]
    }
    ],
    "source": [
    "# Loop over the words:\n",
    "for n in range(len(words) - 2):\n",
    " # Get the first and second words and put them into a tuple. They will act as keys in the chain:\n",
    " key = (words[n], words[n+1],)\n",
    " value = words[n+2]\n",
    " \n",
    " # See if the key is already there\n",
    " if key not in chains.keys():\n",
    " # If it's not, initialize it's value to an empty list so we can insert words into it later\n",
    " chains[key] = []\n",
    "def make_chains(text_string):\n",
    " chains = {}\n",
    " words = text_string\n",
    " \n",
    " # Finally, add the next word to our keys\n",
    " chains[key].append(value)"
    " # Loop over the words:\n",
    " for n in range(len(text_string) - 2):\n",
    " # Get the first and second words and put them into a tuple. They will act as keys in the chain:\n",
    " key = (words[n], words[n+1],)\n",
    " value = words[n+2]\n",
    "\n",
    " # See if the key is already there\n",
    " if key not in chains.keys():\n",
    " # If it's not, initialize it's value to an empty list so we can insert words into it later\n",
    " chains[key] = []\n",
    "\n",
    " # Finally, add the next word to our keys\n",
    " chains[key].append(value)\n",
    "\n",
    " return chains\n",
    "\n",
    "chains = make_chains(words)\n",
    "print(chains)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 14,
    "execution_count": 5,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "{('Sam', 'I'): ['am?'],\n",
    " ('Would', 'you'): ['could', 'could', 'could', 'could', 'like', 'like'],\n",
    " ('a', 'box?'): ['Would'],\n",
    " ('a', 'fox?'): ['Would'],\n",
    " ('a', 'house?'): ['Would'],\n",
    " ('a', 'mouse?'): ['Would'],\n",
    " ('and', 'ham?'): ['Would'],\n",
    " ('box?', 'Would'): ['you'],\n",
    " ('could', 'you'): ['in', 'with', 'in', 'with'],\n",
    " ('eggs', 'and'): ['ham?'],\n",
    " ('fox?', 'Would'): ['you'],\n",
    " ('green', 'eggs'): ['and'],\n",
    " ('ham?', 'Would'): ['you'],\n",
    " ('house?', 'Would'): ['you'],\n",
    " ('in', 'a'): ['house?', 'box?'],\n",
    " ('like', 'green'): ['eggs'],\n",
    " ('like', 'them,'): ['Sam'],\n",
    " ('mouse?', 'Would'): ['you'],\n",
    " ('them,', 'Sam'): ['I'],\n",
    " ('with', 'a'): ['mouse?', 'fox?'],\n",
    " ('you', 'could'): ['you', 'you', 'you', 'you'],\n",
    " ('you', 'in'): ['a', 'a'],\n",
    " ('you', 'like'): ['green', 'them,'],\n",
    " ('you', 'with'): ['a', 'a']}"
    ]
    },
    "execution_count": 14,
    "metadata": {},
    "output_type": "execute_result"
    "ename": "KeyError",
    "evalue": "('I', 'am?')",
    "traceback": [
    "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
    "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
    "\u001b[0;32m<ipython-input-5-4fbf1fc9582c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 24\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 25\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 26\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmake_text\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mchains\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
    "\u001b[0;32m<ipython-input-5-4fbf1fc9582c>\u001b[0m in \u001b[0;36mmake_text\u001b[0;34m(chains)\u001b[0m\n\u001b[1;32m 18\u001b[0m \u001b[0mtext\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mword\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0;31m# Get a new rand word from the new key\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 20\u001b[0;31m \u001b[0mword\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mchoice\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mchains\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 21\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 22\u001b[0m \u001b[0;31m# Finally return the new text!\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
    "\u001b[0;31mKeyError\u001b[0m: ('I', 'am?')"
    ],
    "output_type": "error"
    }
    ],
    "source": [
    "chains"
    "def make_text(chains):\n",
    " \"\"\"Return text from chains.\"\"\"\n",
    "\n",
    " # Choose a random key from the chain to start\n",
    " key = choice(list(chains.keys()))\n",
    " \n",
    " # Create our new text that we can add to and return later\n",
    " text = [key[0], key[1]]\n",
    "\n",
    " # Choose a random value from the key in our chain\n",
    " word = choice(chains[key])\n",
    "\n",
    " # This loop will try to visit every key.\n",
    " while word is not None:\n",
    " # Form the next key\n",
    " key = (key[1], word)\n",
    " # Add the current word to the list\n",
    " text.append(word)\n",
    " # Get a new rand word from the new key\n",
    " word = choice(chains[key])\n",
    "\n",
    " # Finally return the new text!\n",
    " return \" \".join(text)\n",
    "\n",
    "\n",
    "print(make_text(chains))\n"
    ]
    },
    {
  4. Proto created this gist Oct 9, 2018.
    192 changes: 192 additions & 0 deletions markov_chain.ipynb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,192 @@
    {
    "cells": [
    {
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "# Markov Chains\n",
    "\n",
    "Play with this step by step walk through of how it works. Let's make a tiny chain."
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 10,
    "metadata": {},
    "outputs": [],
    "source": [
    "book = \"\"\"Would you could you in a house?\n",
    "Would you could you with a mouse?\n",
    "Would you could you in a box?\n",
    "Would you could you with a fox?\n",
    "Would you like green eggs and ham?\n",
    "Would you like them, Sam I am?\"\"\""
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 11,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "['Would',\n",
    " 'you',\n",
    " 'could',\n",
    " 'you',\n",
    " 'in',\n",
    " 'a',\n",
    " 'house?',\n",
    " 'Would',\n",
    " 'you',\n",
    " 'could',\n",
    " 'you',\n",
    " 'with',\n",
    " 'a',\n",
    " 'mouse?',\n",
    " 'Would',\n",
    " 'you',\n",
    " 'could',\n",
    " 'you',\n",
    " 'in',\n",
    " 'a',\n",
    " 'box?',\n",
    " 'Would',\n",
    " 'you',\n",
    " 'could',\n",
    " 'you',\n",
    " 'with',\n",
    " 'a',\n",
    " 'fox?',\n",
    " 'Would',\n",
    " 'you',\n",
    " 'like',\n",
    " 'green',\n",
    " 'eggs',\n",
    " 'and',\n",
    " 'ham?',\n",
    " 'Would',\n",
    " 'you',\n",
    " 'like',\n",
    " 'them,',\n",
    " 'Sam',\n",
    " 'I',\n",
    " 'am?']"
    ]
    },
    "execution_count": 11,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "# Let's split the book into a list of words:\n",
    "words = book.split()\n",
    "words"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 12,
    "metadata": {},
    "outputs": [],
    "source": [
    "# Create a dictionary named chains.\n",
    "chains = {}"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 13,
    "metadata": {},
    "outputs": [],
    "source": [
    "# Loop over the words:\n",
    "for n in range(len(words) - 2):\n",
    " # Get the first and second words and put them into a tuple. They will act as keys in the chain:\n",
    " key = (words[n], words[n+1],)\n",
    " value = words[n+2]\n",
    " \n",
    " # See if the key is already there\n",
    " if key not in chains.keys():\n",
    " # If it's not, initialize it's value to an empty list so we can insert words into it later\n",
    " chains[key] = []\n",
    " \n",
    " # Finally, add the next word to our keys\n",
    " chains[key].append(value)"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 14,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "{('Sam', 'I'): ['am?'],\n",
    " ('Would', 'you'): ['could', 'could', 'could', 'could', 'like', 'like'],\n",
    " ('a', 'box?'): ['Would'],\n",
    " ('a', 'fox?'): ['Would'],\n",
    " ('a', 'house?'): ['Would'],\n",
    " ('a', 'mouse?'): ['Would'],\n",
    " ('and', 'ham?'): ['Would'],\n",
    " ('box?', 'Would'): ['you'],\n",
    " ('could', 'you'): ['in', 'with', 'in', 'with'],\n",
    " ('eggs', 'and'): ['ham?'],\n",
    " ('fox?', 'Would'): ['you'],\n",
    " ('green', 'eggs'): ['and'],\n",
    " ('ham?', 'Would'): ['you'],\n",
    " ('house?', 'Would'): ['you'],\n",
    " ('in', 'a'): ['house?', 'box?'],\n",
    " ('like', 'green'): ['eggs'],\n",
    " ('like', 'them,'): ['Sam'],\n",
    " ('mouse?', 'Would'): ['you'],\n",
    " ('them,', 'Sam'): ['I'],\n",
    " ('with', 'a'): ['mouse?', 'fox?'],\n",
    " ('you', 'could'): ['you', 'you', 'you', 'you'],\n",
    " ('you', 'in'): ['a', 'a'],\n",
    " ('you', 'like'): ['green', 'them,'],\n",
    " ('you', 'with'): ['a', 'a']}"
    ]
    },
    "execution_count": 14,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "chains"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": null,
    "metadata": {},
    "outputs": [],
    "source": []
    }
    ],
    "metadata": {
    "kernelspec": {
    "display_name": "Python 3",
    "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.7.0"
    }
    },
    "nbformat": 4,
    "nbformat_minor": 2
    }