# Developing Modified KNN Approach

I want to divide the training set into n partitions further besides testing set. How can I do that?

Furthermore, I'm creating these groups in the training set. How can I calculate centroid of each partition ?

• Are you trying to implement K-Means and then KNN based on its results? Or are you just looking for a clustering technique with n partitions? Oct 13, 2022 at 9:40
• Clustering technique with n partitions . Oct 20, 2022 at 5:30

## 1 Answer

If you already know n, you can use cluster module on scikit-learn. Here's a toy example:

# Load libraries
import pandas as pd
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans

centers = 3 #here your n should go
# Make simulated feature matrix
features, _ = make_blobs(n_samples = 50,
n_features = 2,
centers = centers,
random_state = 22)

dataframe = pd.DataFrame(features, columns=["feature_1", "feature_2"])

# Make k-means clusterer
clusterer = KMeans(3, random_state=0)
clusterer.fit(features)


After fitting the model, ou can get the class labels by predict attribute:

clusterer.predict(features)


And each observation centroid is accessible through cluster_centers_:

clusterer.cluster_centers_


You can use scikit-learn documentation on this for more information.