# How to use classify to separate lines and circles

I generated lines and circles:

lines = Table[
Graphics[
Line[{{RandomInteger[{0, 10}],
RandomInteger[{0, 10}]}, {RandomInteger[{0, 10}],
RandomInteger[{0, 10}]}}], ImageSize -> 10], {x, 1, 20}];
circles =
Table[Graphics[
Circle[{RandomInteger[{0, 10}], RandomInteger[{0, 10}]},
RandomInteger[{0, 20}]], ImageSize -> 10], {x, 1, 20}];


and put them into a classifier

c = Classify[{lines -> "lines", circles -> "circles"}]


the training was successful with no errors, but when trying to test the classifier with:

test = Graphics[Line[{{0, 1}, {0, 2}}], ImageSize -> 10]
c[test]


I get the error:

ClassifierFunction::mlbddataev: The data being evaluated is not formatted correctly.

And I do not understand what the problem is. Can somebody tell me, how to correctly format the data?

This is the way I would do it.

• Data needs to be Association, not list, when you label by the whole batches and not single objects.
• A good training set has varying data, not almost identical. Hence, carefully design Graphics so circles and lines are different sizes and in different places. You can also add noise (like random points), but I did not do it for simplicity.
• Experiment with larger training sets to see improvement of accuracy.
• PerformanceGoal->"Quality" is recommended for more accurate results.
• This case works out well with Graphics but in more complex cases you can always consider converting Graphics to Image with Rasterize and using Neural Networks.

Training set:

lines = ParallelTable[Graphics[{Thick,Line[RandomReal[10,{2,2}]]},
ImageSize -> 50,PlotRange->{{0,10},{0,10}}], {x, 1, 100}];

circles = ParallelTable[Graphics[{Thick,Circle[RandomReal[{3,7},2], RandomReal[{1,3}]]},
ImageSize -> 50,PlotRange->{{0,10},{0,10}}], {x, 1, 100}];


Here is a sample (frames are only to stress varying locations of lines and circles within the Graphics):

Framed /@ RandomSample[lines~Join~circles, 10]


Train:

c=Classify[<|"lines"->lines,"circles"->circles|>,PerformanceGoal->"Quality"]

Information[c]


Test set:

linesTEST=ParallelTable[Graphics[{Thick,Line[RandomReal[10,{2,2}]]},
ImageSize -> 100,PlotRange->{{0,10},{0,10}}], {x, 1, 20}];

circlesTEST = ParallelTable[Graphics[{Thick,Circle[RandomReal[{3,7},2], RandomReal[{1,3}]]},
ImageSize -> 50,PlotRange->{{0,10},{0,10}}], {x, 1, 20}];


Measure performance:

cm=ClassifierMeasurements[c,<|"lines"->linesTEST,"circles"->circlesTEST|>]

cm["ConfusionMatrixPlot"]


• c = Classify[<|"lines" -> lines, "circles" -> circles|>, PerformanceGoal -> "Quality"] does not work for me, outputting "ClassifierFunction[Input type: Image Classes: circles,lines Data not in notebook; Store now ». ]". Oct 19, 2020 at 11:41
• @user64494 it is not an error. Oct 19, 2020 at 13:13
• @Vtaliy Kaurov: You wrote "it is not an error". However, after that the rest of your code does not work for me in 12.1.1.0 on Windows 10, resultng in "ClassifierMeasurementsObject[Classifier: LogisticRegression Number of test examples: 40 Data not in notebook; Store now » ]". Oct 19, 2020 at 13:22
• Works for me in 12.1.1.0 on Windows 10. I also get "Data not in notebook; Store now" in the ClassifierFunction and that's not an error. Get the same results as the answer. @user64494 Oct 19, 2020 at 14:15
• Thank you, I was not aware of the use of Associations in this case. But it works well for me! Oct 20, 2020 at 7:39