import numpy X = numpy.loadtxt("spirals.csv", dtype = numpy.float, delimiter = ',') from sklearn.cluster import SpectralClustering spectral = SpectralClustering(n_clusters = 2, gamma = 4) # larger gamma shrinks the kernel width spectral.fit(X) from matplotlib import pyplot %matplotlib inline cluster_labels = spectral.labels_ pyplot.scatter(X[:,0], X[:,1], c = cluster_labels) output = open("predictions.csv", "w") output.write("Id,Prediction\n") index = 0 for i in range(len(cluster_labels) - 1): for j in range(i + 1, len(cluster_labels)): if (cluster_labels[i] == cluster_labels[j]): output.write(str(index) + ",1\n") # same cluster else: output.write(str(index) + ",0\n") # different clusters index = index + 1 output.close()