-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathk_means_main.py
30 lines (23 loc) · 1.41 KB
/
k_means_main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import sklearn.datasets
import argparse
from k_means import k_means
parser = argparse.ArgumentParser()
# These arguments will be set appropriately by ReCodEx, even if you change them.
parser.add_argument("--clusters", default=3, type=int, help="Number of clusters")
parser.add_argument("--examples", default=200, type=int, help="Number of examples")
parser.add_argument("--init", default="random", type=str, help="Initialization (random/kmeans++)")
parser.add_argument("--iterations", default=20, type=int, help="Number of kmeans iterations to perfom")
parser.add_argument("--plot", default='plot', const=True, nargs="?", type=str, help="Plot the predictions")
parser.add_argument("--recodex", default=False, action="store_true", help="Running in ReCodEx")
parser.add_argument("--seed", default=42, type=int, help="Random seed")
# If you add more arguments, ReCodEx will keep them with your default values.
def main(args):
# Generate artificial data
data, target = sklearn.datasets.make_blobs(
n_samples=args.examples, centers=args.clusters, n_features=2, random_state=args.seed)
clusters = k_means(data, iterations=args.iterations, k=args.clusters, initialization=args.init, plot=args.plot, random_seed=args.seed)
return clusters
if __name__ == "__main__":
args = parser.parse_args([] if "__file__" not in globals() else None)
centers = main(args)
print("Cluster assignments:", centers, sep="\n")