Skip to content

Instantly share code, notes, and snippets.

@isedgar
Created February 23, 2024 19:57
Show Gist options
  • Save isedgar/02db92ef698b6cdb3314785bf1809124 to your computer and use it in GitHub Desktop.
Save isedgar/02db92ef698b6cdb3314785bf1809124 to your computer and use it in GitHub Desktop.
Unbeatable tic-tac-toe one-vs-one classifier (assuming the machine always starts in the upper left corner).
import numpy as np
from sklearn.preprocessing import normalize
from sklearn.multiclass import OneVsOneClassifier as ovo
from sklearn.linear_model import Perceptron
X = np.array([[-1,0,0,0,0,0,0,0],[-1,-1,0,0,0,1,0,0],[-1,0,-1,0,0,1,0,0],[-1,-1,-1,0,0,1,0,1],[-1,0,-1,-1,0,1,0,1],[-1,0,-1,0,-1,1,0,1],[-1,0,-1,0,0,1,-1,1],[-1,0,0,-1,0,1,0,0],[-1,0,0,0,-1,1,0,0],[-1,0,0,0,0,1,-1,0],[-1,0,0,0,0,1,0,-1],[0,-1,0,0,0,0,0,0],[-1,-1,0,0,0,1,0,0],[0,-1,-1,0,0,1,0,0],[-1,-1,-1,0,0,1,0,1],[0,-1,-1,-1,0,1,0,1],[0,-1,-1,0,-1,1,0,1],[0,-1,-1,0,0,1,-1,1],[0,-1,0,-1,0,1,0,0],[0,-1,0,0,-1,1,0,0],[0,-1,0,0,0,1,-1,0],[0,-1,0,0,0,1,0,-1],[0,0,-1,0,0,0,0,0],[-1,1,-1,0,0,0,0,0],[-1,1,-1,1,-1,0,0,0],[-1,1,-1,1,0,-1,0,0],[-1,1,-1,1,0,0,-1,0],[-1,1,-1,1,0,0,0,-1],[0,1,-1,-1,0,0,0,0],[0,1,-1,0,-1,0,0,0],[0,1,-1,0,0,-1,0,0],[0,1,-1,0,0,0,-1,0],[0,1,-1,0,0,0,0,-1],[0,0,0,-1,0,0,0,0],[1,-1,0,-1,0,0,0,0],[1,-1,-1,-1,0,1,0,0],[1,-1,-1,-1,1,1,-1,0],[1,-1,-1,-1,1,1,0,-1],[1,-1,0,-1,-1,1,0,0],[1,-1,0,-1,0,1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2,2,2,1,3,7,7,7,5,0,0,0,0,0,0,5,4,7,6,2,2,2,1,1,1,1,1,1,3,7,7,7,5,0,0,0,0,0,1,7,3,4,3,3,0,0,0,0,0,1,3,7,7,7,5,0,0,0,0,0,1,5,3,2,3,3,0,0,0,0,0], dtype=np.int32)
X = normalize(X, norm='l2')
clf = ovo(Perceptron(tol=None, shuffle=False)).fit(X, y)
print(clf.score(X,y))
@abrahimzaman360
Copy link

What is y here?

@isedgar
Copy link
Author

isedgar commented Feb 28, 2024

What is y here?

Labels (or classes)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment