def kmeans_missing(xx_, no_clusters, max_iter=30):
    """Perform K-Means clustering on data with missing values.
    Found it in stackoverflow
    Argumentss:
      xx_:            An [n_samples, n_features] array of data to cluster.
      n_clusters:   Number of clusters to form.
      max_iter:     Maximum number of EM iterations to perform.

    Returns:
      labels:       An [n_samples] vector of integer labels.
      centroids:    An [n_clusters, n_features] array of cluster centroids.
      x_hat:        Copy of xx_ with the missing values filled in.
    """
    from sklearn.cluster import KMeans
    import numpy as np
    prev_centroids = []
    prev_labels = []
    missing = ~np.isfinite(xx_)
    mu_ = np.nanmean(xx_, 0, keepdims=1)
    x_hat = np.where(missing, mu_, xx_)
    for cc_ in np.arange(max_iter):
        if cc_ > 0:
            cls = KMeans(no_clusters, init=prev_centroids)
        else:
            cls = KMeans(no_clusters, n_jobs=-1)
        labels = cls.fit_predict(x_hat)
        centroids = cls.cluster_centers_
        x_hat[missing] = centroids[labels][missing]
        if cc_ > 0 and np.all(labels == prev_labels):
            break
        prev_labels = labels
        prev_centroids = cls.cluster_centers_
    return labels, centroids, x_hat
