diff --git a/README.md b/README.md index 91efcde94..64b7c1612 100644 --- a/README.md +++ b/README.md @@ -140,7 +140,7 @@ Here is a table of algorithms, the figure, name of the algorithm in the book and | 18.34 | AdaBoost | `AdaBoost` | [`learning.py`][learning] | Done | Included | | 19.2 | Current-Best-Learning | `current_best_learning` | [`knowledge.py`](knowledge.py) | Done | Included | | 19.3 | Version-Space-Learning | `version_space_learning` | [`knowledge.py`](knowledge.py) | Done | Included | -| 19.8 | Minimal-Consistent-Det | `minimal_consistent_det` | [`knowledge.py`](knowledge.py) | Done | | +| 19.8 | Minimal-Consistent-Det | `minimal_consistent_det` | [`knowledge.py`](knowledge.py) | Done | Included | | 19.12 | FOIL | `FOIL_container` | [`knowledge.py`](knowledge.py) | Done | | | 21.2 | Passive-ADP-Agent | `PassiveADPAgent` | [`rl.py`][rl] | Done | Included | | 21.4 | Passive-TD-Agent | `PassiveTDAgent` | [`rl.py`][rl] | Done | Included | diff --git a/knowledge.ipynb b/knowledge.ipynb index 2ffb20362..c21de646c 100644 --- a/knowledge.ipynb +++ b/knowledge.ipynb @@ -13,10 +13,8 @@ }, { "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": true - }, + "execution_count": 50, + "metadata": {}, "outputs": [], "source": [ "from knowledge import *\n", @@ -96,7 +94,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 51, "metadata": {}, "outputs": [ { @@ -126,7 +124,7 @@ "" ] }, - "execution_count": 2, + "execution_count": 51, "metadata": {}, "output_type": "execute_result" } @@ -150,9 +148,192 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 52, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "\n", + "\n", + "\n", + "\n", + " \n", + " \n", + " \n", + "\n", + "\n", + "

