|
| 1 | +defmodule Classifiers.Perceptron.Average do |
| 2 | + defstruct weights: %{}, |
| 3 | + edges: %{}, |
| 4 | + count: 0, |
| 5 | + epoch: 0 |
| 6 | + |
| 7 | + @doc """ |
| 8 | + Get a new classifier pid. |
| 9 | + """ |
| 10 | + def new do |
| 11 | + {:ok, pid} = Agent.start_link fn -> |
| 12 | + %Classifiers.Perceptron.Average{} |
| 13 | + end |
| 14 | + |
| 15 | + pid |
| 16 | + end |
| 17 | + |
| 18 | + @doc """ |
| 19 | + Fit a stream of data to an existing classifier. |
| 20 | + Currently expects input in the form of a stream of maps as the following: |
| 21 | + [ feature_1, feature_2, ... feature_n, class ] |
| 22 | + """ |
| 23 | + def fit(stream, pid) do |
| 24 | + stream |> Stream.chunk(10) |> Enum.each fn chunk -> |
| 25 | + Agent.get_and_update pid, fn classifier -> |
| 26 | + c = chunk |> Enum.reduce classifier, fn row, classifier -> |
| 27 | + label = row |> List.last |
| 28 | + features = row |> Enum.drop(-1) |
| 29 | + |> Enum.with_index |
| 30 | + |> Enum.map(fn {a, b} -> {a,b} end) |
| 31 | + |
| 32 | + classifier = case classifier |> make_prediction(features, true) do |
| 33 | + nil -> |
| 34 | + %{ |
| 35 | + classifier | edges: classifier.edges |> Map.put( |
| 36 | + label, features |> Enum.into(%{}, &({&1, 1})) |
| 37 | + ) |
| 38 | + } |
| 39 | + ^label -> |
| 40 | + classifier |
| 41 | + prediction -> |
| 42 | + %{ |
| 43 | + classifier | edges: classifier.edges |> Map.update( |
| 44 | + label, %{}, fn current -> |
| 45 | + features |> Enum.reduce( |
| 46 | + current, fn feature, current -> |
| 47 | + current |> Map.update(feature, 0, &(&1 + 1)) |
| 48 | + end |
| 49 | + ) |
| 50 | + end |
| 51 | + ) |> Map.update( |
| 52 | + prediction, %{}, fn current -> |
| 53 | + features |> Enum.reduce( |
| 54 | + current, fn feature, current -> |
| 55 | + current |> Map.update(feature, 0, &(&1 - 1)) |
| 56 | + end |
| 57 | + ) |
| 58 | + end |
| 59 | + ) |
| 60 | + } |
| 61 | + end |
| 62 | + |
| 63 | + %{ classifier | |
| 64 | + count: classifier.count + 1, |
| 65 | + weights: classifier.edges |> Enum.reduce( |
| 66 | + classifier.weights, fn { label, edges }, weights -> |
| 67 | + target = weights |> Map.get(label, %{}) |
| 68 | + target = edges |> Enum.reduce(target, fn { feature, edge }, target -> |
| 69 | + target |> Map.update(feature, 0, fn weight -> |
| 70 | + (classifier.count * weight + edge) / (classifier.count + 1) |
| 71 | + end) |
| 72 | + end) |
| 73 | + |
| 74 | + weights |> Map.update(label, %{}, fn w -> w |> Map.merge(target) end) |
| 75 | + end |
| 76 | + ) |
| 77 | + } |
| 78 | + end |
| 79 | + |
| 80 | + {:ok, c} |
| 81 | + end |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + @doc """ |
| 86 | + Predict the class for one set of features. |
| 87 | + """ |
| 88 | + def predict_one(features, pid) do |
| 89 | + end |
| 90 | + |
| 91 | + @doc """ |
| 92 | + Predict the classes for a stream of features |
| 93 | + """ |
| 94 | + def predict(stream, pid) do |
| 95 | + c = classifier(pid) |
| 96 | + stream |> Stream.transform(0, fn row, acc -> |
| 97 | + features = row |> Enum.with_index |> Enum.map(fn {a, b} -> {a, b} end) |
| 98 | + |
| 99 | + { [ c |> make_prediction(features, false) ], acc + 1 } |
| 100 | + end) |
| 101 | + end |
| 102 | + |
| 103 | + defp make_prediction(%{edges: edges}, features, true) when map_size(edges) == 0 do |
| 104 | + end |
| 105 | + defp make_prediction(%{edges: edges}, features, true) do |
| 106 | + {p, _} = edges |> Enum.max_by fn { label, edge } -> |
| 107 | + features |> Enum.reduce(0, fn feature, weight -> weight + Map.get(edge, feature, 0) end) |
| 108 | + end |
| 109 | + |
| 110 | + p |
| 111 | + end |
| 112 | + defp make_prediction(%{weights: weights}, features, false) do |
| 113 | + {p, _} = weights |> Enum.max_by fn { label, weight } -> |
| 114 | + features |> Enum.reduce(0, fn feature, w -> w + Map.get(weight, feature, 0) end) |
| 115 | + end |
| 116 | + |
| 117 | + p |
| 118 | + end |
| 119 | + |
| 120 | + defp classifier(pid) do |
| 121 | + Agent.get pid, fn c -> c end |
| 122 | + end |
| 123 | + |
| 124 | +end |
0 commit comments