Chapter 4: Dimensionality Reduction and Unsupervised Learning
Activity 12: Ensemble k-means Clustering and Calculating Predictions
Solution:
After the glass dataset has been imported, shuffled, and standardized (see Exercise 58):
- Instantiate an empty data frame to append each model and save it as the new data frame object
labels_df
with the following code:import pandas as pd labels_df = pd.DataFrame()
- Import the
KMeans
function outside of the loop using the following:from sklearn.cluster import KMeans
- Complete 100 iterations as follows:
for i in range(0, 100):
- Save a KMeans model object with two clusters (arbitrarily decided upon, a priori) using:
model = KMeans(n_clusters=2)
- Fit the model to
scaled_features
using the following:model.fit(scaled_features)
- Generate the labels array and save it as the labels object, as follows:
labels = model.labels_
- Store labels as a column in
labels_df
named after the iteration using the code:labels_df['Model_...