\n", + "\n", + "
def current_best_learning(examples, h, examples_so_far=None):\n",
+       "    """ [Figure 19.2]\n",
+       "    The hypothesis is a list of dictionaries, with each dictionary representing\n",
+       "    a disjunction."""\n",
+       "    if not examples:\n",
+       "        return h\n",
+       "\n",
+       "    examples_so_far = examples_so_far or []\n",
+       "    e = examples[0]\n",
+       "    if is_consistent(e, h):\n",
+       "        return current_best_learning(examples[1:], h, examples_so_far + [e])\n",
+       "    elif false_positive(e, h):\n",
+       "        for h2 in specializations(examples_so_far + [e], h):\n",
+       "            h3 = current_best_learning(examples[1:], h2, examples_so_far + [e])\n",
+       "            if h3 != 'FAIL':\n",
+       "                return h3\n",
+       "    elif false_negative(e, h):\n",
+       "        for h2 in generalizations(examples_so_far + [e], h):\n",
+       "            h3 = current_best_learning(examples[1:], h2, examples_so_far + [e])\n",
+       "            if h3 != 'FAIL':\n",
+       "                return h3\n",
+       "\n",
+       "    return 'FAIL'\n",
+       "\n",
+       "\n",
+       "def specializations(examples_so_far, h):\n",
+       "    """Specialize the hypothesis by adding AND operations to the disjunctions"""\n",
+       "    hypotheses = []\n",
+       "\n",
+       "    for i, disj in enumerate(h):\n",
+       "        for e in examples_so_far:\n",
+       "            for k, v in e.items():\n",
+       "                if k in disj or k == 'GOAL':\n",
+       "                    continue\n",
+       "\n",
+       "                h2 = h[i].copy()\n",
+       "                h2[k] = '!' + v\n",
+       "                h3 = h.copy()\n",
+       "                h3[i] = h2\n",
+       "                if check_all_consistency(examples_so_far, h3):\n",
+       "                    hypotheses.append(h3)\n",
+       "\n",
+       "    shuffle(hypotheses)\n",
+       "    return hypotheses\n",
+       "\n",
+       "\n",
+       "def generalizations(examples_so_far, h):\n",
+       "    """Generalize the hypothesis. First delete operations\n",
+       "    (including disjunctions) from the hypothesis. Then, add OR operations."""\n",
+       "    hypotheses = []\n",
+       "\n",
+       "    # Delete disjunctions\n",
+       "    disj_powerset = powerset(range(len(h)))\n",
+       "    for disjs in disj_powerset:\n",
+       "        h2 = h.copy()\n",
+       "        for d in reversed(list(disjs)):\n",
+       "            del h2[d]\n",
+       "\n",
+       "        if check_all_consistency(examples_so_far, h2):\n",
+       "            hypotheses += h2\n",
+       "\n",
+       "    # Delete AND operations in disjunctions\n",
+       "    for i, disj in enumerate(h):\n",
+       "        a_powerset = powerset(disj.keys())\n",
+       "        for attrs in a_powerset:\n",
+       "            h2 = h[i].copy()\n",
+       "            for a in attrs:\n",
+       "                del h2[a]\n",
+       "\n",
+       "            if check_all_consistency(examples_so_far, [h2]):\n",
+       "                h3 = h.copy()\n",
+       "                h3[i] = h2.copy()\n",
+       "                hypotheses += h3\n",
+       "\n",
+       "    # Add OR operations\n",
+       "    if hypotheses == [] or hypotheses == [{}]:\n",
+       "        hypotheses = add_or(examples_so_far, h)\n",
+       "    else:\n",
+       "        hypotheses.extend(add_or(examples_so_far, h))\n",
+       "\n",
+       "    shuffle(hypotheses)\n",
+       "    return hypotheses\n",
+       "
\n", + "\n", + "\n" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], "source": [ "psource(current_best_learning, specializations, generalizations)" ] @@ -195,10 +376,8 @@ }, { "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": true - }, + "execution_count": 53, + "metadata": {}, "outputs": [], "source": [ "animals_umbrellas = [\n", @@ -221,7 +400,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 54, "metadata": {}, "outputs": [ { @@ -254,7 +433,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 55, "metadata": {}, "outputs": [ { @@ -287,14 +466,14 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 56, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "[{'Species': 'Cat', 'Rain': '!No'}, {'Coat': 'Yes', 'Rain': 'Yes'}, {'Coat': 'Yes'}]\n" + "[{'Species': 'Cat', 'Rain': '!No'}, {'Rain': 'Yes', 'Coat': '!No'}, {'Rain': 'No', 'Coat': 'Yes'}]\n" ] } ], @@ -340,10 +519,8 @@ }, { "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": true - }, + "execution_count": 28, + "metadata": {}, "outputs": [], "source": [ "def r_example(Alt, Bar, Fri, Hun, Pat, Price, Rain, Res, Type, Est, GOAL):\n", @@ -363,10 +540,8 @@ }, { "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": true - }, + "execution_count": 29, + "metadata": {}, "outputs": [], "source": [ "restaurant = [\n", @@ -394,7 +569,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 30, "metadata": {}, "outputs": [ { @@ -432,14 +607,14 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "[{'Res': '!No', 'Fri': '!Yes', 'Alt': 'Yes'}, {'Bar': 'Yes', 'Fri': 'No', 'Rain': 'No', 'Hun': 'No'}, {'Bar': 'No', 'Price': '$', 'Fri': 'Yes'}, {'Res': 'Yes', 'Price': '$$', 'Rain': 'Yes', 'Alt': 'No', 'Est': '0-10', 'Fri': 'No', 'Hun': 'Yes', 'Bar': 'Yes'}, {'Fri': 'No', 'Pat': 'Some', 'Price': '$$', 'Rain': 'Yes', 'Hun': 'Yes'}, {'Est': '30-60', 'Res': 'No', 'Price': '$', 'Fri': 'Yes', 'Hun': 'Yes'}]\n" + "[{'Alt': 'Yes', 'Type': '!Thai', 'Hun': '!No', 'Pat': '!Full'}, {'Alt': 'No', 'Bar': 'Yes', 'Hun': 'No', 'Price': '$', 'Rain': 'No', 'Res': 'No'}, {'Pat': 'Full', 'Price': '$', 'Rain': 'Yes', 'Type': '!Burger'}, {'Price': '$$', 'Type': 'Italian'}, {'Bar': 'No', 'Hun': 'Yes', 'Pat': 'Some', 'Price': '$$', 'Rain': 'Yes', 'Res': 'Yes', 'Type': 'Thai', 'Est': '0-10'}, {'Bar': 'Yes', 'Fri': 'Yes', 'Hun': 'Yes', 'Pat': 'Full', 'Rain': 'No', 'Res': 'No', 'Type': 'Burger'}]\n" ] } ], @@ -476,7 +651,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 32, "metadata": {}, "outputs": [ { @@ -502,7 +677,7 @@ "" ] }, - "execution_count": 3, + "execution_count": 32, "metadata": {}, "output_type": "execute_result" } @@ -528,27 +703,413 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 33, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "\n", + "\n", + "\n", + "\n", + " \n", + " \n", + " \n", + "\n", + "\n", + "

\n", + "\n", + "
def version_space_learning(examples):\n",
+       "    """ [Figure 19.3]\n",
+       "    The version space is a list of hypotheses, which in turn are a list\n",
+       "    of dictionaries/disjunctions."""\n",
+       "    V = all_hypotheses(examples)\n",
+       "    for e in examples:\n",
+       "        if V:\n",
+       "            V = version_space_update(V, e)\n",
+       "\n",
+       "    return V\n",
+       "\n",
+       "\n",
+       "def version_space_update(V, e):\n",
+       "    return [h for h in V if is_consistent(e, h)]\n",
+       "
\n", + "\n", + "\n" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], "source": [ "psource(version_space_learning, version_space_update)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 34, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "\n", + "\n", + "\n", + "\n", + " \n", + " \n", + " \n", + "\n", + "\n", + "

\n", + "\n", + "
def all_hypotheses(examples):\n",
+       "    """Build a list of all the possible hypotheses"""\n",
+       "    values = values_table(examples)\n",
+       "    h_powerset = powerset(values.keys())\n",
+       "    hypotheses = []\n",
+       "    for s in h_powerset:\n",
+       "        hypotheses.extend(build_attr_combinations(s, values))\n",
+       "\n",
+       "    hypotheses.extend(build_h_combinations(hypotheses))\n",
+       "\n",
+       "    return hypotheses\n",
+       "\n",
+       "\n",
+       "def values_table(examples):\n",
+       "    """Build a table with all the possible values for each attribute.\n",
+       "    Returns a dictionary with keys the attribute names and values a list\n",
+       "    with the possible values for the corresponding attribute."""\n",
+       "    values = defaultdict(lambda: [])\n",
+       "    for e in examples:\n",
+       "        for k, v in e.items():\n",
+       "            if k == 'GOAL':\n",
+       "                continue\n",
+       "\n",
+       "            mod = '!'\n",
+       "            if e['GOAL']:\n",
+       "                mod = ''\n",
+       "\n",
+       "            if mod + v not in values[k]:\n",
+       "                values[k].append(mod + v)\n",
+       "\n",
+       "    values = dict(values)\n",
+       "    return values\n",
+       "
\n", + "\n", + "\n" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], "source": [ "psource(all_hypotheses, values_table)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 35, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "\n", + "\n", + "\n", + "\n", + " \n", + " \n", + " \n", + "\n", + "\n", + "

\n", + "\n", + "
def build_attr_combinations(s, values):\n",
+       "    """Given a set of attributes, builds all the combinations of values.\n",
+       "    If the set holds more than one attribute, recursively builds the\n",
+       "    combinations."""\n",
+       "    if len(s) == 1:\n",
+       "        # s holds just one attribute, return its list of values\n",
+       "        k = values[s[0]]\n",
+       "        h = [[{s[0]: v}] for v in values[s[0]]]\n",
+       "        return h\n",
+       "\n",
+       "    h = []\n",
+       "    for i, a in enumerate(s):\n",
+       "        rest = build_attr_combinations(s[i+1:], values)\n",
+       "        for v in values[a]:\n",
+       "            o = {a: v}\n",
+       "            for r in rest:\n",
+       "                t = o.copy()\n",
+       "                for d in r:\n",
+       "                    t.update(d)\n",
+       "                h.append([t])\n",
+       "\n",
+       "    return h\n",
+       "\n",
+       "\n",
+       "def build_h_combinations(hypotheses):\n",
+       "    """Given a set of hypotheses, builds and returns all the combinations of the\n",
+       "    hypotheses."""\n",
+       "    h = []\n",
+       "    h_powerset = powerset(range(len(hypotheses)))\n",
+       "\n",
+       "    for s in h_powerset:\n",
+       "        t = []\n",
+       "        for i in s:\n",
+       "            t.extend(hypotheses[i])\n",
+       "        h.append(t)\n",
+       "\n",
+       "    return h\n",
+       "
\n", + "\n", + "\n" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], "source": [ "psource(build_attr_combinations, build_h_combinations)" ] @@ -564,10 +1125,8 @@ }, { "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": true - }, + "execution_count": 36, + "metadata": {}, "outputs": [], "source": [ "party = [\n", @@ -586,7 +1145,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 37, "metadata": {}, "outputs": [ { @@ -620,7 +1179,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 38, "metadata": {}, "outputs": [ { @@ -651,6 +1210,426 @@ "\n", "Our initial prediction is indeed in the set of hypotheses. Also, the two other random hypotheses we got are consistent with the examples (since they both include the \"Pizza is available\" disjunction)." ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Minimal Consistent Determination" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This algorithm is based on a straightforward attempt to find the simplest determination consistent with the observations. A determinaton P > Q says that if any examples match on P, then they must also match on Q. A determination is therefore consistent with a set of examples if every pair that matches on the predicates on the left-hand side also matches on the goal predicate." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Pseudocode" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "Lets look at the pseudocode for this algorithm" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [ + { + "data": { + "text/markdown": [ + "### AIMA3e\n", + "__function__ Minimal-Consistent-Det(_E_, _A_) __returns__ a set of attributes \n", + " __inputs__: _E_, a set of examples \n", + "     _A_, a set of attributes, of size _n_ \n", + "\n", + " __for__ _i_ = 0 __to__ _n_ __do__ \n", + "   __for each__ subset _Ai_ of _A_ of size _i_ __do__ \n", + "     __if__ Consistent-Det?(_Ai_, _E_) __then return__ _Ai_ \n", + "\n", + "---\n", + "__function__ Consistent-Det?(_A_, _E_) __returns__ a truth value \n", + " __inputs__: _A_, a set of attributes \n", + "     _E_, a set of examples \n", + " __local variables__: _H_, a hash table \n", + "\n", + " __for each__ example _e_ __in__ _E_ __do__ \n", + "   __if__ some example in _H_ has the same values as _e_ for the attributes _A_ \n", + "    but a different classification __then return__ _false_ \n", + "   store the class of _e_ in_H_, indexed by the values for attributes _A_ of the example _e_ \n", + " __return__ _true_ \n", + "\n", + "---\n", + "__Figure ??__ An algorithm for finding a minimal consistent determination." + ], + "text/plain": [ + "" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pseudocode('Minimal-Consistent-Det')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You can read the code for the above algorithm by running the cells below:" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "\n", + "\n", + "\n", + "\n", + " \n", + " \n", + " \n", + "\n", + "\n", + "

\n", + "\n", + "
def minimal_consistent_det(E, A):\n",
+       "    """Return a minimal set of attributes which give consistent determination"""\n",
+       "    n = len(A)\n",
+       "\n",
+       "    for i in range(n + 1):\n",
+       "        for A_i in combinations(A, i):\n",
+       "            if consistent_det(A_i, E):\n",
+       "                return set(A_i)\n",
+       "
\n", + "\n", + "\n" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "psource(minimal_consistent_det)" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "\n", + "\n", + "\n", + "\n", + " \n", + " \n", + " \n", + "\n", + "\n", + "

\n", + "\n", + "
def consistent_det(A, E):\n",
+       "    """Check if the attributes(A) is consistent with the examples(E)"""\n",
+       "    H = {}\n",
+       "\n",
+       "    for e in E:\n",
+       "        attr_values = tuple(e[attr] for attr in A)\n",
+       "        if attr_values in H and H[attr_values] != e['GOAL']:\n",
+       "            return False\n",
+       "        H[attr_values] = e['GOAL']\n",
+       "\n",
+       "    return True\n",
+       "
\n", + "\n", + "\n" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "psource(consistent_det)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Example:" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We already know that no-pizza-no-party but we will still check it through the `minimal_consistent_det` algorithm." + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Pizza'}\n" + ] + } + ], + "source": [ + "print(minimal_consistent_det(party, {'Pizza', 'Soda'}))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can also check it on some other example. Let's consider the following example :" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [], + "source": [ + "conductance = [\n", + " {'Sample': 'S1', 'Mass': 12, 'Temp': 26, 'Material': 'Cu', 'Size': 3, 'GOAL': 0.59},\n", + " {'Sample': 'S1', 'Mass': 12, 'Temp': 100, 'Material': 'Cu', 'Size': 3, 'GOAL': 0.57},\n", + " {'Sample': 'S2', 'Mass': 24, 'Temp': 26, 'Material': 'Cu', 'Size': 6, 'GOAL': 0.59},\n", + " {'Sample': 'S3', 'Mass': 12, 'Temp': 26, 'Material': 'Pb', 'Size': 2, 'GOAL': 0.05},\n", + " {'Sample': 'S3', 'Mass': 12, 'Temp': 100, 'Material': 'Pb', 'Size': 2, 'GOAL': 0.04},\n", + " {'Sample': 'S4', 'Mass': 18, 'Temp': 100, 'Material': 'Pb', 'Size': 3, 'GOAL': 0.04},\n", + " {'Sample': 'S4', 'Mass': 18, 'Temp': 100, 'Material': 'Pb', 'Size': 3, 'GOAL': 0.04},\n", + " {'Sample': 'S5', 'Mass': 24, 'Temp': 100, 'Material': 'Pb', 'Size': 4, 'GOAL': 0.04},\n", + " {'Sample': 'S6', 'Mass': 36, 'Temp': 26, 'Material': 'Pb', 'Size': 6, 'GOAL': 0.05},\n", + "]\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now, we check the `minimal_consistent_det` algorithm on the above example:" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Temp', 'Material'}\n" + ] + } + ], + "source": [ + "print(minimal_consistent_det(conductance, {'Mass', 'Temp', 'Material', 'Size'}))" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Temp', 'Size', 'Mass'}\n" + ] + } + ], + "source": [ + "print(minimal_consistent_det(conductance, {'Mass', 'Temp', 'Size'}))\n" + ] } ], "metadata": { @@ -669,7 +1648,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.3" + "version": "3.6.4" } }, "nbformat": 4,