You are on page 1of 3

K-means | MLExpert https://www.algoexpert.

io/machine-learning/coding-questions/K-means

MLExpert Quad Layout 14px


00:00:00 | 00:00:00
Sublime

Prompt Scratchpad Our Solution(s) Video


Your
Explanation
Solutions Run Code

Solution 1 Solution 2 Solution 3


Category: Model Concepts
1 import random

K-means 2
3
4 class Centroid:
Use the k-means algorithm to return the k
5 def __init__(self, location):
means (or centroids) for the provided user 6 self.location = location
features. 7 self.closest_users = set()
8
These user features are the result of a 9
dimensionality reduction by PCA on some 10 def get_k_means(user_feature_map, num_featur
user-app interaction data. You'll have access 11 # Don't change the following two lines o
12 random.seed(42)
to a USER_FEATURE_MAP dictionary,
13 # Gets the inital users, to be used as c
mapping each user "uid_i" to a respective 14 inital_centroid_users = random.sample
list of 4 features associated with the user in 15
question. 16 centroids = [Centroid(user_feature_map[i
17 for _ in range(10):
Below is an example portion of the 18 for uid, features in user_feature_ma
19 closest_centroid_distance
USER_FEATURE_MAP :
20 closest_centroid = None
21 for centroid in centroids:
{ 22 features_to_centroid_distanc
"uid_0": [-1.479359467505669, -1.895497044385029,
23 -2. if features_to_centroid_dist
"uid_1": [-1.8284426855307128, -1.714098142408679,
24 -0 closest_centroid_distanc
"uid_2": [-1.8398933218386004, -1.7896757009107565,
25 - closest_centroid
26
"uid_3": [-1.23224975874512, -1.8447858273094768, -1. closest_centroid.closest_users
"uid_4": [-1.7714737791268318, -1.2725603446513774, -
# ...
# More of the same kind of data.
}

Note that:

• The initial centroid locations are


selected for you to ensure consistency
when verifying your solution.

• You should execute at least 10


iterations of the k-means algorithm, not
including the initialization of the
centroids.

• You should use the Manhattan distance

1 de 3 20/05/2022 08:09 p. m.
K-means | MLExpert https://www.algoexpert.io/machine-learning/coding-questions/K-means

as the distance metric.

• You shouldn't use any libraries that


implement k-means for you.

• Your output values will automatically be


rounded to the fourth decimal.

If you're unfamiliar with the k-means


algorithm, we recommend watching the
k-means video in the ML Crash Course on
MLExpert before starting to code.

Sample Input

Tests Quick Test Sandbox Custom Output Raw Output Submit Code

Test Case 1 Yay, your code passed all the


1 { test cases!
2 "k": 1
3 }
5 / 5 test cases passed.

Test Case 2 Test Case 1 passed!


1 {
2 "k": 2
3 } Test Case 2 passed!

Test Case 3
Test Case 3 passed!
1 {
2 "k": 3
3 }
Test Case 4 passed!
Test Case 4

1 { Test Case 5 passed!


2 "k": 4
3 }

Test Case 5

1 {
2 "k": 5

2 de 3 20/05/2022 08:09 p. m.
K-means | MLExpert https://www.algoexpert.io/machine-learning/coding-questions/K-means

3 de 3 20/05/2022 08:09 p. m.

You might also like