|
10 | 10 | "import random\n", |
11 | 11 | "\n", |
12 | 12 | "import pandas as pd\n", |
13 | | - "import tornado.ioloop\n", |
14 | 13 | "\n", |
15 | 14 | "from streamz import Stream\n", |
16 | 15 | "import hvplot.streamz\n", |
17 | 16 | "from streamz.river import RiverTrain\n", |
18 | 17 | "from river import cluster\n", |
19 | 18 | "import holoviews as hv\n", |
20 | | - "import panel as pn\n", |
21 | 19 | "from panel.pane.holoviews import HoloViews\n", |
22 | | - "import tornado.ioloop\n", |
23 | 20 | "hv.extension('bokeh')" |
24 | 21 | ] |
25 | 22 | }, |
|
30 | 27 | "metadata": {}, |
31 | 28 | "outputs": [], |
32 | 29 | "source": [ |
33 | | - "model = cluster.STREAMKMeans(n_clusters=3)\n", |
| 30 | + "model = cluster.KMeans(n_clusters=3, sigma=0.1, mu=0.5)\n", |
34 | 31 | "centres = [[random.random(), random.random()] for _ in range(3)]\n", |
35 | 32 | "\n", |
36 | | - "def gen(split_chance=0.01):\n", |
| 33 | + "def gen(move_chance=0.05):\n", |
37 | 34 | " centre = int(random.random() * 3) # 3x faster than random.randint(0, 2)\n", |
38 | | - " if random.random() < split_chance:\n", |
39 | | - " centres[centre] = [random.random(), random.random()]\n", |
| 35 | + " if random.random() < move_chance:\n", |
| 36 | + " centres[centre][0] += random.random() / 5 - 0.1\n", |
| 37 | + " centres[centre][1] += random.random() / 5 - 0.1\n", |
40 | 38 | " value = {'x': random.random() / 20 + centres[centre][0],\n", |
41 | 39 | " 'y': random.random() / 20 + centres[centre][1]}\n", |
42 | 40 | " return value\n", |
|
55 | 53 | "metadata": {}, |
56 | 54 | "outputs": [], |
57 | 55 | "source": [ |
58 | | - "s = Stream.from_periodic(gen, 0.05)\n", |
| 56 | + "s = Stream.from_periodic(gen, 0.03)\n", |
59 | 57 | "km = RiverTrain(model, pass_model=True)\n", |
60 | 58 | "s.map(lambda x: (x,)).connect(km) # learn takes a tuple of (x,[ y[, w]])\n", |
61 | 59 | "ex = pd.DataFrame({'x': [0.5], 'y': [0.5]})\n", |
62 | 60 | "ooo = s.map(lambda x: pd.DataFrame([x])).to_dataframe(example=ex)\n", |
63 | 61 | "out = km.map(get_clusters)\n", |
64 | | - "s.emit(gen()) # set initial model" |
| 62 | + "\n", |
| 63 | + "# start things\n", |
| 64 | + "s.emit(gen()) # set initial model\n", |
| 65 | + "for i, (x, y) in enumerate(centres):\n", |
| 66 | + " model.centers[i]['x'] = x\n", |
| 67 | + " model.centers[i]['y'] = y\n" |
65 | 68 | ] |
66 | 69 | }, |
67 | 70 | { |
68 | 71 | "cell_type": "code", |
69 | 72 | "execution_count": null, |
70 | | - "id": "c24d2363", |
| 73 | + "id": "1b4de451", |
71 | 74 | "metadata": {}, |
72 | 75 | "outputs": [], |
73 | 76 | "source": [ |
74 | | - "for i, (x, y) in enumerate(centres):\n", |
75 | | - " model.centers[i]['x'] = x\n", |
76 | | - " model.centers[i]['y'] = y\n" |
| 77 | + "pout = out.to_dataframe(example=ex)\n", |
| 78 | + "pl = (ooo.hvplot.scatter('x', 'y', color=\"blue\", backlog=50) *\n", |
| 79 | + " pout.hvplot.scatter('x', 'y', color=\"red\", backlog=3))\n", |
| 80 | + "pl.opts(xlim=(-0.2, 1.2), ylim=(-0.2, 1.2), height=600, width=600)\n", |
| 81 | + "pl" |
77 | 82 | ] |
78 | 83 | }, |
79 | 84 | { |
80 | 85 | "cell_type": "code", |
81 | 86 | "execution_count": null, |
82 | | - "id": "1b4de451", |
| 87 | + "id": "c24d2363", |
83 | 88 | "metadata": {}, |
84 | 89 | "outputs": [], |
85 | 90 | "source": [ |
|
89 | 94 | { |
90 | 95 | "cell_type": "code", |
91 | 96 | "execution_count": null, |
92 | | - "id": "8f356afa", |
| 97 | + "id": "18cfd94e", |
93 | 98 | "metadata": {}, |
94 | 99 | "outputs": [], |
95 | 100 | "source": [ |
96 | | - "pout = out.to_dataframe(example=ex)\n", |
97 | | - "pl = (ooo.hvplot.scatter('x', 'y', color=\"blue\", backlog=100) * \n", |
98 | | - " pout.hvplot.scatter('x', 'y', color=\"red\", backlog=3))\n", |
99 | | - "pl.opts(xlim=(-0.5, 1.5), ylim=(-0.5, 1.5), height=600, width=600)" |
| 101 | + "s.stop()" |
100 | 102 | ] |
101 | 103 | }, |
102 | 104 | { |
103 | 105 | "cell_type": "code", |
104 | 106 | "execution_count": null, |
105 | | - "id": "18cfd94e", |
| 107 | + "id": "4537495c", |
106 | 108 | "metadata": {}, |
107 | 109 | "outputs": [], |
108 | | - "source": [ |
109 | | - "s.stop()" |
110 | | - ] |
| 110 | + "source": [] |
111 | 111 | } |
112 | 112 | ], |
113 | 113 | "metadata": { |
|
0 commit comments