knn sklearn Algorithm

K-means clustering is a method of vector quantization, originally from signal processing, that aims to partition N observations into K clusters in which each observation belongs to the cluster with the nearest mean (cluster centers or cluster centroid), serve as a prototype of the cluster. use the 1-nearest neighbor classifier to the cluster centers obtained by K-means classifies new data into the existing clusters. The standard algorithm was first proposed by Stuart Lloyd of Bell Labs in 1957 as a technique for pulsing-code modulation, though it was n't published as a journal article until 1982.The term" K-means" was first used by James MacQueen in 1967, though the idea goes back to Hugo Steinhaus in 1956.In 1965, Edward W. Forgy published essentially the same method, which is why it is sometimes referred to as Lloyd-Forgy.
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris
from sklearn.neighbors import KNeighborsClassifier

# Load iris file
iris = load_iris()
iris.keys()


print(f"Target names: \n {iris.target_names} ")
print(f"\n Features: \n {iris.feature_names}")

# Train set e Test set
X_train, X_test, y_train, y_test = train_test_split(
    iris["data"], iris["target"], random_state=4
)

# KNN

knn = KNeighborsClassifier(n_neighbors=1)
knn.fit(X_train, y_train)

# new array to test
X_new = [[1, 2, 1, 4], [2, 3, 4, 5]]

prediction = knn.predict(X_new)

print(
    "\nNew array: \n {}"
    "\n\nTarget Names Prediction: \n {}".format(X_new, iris["target_names"][prediction])
)

LANGUAGE:

DARK MODE: