Select to view content in your preferred language

Randomly split the polygon layer in a ratio of 80% to 20%.

1098
2
Jump to solution
08-23-2022 06:39 AM
JúliaMatejčíková
New Contributor

Hi all, Im working with ArcGIS Pro 3.0.0. I have polygon layer with 5400 attributes, and I need  to split them randomly in ration of 80% to 20%, so than I will have two polygon layers with 4320 attributes and 1080.

What can I use for the random split?

Thank you all!

Have a nice day.

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Alum

This Python script will create 2 layers (named "20%" and "80%") in your ArcGIS Pro map. These show the input features, but with a definition query. If you want to save those subsets, right-click on the layers and export data.

Copy/paste the script into the Python window, edit the in_features variable, run it.

in_features = "EZG"  # path to the featureclass or name of the layer

import random
# get all objectids
oids = [row[0] for row in arcpy.da.SearchCursor(in_features, ["OBJECTID"])]
# get a random sample
oids_80 = random.sample(oids, int(0.8 * len(oids)))
# create the layers
arcpy.management.MakeFeatureLayer(in_features, "80%", f"OBJECTID IN {tuple(oids_80)}")
arcpy.management.MakeFeatureLayer(in_features, "20%", f"OBJECTID NOT IN {tuple(oids_80)}")

Have a great day!
Johannes

View solution in original post

2 Replies
JohannesLindner
MVP Alum

This Python script will create 2 layers (named "20%" and "80%") in your ArcGIS Pro map. These show the input features, but with a definition query. If you want to save those subsets, right-click on the layers and export data.

Copy/paste the script into the Python window, edit the in_features variable, run it.

in_features = "EZG"  # path to the featureclass or name of the layer

import random
# get all objectids
oids = [row[0] for row in arcpy.da.SearchCursor(in_features, ["OBJECTID"])]
# get a random sample
oids_80 = random.sample(oids, int(0.8 * len(oids)))
# create the layers
arcpy.management.MakeFeatureLayer(in_features, "80%", f"OBJECTID IN {tuple(oids_80)}")
arcpy.management.MakeFeatureLayer(in_features, "20%", f"OBJECTID NOT IN {tuple(oids_80)}")

Have a great day!
Johannes
JúliaMatejčíková
New Contributor

Thank you! It works.

 

Have a nice day!

Júlia

0 Kudos