Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kwinkunks/203fb8f03b490208bb68af744cf45b06 to your computer and use it in GitHub Desktop.
Save kwinkunks/203fb8f03b490208bb68af744cf45b06 to your computer and use it in GitHub Desktop.
Small demo of how you might cluster mapped features, ignore (x, y) location and accounting for some NaNs around the edge.
import numpy as np
# Make some fake data
# Make array with 100 rows, 100 columns, and 6 'features' (different maps)
shape = (100, 100, 3)
data = np.random.random(shape)
# Pretend it has NaNs around edge.
data[:10] = np.nan
data[-10:] = np.nan
data[:, :10] = np.nan
data[:, -10:] = np.nan
# If arr has some NaNs in it (e.g. around edge)
# Make a 2D array that has nans where your data has at least one nan.
summ = np.sum(data, -1)
mask = ~np.isnan(summ)
X = data[mask]
# Now you can cluster the features:
from sklearn.cluster import KMeans
clu = KMeans(n_clusters=4)
yhat = clu.fit_transform(X)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment