Leave One Out Per Class (LOOPC)

This example shows how to make a Leave One Out for each class.

Import librairies

from museotoolbox.cross_validation import LeaveOneOut
from museotoolbox import datasets

Load HistoricalMap dataset

X,y = datasets.load_historical_data(return_X_y=True,low_res=True)

Create CV

LOOPC = LeaveOneOut(random_state=8,verbose=False)
for tr,vl in LOOPC.split(X=None,y=y):
    print(tr,vl)

Out:

[   0    1    2 ... 2997 2998 3174] [ 594 2539 3145 2879 3046]
[   0    1    2 ... 2997 2998 3046] [1231  736  321  498 3174]

Note

Split is made to generate each fold

# Show label

for tr,vl in LOOPC.split(X=None,y=y):
    print(y[vl])

Out:

[1 2 3 4 5]
[1 2 3 4 5]

Save each train/valid fold in a file

In order to translate polygons into points (each points is a pixel in the raster) we use sampleExtraction from vector_tools to generate a temporary vector.

trvl = LOOPC.save_to_vector(datasets.load_historical_data()[1],'Class',out_vector='/tmp/LOO.gpkg')
for tr,vl in trvl:
    print(tr,vl)

Out:

Warning : This function generates vector files according to your vector.
        The number of features may differ from the number of pixels used in classification.
        If you want to save every ROI pixels in the vector, please use processing.sample_extraction before.
/tmp/LOO_train_0.gpkg /tmp/LOO_valid_0.gpkg
/tmp/LOO_train_1.gpkg /tmp/LOO_valid_1.gpkg

Plot example on how a polygon was splitted

import ogr
import numpy as np
from matplotlib import pyplot as plt
import matplotlib.path as mpath
import matplotlib.patches as mpatches

# Prepare figure
plt.ioff()
ax=plt.subplot(1,1,1)
ax = plt.gca()


xBounds,yBounds=[[],[]]

for idx,vector in enumerate([tr,vl]):
    # Read all features in layer and store as paths
    ds = ogr.Open(vector)
    lyr = ds.GetLayer(0)

    for feat in lyr:
        paths = []
        codes = []
        all_x = []
        all_y = []

        for geom in feat.GetGeometryRef():
            x = [geom.GetX(j) for j in range(geom.GetPointCount())]
            y = [geom.GetY(j) for j in range(geom.GetPointCount())]
            print(y)
            codes += [mpath.Path.MOVETO] + \
                             (len(x)-1)*[mpath.Path.LINETO]
            all_x += x
            all_y += y
        path = mpath.Path(np.column_stack((all_x,all_y)), codes)
        paths.append(path)

        # Add paths as patches to axes
        for path in paths:
            if idx==0:
                ax.add_patch(mpatches.PathPatch(path,color='C0'))
            else:
                ax.add_patch(mpatches.PathPatch(path,color='C1'))

        xBounds.append([np.min(all_x),np.max(all_x)])
        yBounds.append([np.min(all_y),np.max(all_y)])


ax.set_xlim(np.min(np.array(xBounds)[:,0]),np.max(np.array(xBounds)[:,1]))
ax.set_ylim(np.min(np.array(yBounds)[:,0]),np.max(np.array(yBounds)[:,1]))


legend = [mpatches.Patch(color='C0', label='Train'),mpatches.Patch(color='C1', label='Valid')]
plt.legend(handles=legend)

plt.show()
LeaveOneOutPerClass

Out:

[43.45307126668782, 43.45358183343845, 43.45215976639042, 43.45071681830958, 43.44986486422962, 43.450225598023536, 43.450432409999465, 43.45115102649867, 43.45168310610053, 43.452219308538304, 43.45263958978894, 43.45307126668782]
[43.44370717118851, 43.44469136358583, 43.44509531064528, 43.4452408063241, 43.44449550362173, 43.44368600593977, 43.44293777015743, 43.44370717118851]
[43.45701757265161, 43.45787604460479, 43.457082228059676, 43.45612358357664, 43.45636916875696, 43.45707388717859, 43.45701757265161]
[43.44042241148999, 43.440483448547354, 43.43970544176301, 43.43743842201553, 43.43614203204217, 43.438356822645304, 43.44042241148999]
[43.4318248832688, 43.432060348143885, 43.43163878059781, 43.429995159616546, 43.43004169087143, 43.43094523915542, 43.4318248832688]
[43.43896495888437, 43.4376879011289, 43.436967744374826, 43.437532199809766, 43.43896495888437]
[43.44817537018598, 43.44792595842401, 43.44558787103352, 43.44613275991619, 43.44817537018598]
[43.44045937166199, 43.43947835932513, 43.43720351099278, 43.438029659418305, 43.44045937166199]
[43.429480617832176, 43.43118562632347, 43.43079153485258, 43.42898231904695, 43.428821906220705, 43.42854269176011, 43.429480617832176]
[43.42847738973184, 43.42849867180303, 43.428304533136654, 43.428301356592094, 43.42847738973184]
[43.429628973462655, 43.42850034799008, 43.42717786408072, 43.4283516326417, 43.429628973462655]
[43.44530029440977, 43.443586058600644, 43.44284162971472, 43.44368506299165, 43.44530029440977]
[43.452482102734386, 43.452702877191605, 43.450747798112104, 43.45033001465035, 43.450392291236575, 43.45136142944319, 43.452482102734386]
[43.44698255495887, 43.4476553210238, 43.44800272758978, 43.44618707355676, 43.44604148878518, 43.44477098057019, 43.44414644227549, 43.44489392218304, 43.44590300637205, 43.44629784038614, 43.44698255495887]
[43.45197786107204, 43.45166516190598, 43.45081485681607, 43.45115196410581, 43.451463279488834, 43.45222227187598, 43.45215723741679, 43.45197786107204]
[43.4235100236328, 43.42341331169479, 43.42337663261564, 43.42348062559491, 43.42350618739534, 43.4235100236328]
[43.45372447560291, 43.45300210030046, 43.45168264422287, 43.452705327620265, 43.45372447560291]

Total running time of the script: ( 0 minutes 0.241 seconds)

Gallery generated by Sphinx-Gallery