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 ?
Data Science Stack Exchange is a question and answer site for Data science professionals, Machine Learning specialists, and those interested in learning more about the field. It only takes a minute to sign up.
Sign up to join this communityIf 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.
n
partitions? $\endgroup$