Skip to content

Instantly share code, notes, and snippets.

@aymanosman
Created November 30, 2024 12:28
Show Gist options
  • Save aymanosman/aa20b3832148260d257cf43e2d497cc4 to your computer and use it in GitHub Desktop.
Save aymanosman/aa20b3832148260d257cf43e2d497cc4 to your computer and use it in GitHub Desktop.

SVM bug

Mix.install([
  {:scholar, "~> 0.3.1"},
  {:tucan, "~> 0.4.1"},
  {:kino_vega_lite, "~> 0.1.13"}
])

Section

x = Nx.tensor([[1.0, 2.0], [3.0, 2.0], [4.0, 7.0]])
y = Nx.tensor([1, 0, 1])
model = Scholar.Linear.SVM.fit(x, y, num_classes: 2)
Scholar.Linear.SVM.predict(model, x)
#Nx.Tensor<
  s32[3]
  [1, 1, 1]
>
points = 
  Nx.concatenate({x, Nx.new_axis(y, -1)}, axis: -1)
  |> Nx.to_list()
  |> Enum.map(fn [x1, x2, y] -> 
    %{x1: x1, x2: x2, y: y}
  end)
[%{y: 1.0, x2: 2.0, x1: 1.0}, %{y: 0.0, x2: 2.0, x1: 3.0}, %{y: 1.0, x2: 7.0, x1: 4.0}]
Tucan.scatter(points, "x1", "x2")
|> Tucan.color_by("y")
{"$schema":"https://vega.github.io/schema/vega-lite/v5.json","data":{"values":[{"x1":1.0,"x2":2.0,"y":1.0},{"x1":3.0,"x2":2.0,"y":0.0},{"x1":4.0,"x2":7.0,"y":1.0}]},"encoding":{"color":{"field":"y"},"x":{"field":"x1","scale":{"zero":false},"type":"quantitative"},"y":{"field":"x2","scale":{"zero":false},"type":"quantitative"}},"mark":{"fillOpacity":1,"type":"point"}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment