1

After training the kmeans model, how can i obtain the number of classes of the model? i've tried with get(), getInfo() and doesnt work.

enter image description here

https://code.earthengine.google.com/ff0c7bd8102d17b2d267b96bd070342a

Value that must to be extracted in "//var to_print_2 = clusterer.get('nClusters')"

// STEP 1: Import image we wish to classify
var l8 = ee.ImageCollection('LANDSAT/LC08/C01/T1')

// Remove the clouds
var cloud_free = ee.Algorithms.Landsat.simpleComposite({
  collection: l8.filterDate('2018-01-01', '2018-12-31'),
  asFloat: true
})

// STEP 2: Draw your box which you will sample from!  
// Make the training dataset.
var training = cloud_free.sample({
  region: geometry,
  scale: 30,
  numPixels: 5000
});

// Step 3: Create the clusterer and train it, play around with
// number of classes
var clusterer = ee.Clusterer.wekaKMeans(3).train(training);

print('clusterer',clusterer instanceof ee.ComputedObject);

var to_print = clusterer.getInfo();

print('clusterer_info',clusterer.toString());

print(typeof clusterer);

var clusterer_list = ee.List(clusterer);

print('clusterer_list',clusterer_list);

print(typeof clusterer_list);


//var to_print_2 = clusterer.get('nClusters');

//print(to_print_2);

//var to_print_3 = clusterer.Filter.filterMetadata('nClusters','equals',3);

//print(to_print_3);

// Step 4: Classify our image
var classified = cloud_free.cluster(clusterer);
print(classified);
var cluster_1 = classified.eq(0);
print('min',cluster_1);


// Display
Map.setCenter(-122.31040963183364, 37.782065864682096, 11)
Map.addLayer(classified.randomVisualizer(), {}, 'clusters');
  • Can you provide a working example for us? Follow the guidelines of StackExchange, and you'll more likely get a suitable answer! – Jobbo90 Dec 8 '19 at 10:42
  • sorry, here's the link: code.earthengine.google.com/ff0c7bd8102d17b2d267b96bd070342a – R. Márquez Dec 8 '19 at 17:32
  • 1
    Assuming your edits are sufficient to get this question on-topic again I'll already post an suggestion here. It's a bit of dirty work around, don't know if ee.Clusterer objects are directly accessable, but this could work: "var clusters = ee.FeatureCollection(training.cluster(clusterer, "cluster")) print(clusters.limit(5))" "var to_print3 = clusters.reduceColumns(ee.Reducer.countDistinct(), ["cluster"]) print(to_print3);" – Jobbo90 Dec 9 '19 at 7:48
  • 1
    I must be misunderstanding something here. Don't you specify the number of classes you want at this point: ee.Clusterer.wekaKMeans(3) – Daniel Wiell Dec 9 '19 at 20:23
  • Yes, you're right, but in other cases, like using "ee.Clusterer.wekaCobweb(0.1,0.01,23).train(training)" or generation an iteration over wekaKMeans, i need to obtain the value from the ee.ComputedObject. – R. Márquez Dec 9 '19 at 20:33
2

You can obtain the number of clusters as follows:

var classified = cloud_free.cluster(clusterer, "cluster");

var nClusters = classified.distinct("cluster").size();

print('number of clusters',nClusters);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.