Only the Border Matters

Machine Learning in CodeEasy+10 XP+10

Only the Border Matters

Two groups of points can be split by a straight line. A support vector machine picks the line with the widest gap, and only the points touching the gap decide where it goes.

Write linear_svm(points, labels, new_points).

  • Fit SVC(kernel="linear", C=10) on the points. The two columns share the same units, so no scaling is needed.
  • Count the support vectors: add up the model's n_support_.
  • Predict the label of every point in new_points, in order.

Return a dict {"support_vectors": ..., "predictions": [...]} with an int and a list of ints.

Examples

points = [[1.0, 1.5], [1.5, 2.5], [2.0, 1.0], [2.5, 2.0], [1.2, 3.0], [5.0, 5.5], [5.5, 4.5], [6.0, 6.0], [4.6, 6.3], [6.5, 5.0], [3.1, 2.9], [4.0, 4.4]]
labels = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1]
new_points = [[2, 2], [5, 5]]
returns {'support_vectors': 2, 'predictions': [0, 1]}
points = [[1.0, 1.5], [1.5, 2.5], [2.0, 1.0], [2.5, 2.0], [1.2, 3.0], [5.0, 5.5], [5.5, 4.5], [6.0, 6.0], [4.6, 6.3], [6.5, 5.0], [3.1, 2.9], [4.0, 4.4]]
labels = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1]
new_points = [[3.0, 3.3], [4.2, 3.6]]
returns {'support_vectors': 2, 'predictions': [0, 1]}

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run