|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 10, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "import pandas as pd\n", |
| 10 | + "\n", |
| 11 | + "get_data = lambda x, y, n: [(x, y) * 2 for _ in range(n)]\n", |
| 12 | + "data = get_data(1, 1, 207) + get_data(1, 0, 282) + get_data(0, 1, 231) + get_data(0, 0, 242)\n", |
| 13 | + "pdf = pd.DataFrame(data, columns=['x1', 'x2', 'x3', 'x4'])\n", |
| 14 | + "sdf = sqlContext.createDataFrame(pdf)" |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "code", |
| 19 | + "execution_count": 13, |
| 20 | + "metadata": {}, |
| 21 | + "outputs": [ |
| 22 | + { |
| 23 | + "name": "stdout", |
| 24 | + "output_type": "stream", |
| 25 | + "text": [ |
| 26 | + "root\n", |
| 27 | + " |-- x1: long (nullable = true)\n", |
| 28 | + " |-- x2: long (nullable = true)\n", |
| 29 | + " |-- x3: long (nullable = true)\n", |
| 30 | + " |-- x4: long (nullable = true)\n", |
| 31 | + "\n" |
| 32 | + ] |
| 33 | + } |
| 34 | + ], |
| 35 | + "source": [ |
| 36 | + "sdf.printSchema()" |
| 37 | + ] |
| 38 | + }, |
| 39 | + { |
| 40 | + "cell_type": "code", |
| 41 | + "execution_count": 29, |
| 42 | + "metadata": {}, |
| 43 | + "outputs": [], |
| 44 | + "source": [ |
| 45 | + "from itertools import combinations\n", |
| 46 | + "\n", |
| 47 | + "def to_counts(d):\n", |
| 48 | + " def as_key(k1, k2):\n", |
| 49 | + " keys = sorted([k1, k2])\n", |
| 50 | + " return keys[0], keys[1]\n", |
| 51 | + " \n", |
| 52 | + " def as_count(v1, v2):\n", |
| 53 | + " a, b, c, d = 0, 0, 0, 0\n", |
| 54 | + " if v1 is not None and v2 is not None:\n", |
| 55 | + " if v1 == 1 and v2 == 1:\n", |
| 56 | + " a = 1\n", |
| 57 | + " elif v1 == 1 and v2 == 0:\n", |
| 58 | + " b = 1\n", |
| 59 | + " elif v1 == 0 and v2 == 1:\n", |
| 60 | + " c = 1\n", |
| 61 | + " else:\n", |
| 62 | + " d = 1\n", |
| 63 | + " return a, b, c, d\n", |
| 64 | + " \n", |
| 65 | + " def transform(k1, k2):\n", |
| 66 | + " v1, v2 = d[k1], d[k2]\n", |
| 67 | + " return as_key(k1, k2), as_count(v1, v2)\n", |
| 68 | + " \n", |
| 69 | + " return [transform(k1, k2) for k1, k2 in combinations(d.keys(), 2)]\n", |
| 70 | + "\n", |
| 71 | + "def add_counts(a, b):\n", |
| 72 | + " return a[0] + b[0], a[1] + b[1], a[2] + b[2], a[3] + b[3]\n", |
| 73 | + "\n", |
| 74 | + "# to_counts({'x1': 1, 'x2': 1, 'x3': 1, 'x4': 1})" |
| 75 | + ] |
| 76 | + }, |
| 77 | + { |
| 78 | + "cell_type": "code", |
| 79 | + "execution_count": 31, |
| 80 | + "metadata": {}, |
| 81 | + "outputs": [ |
| 82 | + { |
| 83 | + "data": { |
| 84 | + "text/plain": [ |
| 85 | + "[(('x1', 'x2'), (207, 282, 231, 242)),\n", |
| 86 | + " (('x1', 'x3'), (489, 0, 0, 473)),\n", |
| 87 | + " (('x1', 'x4'), (207, 282, 231, 242)),\n", |
| 88 | + " (('x2', 'x3'), (207, 231, 282, 242)),\n", |
| 89 | + " (('x2', 'x4'), (438, 0, 0, 524)),\n", |
| 90 | + " (('x3', 'x4'), (207, 282, 231, 242))]" |
| 91 | + ] |
| 92 | + }, |
| 93 | + "execution_count": 31, |
| 94 | + "metadata": {}, |
| 95 | + "output_type": "execute_result" |
| 96 | + } |
| 97 | + ], |
| 98 | + "source": [ |
| 99 | + "sdf.rdd\\\n", |
| 100 | + " .flatMap(lambda r: to_counts(r.asDict()))\\\n", |
| 101 | + " .reduceByKey(lambda a, b: add_counts(a, b))\\\n", |
| 102 | + " .sortByKey()\\\n", |
| 103 | + " .collect()" |
| 104 | + ] |
| 105 | + }, |
| 106 | + { |
| 107 | + "cell_type": "code", |
| 108 | + "execution_count": null, |
| 109 | + "metadata": {}, |
| 110 | + "outputs": [], |
| 111 | + "source": [] |
| 112 | + } |
| 113 | + ], |
| 114 | + "metadata": { |
| 115 | + "kernelspec": { |
| 116 | + "display_name": "Python 3", |
| 117 | + "language": "python", |
| 118 | + "name": "python3" |
| 119 | + }, |
| 120 | + "language_info": { |
| 121 | + "codemirror_mode": { |
| 122 | + "name": "ipython", |
| 123 | + "version": 3 |
| 124 | + }, |
| 125 | + "file_extension": ".py", |
| 126 | + "mimetype": "text/x-python", |
| 127 | + "name": "python", |
| 128 | + "nbconvert_exporter": "python", |
| 129 | + "pygments_lexer": "ipython3", |
| 130 | + "version": "3.7.6" |
| 131 | + } |
| 132 | + }, |
| 133 | + "nbformat": 4, |
| 134 | + "nbformat_minor": 4 |
| 135 | +} |
0 commit comments