df = stack.to_pandas() # df = stack.to_pandas(res=500) df = df.melt(id_vars=['x', 'y']) from plotnine import * (ggplot(df, aes(x="x", y="y", fill="value")) + geom_tile() + coord_fixed() + facet_wrap("variable") + theme_light() + theme(axis_title=element_blank())) from sklearn.ensemble import RandomForestClassifier clf = RandomForestClassifier(n_estimators=100) clf.fit(X, y) stack.predict(clf, output='test', overwrite=True, height=25) stack.predict_proba(clf, output='test', overwrite=True, height=25) test = RasterRow('test') from grass.pygrass.modules.shortcuts import raster as r r.colors('test', color='random') test test.close() from sklearn.model_selection import cross_validate cross_validate(clf, X, y, cv=3) from grass.pygrass.gis.region import Region from grass.pygrass.modules.grid.grid import GridModule from grass.pygrass.modules.grid import split from grass.pygrass.modules.shortcuts import general as g
def main(): try: import sklearn import joblib if sklearn.__version__ < "0.20": gs.fatal( "Package python3-scikit-learn 0.20 or newer is not installed") except ImportError: gs.fatal("Package python3-scikit-learn 0.20 or newer is not installed") # parser options group = options["group"] output = options["output"] model_load = options["load_model"] probability = flags["p"] prob_only = flags["z"] chunksize = int(options["chunksize"]) # remove @ from output in case overwriting result if "@" in output: output = output.split("@")[0] # check probabilities=True if prob_only=True if prob_only is True and probability is False: gs.fatal("Need to set probabilities=True if prob_only=True") # reload fitted model and training data estimator, y, class_labels = joblib.load(model_load) # define RasterStack stack = RasterStack(group=group) # perform raster prediction region = Region() row_incr = math.ceil(chunksize / region.cols) # do not read by increments if increment > n_rows if row_incr >= region.rows: row_incr = None # prediction if prob_only is False: gs.message("Predicting classification/regression raster...") stack.predict( estimator=estimator, output=output, height=row_incr, overwrite=gs.overwrite(), ) if probability is True: gs.message("Predicting class probabilities...") stack.predict_proba( estimator=estimator, output=output, class_labels=np.unique(y), overwrite=gs.overwrite(), height=row_incr, ) # assign categories for classification map if class_labels is not None and prob_only is False: rules = [] for val, lab in class_labels.items(): rules.append(",".join([str(val), lab])) rules = "\n".join(rules) rules_file = string_to_rules(rules) r.category(map=output, rules=rules_file, separator="comma")
def main(): try: import sklearn from sklearn.externals import joblib if sklearn.__version__ < '0.20': gs.fatal("Scikit learn 0.20 or newer is required") except ImportError: gs.fatal("Scikit learn 0.20 or newer is not installed") # ------------------------------------------------------------------------- # Parser options # ------------------------------------------------------------------------- group = options['group'] output = options['output'] model_load = options['load_model'] probability = flags['p'] prob_only = flags['z'] chunksize = int(options['chunksize']) # remove @ from output in case overwriting result if '@' in output: output = output.split('@')[0] # check that probabilities=True if prob_only=True if prob_only is True and probability is False: gs.fatal('Need to set probabilities=True if prob_only=True') # ------------------------------------------------------------------------- # Reload fitted model and trainign data # ------------------------------------------------------------------------- X, y, sample_coords, group_id, estimator = joblib.load(model_load) # ------------------------------------------------------------------------- # Define RasterStack # ------------------------------------------------------------------------- # fetch individual raster names from group maplist = gs.read_command("i.group", group=group, flags="g").split(os.linesep)[:-1] # create RasterStack stack = RasterStack(rasters=maplist) # ------------------------------------------------------------------------- # Perform raster prediction # ------------------------------------------------------------------------- # calculate chunksize row = stack.read(1) rowsize_mg = row.nbytes * 1e-6 row_incr = int(float(chunksize) / float(rowsize_mg)) # prediction if prob_only is False: gs.message('Predicting classification/regression raster...') stack.predict(estimator=estimator, output=output, height=row_incr, overwrite=gs.overwrite()) if probability is True: gs.message('Predicting class probabilities...') stack.predict_proba(estimator=estimator, output=output, class_labels=np.unique(y), overwrite=gs.overwrite(), height=row_incr)