machine-learning – Scikit Learn – A Basic Simple Classification Problem(XOR) using k nearest neighbor algorithm

Consider you want to predict the correct answer for XOR popular problem. You Knew what is XOR(e.g [x0 x1] => y). for example [0 0] => 0, [0 1] => [1] and…

#Load Sickit learn data
from sklearn.neighbors import KNeighborsClassifier

#X is feature vectors, and y is correct label(To train model)
X = [[0, 0],[0 ,1],[1, 0],[1, 1]]
y = [0,1,1,0]

#Initialize a Kneighbors Classifier with K parameter set to 2
KNC = KNeighborsClassifier(n_neighbors= 2)

#Fit the model(the KNC learn y Given X)
KNC.fit(X, y)

#print the predicted result for [1 1]
print(KNC.predict([[1 1]]))

if you want to reproduce, please indicate the source:
machine-learning – Scikit Learn – A Basic Simple Classification Problem(XOR) using k nearest neighbor algorithm - CodeDay