def impute(target_xs, clf, raster_info, outdir="<STR_LIT>", linechunk=<NUM_LIT:1000>, class_prob=True, certainty=True):
if not os.path.exists(outdir):<EOL><INDENT>os.makedirs(outdir)<EOL><DEDENT>shape = raster_info['<STR_LIT>']<EOL>profile = {<EOL>'<STR_LIT>': raster_info['<STR_LIT>'],<EOL>'<STR_LIT>': shape[<NUM_LIT:1>],<EOL>'<STR_LIT>': shape[<NUM_LIT:0>],<EOL>'<STR_LIT>': <NUM_LIT:1>,<EOL>'<STR_LIT:count>': <NUM_LIT:1>,<EOL>'<STR_LIT>': raster_info['<STR_LIT>'],<EOL>'<STR_LIT>': u'<STR_LIT>',<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': -<NUM_LIT>,<EOL>'<STR_LIT>': False,<EOL>'<STR_LIT>': raster_info['<STR_LIT>'].to_gdal(),<EOL>'<STR_LIT:width>': shape[<NUM_LIT:1>]}<EOL>try:<EOL><INDENT>response_path = os.path.join(outdir, "<STR_LIT>")<EOL>response_ds = rasterio.open(response_path, '<STR_LIT:w>', **profile)<EOL>profile['<STR_LIT>'] = '<STR_LIT>'<EOL>if certainty:<EOL><INDENT>certainty_path = os.path.join(outdir, "<STR_LIT>")<EOL>certainty_ds = rasterio.open(certainty_path, '<STR_LIT:w>', **profile)<EOL><DEDENT>class_dss = []<EOL>if class_prob:<EOL><INDENT>classes = list(clf.classes_)<EOL>class_paths = []<EOL>for i, c in enumerate(classes):<EOL><INDENT>ods = os.path.join(outdir, "<STR_LIT>" % c)<EOL>class_paths.append(ods)<EOL><DEDENT>for p in class_paths:<EOL><INDENT>class_dss.append(rasterio.open(p, '<STR_LIT:w>', **profile))<EOL><DEDENT><DEDENT>if not linechunk:<EOL><INDENT>linechunk = shape[<NUM_LIT:0>]<EOL><DEDENT>chunks = int(math.ceil(shape[<NUM_LIT:0>] / float(linechunk)))<EOL>for chunk in range(chunks):<EOL><INDENT>logger.debug("<STR_LIT>" % (chunk+<NUM_LIT:1>, chunks))<EOL>row = chunk * linechunk<EOL>if row + linechunk > shape[<NUM_LIT:0>]:<EOL><INDENT>linechunk = shape[<NUM_LIT:0>] - row<EOL><DEDENT>start = shape[<NUM_LIT:1>] * row<EOL>end = start + shape[<NUM_LIT:1>] * linechunk<EOL>line = target_xs[start:end, :]<EOL>window = ((row, row + linechunk), (<NUM_LIT:0>, shape[<NUM_LIT:1>]))<EOL>responses = clf.predict(line)<EOL>responses2D = responses.reshape((linechunk, shape[<NUM_LIT:1>])).astype('<STR_LIT>')<EOL>response_ds.write_band(<NUM_LIT:1>, responses2D, window=window)<EOL>if certainty or class_prob:<EOL><INDENT>proba = clf.predict_proba(line)<EOL><DEDENT>if certainty:<EOL><INDENT>certaintymax = proba.max(axis=<NUM_LIT:1>)<EOL>certainty2D = certaintymax.reshape((linechunk, shape[<NUM_LIT:1>])).astype('<STR_LIT>')<EOL>certainty_ds.write_band(<NUM_LIT:1>, certainty2D, window=window)<EOL><DEDENT>for i, class_ds in enumerate(class_dss):<EOL><INDENT>proba_class = proba[:, i]<EOL>classcert2D = proba_class.reshape((linechunk, shape[<NUM_LIT:1>])).astype('<STR_LIT>')<EOL>class_ds.write_band(<NUM_LIT:1>, classcert2D, window=window)<EOL><DEDENT><DEDENT><DEDENT>finally:<EOL><INDENT>response_ds.close()<EOL>if certainty:<EOL><INDENT>certainty_ds.close()<EOL><DEDENT>for class_ds in class_dss:<EOL><INDENT>class_ds.close()<EOL><DEDENT><DEDENT>
Parameters
----------
target_xs: Array of explanatory variables for which to predict responses
clf: instance of a scikit-learn Classifier
raster_info: dictionary of raster attributes with key 'gt', 'shape' and 'srs'

Options
-------
outdir : output directory
linechunk : number of lines to process per pass; reduce only if memory is constrained
class_prob : Boolean. Should we create a probability raster for each class?
certainty : Boolean. Should we produce a raster of overall classification certainty?