Upload Python script and training data used to create model
rodzic
622e00990f
commit
04eb63b107
|
@ -0,0 +1,44 @@
|
||||||
|
# Train a machine-learning model to classify Morse code input
|
||||||
|
#
|
||||||
|
# Requirements
|
||||||
|
# - SciKit-Learn to create the model (tested with v1.2.0)
|
||||||
|
# - MicroMLGen to export it in Arduino-usable C code (tested with v1.1.28)
|
||||||
|
#
|
||||||
|
# These dependencies can be installed from command-line as follows:
|
||||||
|
# pip install -U scikit-learn
|
||||||
|
# pip install micromlgen
|
||||||
|
|
||||||
|
# For loading training dataset from .csv files
|
||||||
|
import numpy as np
|
||||||
|
from glob import glob
|
||||||
|
from os.path import basename
|
||||||
|
# Import chosen classifier function
|
||||||
|
# (for alternatives see https://github.com/eloquentarduino/micromlgen)
|
||||||
|
from sklearn.ensemble import RandomForestClassifier
|
||||||
|
# For exporting model to Arduino C code
|
||||||
|
from micromlgen import port
|
||||||
|
|
||||||
|
# Load training dataset from csv files
|
||||||
|
def load_features(folder):
|
||||||
|
dataset = None
|
||||||
|
classmap = {}
|
||||||
|
for class_idx, filename in enumerate(glob('%s/*.csv' % folder)):
|
||||||
|
class_name = basename(filename)[:-4]
|
||||||
|
classmap[class_idx] = class_name
|
||||||
|
samples = np.loadtxt(filename, dtype=float, delimiter=',')
|
||||||
|
labels = np.ones((len(samples), 1)) * class_idx
|
||||||
|
samples = np.hstack((samples, labels))
|
||||||
|
dataset = samples if dataset is None else np.vstack((dataset, samples))
|
||||||
|
return dataset, classmap
|
||||||
|
|
||||||
|
# Load data, apply classifier, output as Arduino format
|
||||||
|
if __name__ == '__main__':
|
||||||
|
# Load training data from the specified subfolder
|
||||||
|
features, classmap = load_features('training_data')
|
||||||
|
# Create classifier function from feature set
|
||||||
|
X, y = features[:, :-1], features[:, -1]
|
||||||
|
classifier = RandomForestClassifier(20, max_depth=10).fit(X, y)
|
||||||
|
# Use MicroMLgen to port classifier to Arduino C-code
|
||||||
|
c_code = port(classifier, classmap=classmap)
|
||||||
|
# Show generated code
|
||||||
|
print(c_code)
|
|
@ -0,0 +1,13 @@
|
||||||
|
159.00,832.00,0.00,0.00
|
||||||
|
244.00,923.00,0.00,0.00
|
||||||
|
122.00,817.00,0.00,0.00
|
||||||
|
125.00,801.00,0.00,0.00
|
||||||
|
73.00,922.00,0.00,0.00
|
||||||
|
118.00,545.00,0.00,0.00
|
||||||
|
116.00,555.00,0.00,0.00
|
||||||
|
254.00,1040.00,0.00,0.00
|
||||||
|
124.00,666.00,0.00,0.00
|
||||||
|
135.00,803.00,0.00,0.00
|
||||||
|
111.00,1007.00,0.00,0.00
|
||||||
|
160.00,880.00,0.00,0.00
|
||||||
|
139.00,830.00,0.00,0.00
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
538.00,191.00,183.00,193.00
|
||||||
|
773.00,190.00,178.00,195.00
|
||||||
|
519.00,173.00,171.00,168.00
|
||||||
|
1185.00,279.00,358.00,372.00
|
||||||
|
941.00,135.00,161.00,188.00
|
||||||
|
954.00,160.00,183.00,177.00
|
||||||
|
721.00,176.00,199.00,208.00
|
||||||
|
429.00,133.00,128.00,168.00
|
||||||
|
338.00,130.00,126.00,134.00
|
||||||
|
361.00,101.00,111.00,130.00
|
||||||
|
445.00,96.00,103.00,93.00
|
||||||
|
446.00,98.00,107.00,134.00
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
285.00,116.00,377.00,147.00
|
||||||
|
264.00,98.00,347.00,145.00
|
||||||
|
346.00,147.00,449.00,175.00
|
||||||
|
330.00,126.00,363.00,172.00
|
||||||
|
380.00,157.00,482.00,161.00
|
||||||
|
336.00,148.00,414.00,182.00
|
||||||
|
427.00,134.00,363.00,142.00
|
||||||
|
545.00,253.00,610.00,208.00
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
314.00,117.00,124.00,0.00
|
||||||
|
333.00,98.00,125.00,0.00
|
||||||
|
389.00,70.00,118.00,0.00
|
||||||
|
438.00,139.00,130.00,0.00
|
||||||
|
492.00,110.00,131.00,0.00
|
||||||
|
424.00,136.00,153.00,0.00
|
||||||
|
486.00,136.00,142.00,0.00
|
||||||
|
631.00,105.00,114.00,0.00
|
||||||
|
366.00,155.00,180.00,0.00
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
136.00,0.00,0.00,0.00
|
||||||
|
105.00,0.00,0.00,0.00
|
||||||
|
108.00,0.00,0.00,0.00
|
||||||
|
121.00,0.00,0.00,0.00
|
||||||
|
138.00,0.00,0.00,0.00
|
||||||
|
103.00,0.00,0.00,0.00
|
||||||
|
59.00,0.00,0.00,0.00
|
||||||
|
100.00,0.00,0.00,0.00
|
||||||
|
34.00,0.00,0.00,0.00
|
||||||
|
79.00,0.00,0.00,0.00
|
||||||
|
103.00,0.00,0.00,0.00
|
||||||
|
85.00,0.00,0.00,0.00
|
||||||
|
113.00,0.00,0.00,0.00
|
||||||
|
103.00,0.00,0.00,0.00
|
||||||
|
63.00,0.00,0.00,0.00
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
138.00,139.00,480.00,151.00
|
||||||
|
101.00,131.00,612.00,137.00
|
||||||
|
151.00,151.00,402.00,210.00
|
||||||
|
145.00,146.00,367.00,214.00
|
||||||
|
118.00,142.00,377.00,152.00
|
||||||
|
151.00,131.00,428.00,180.00
|
||||||
|
143.00,105.00,428.00,173.00
|
||||||
|
128.00,236.00,532.00,187.00
|
||||||
|
182.00,158.00,556.00,188.00
|
||||||
|
147.00,178.00,447.00,217.00
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
238.00,289.00,137.00,0.00
|
||||||
|
365.00,318.00,151.00,0.00
|
||||||
|
408.00,253.00,126.00,0.00
|
||||||
|
361.00,331.00,166.00,0.00
|
||||||
|
394.00,327.00,80.00,0.00
|
||||||
|
417.00,297.00,122.00,0.00
|
||||||
|
396.00,409.00,136.00,0.00
|
||||||
|
393.00,411.00,118.00,0.00
|
||||||
|
467.00,427.00,150.00,0.00
|
||||||
|
477.00,444.00,142.00,0.00
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
127.00,124.00,125.00,137.00
|
||||||
|
92.00,112.00,113.00,139.00
|
||||||
|
123.00,139.00,119.00,130.00
|
||||||
|
118.00,123.00,112.00,131.00
|
||||||
|
119.00,110.00,122.00,135.00
|
||||||
|
72.00,46.00,50.00,87.00
|
||||||
|
75.00,54.00,31.00,93.00
|
||||||
|
119.00,107.00,89.00,110.00
|
||||||
|
105.00,84.00,87.00,117.00
|
||||||
|
54.00,78.00,63.00,116.00
|
||||||
|
82.00,77.00,67.00,69.00
|
||||||
|
86.00,67.00,59.00,97.00
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
122.00,122.00,0.00,0.00
|
||||||
|
114.00,117.00,0.00,0.00
|
||||||
|
113.00,96.00,0.00,0.00
|
||||||
|
123.00,138.00,0.00,0.00
|
||||||
|
69.00,104.00,0.00,0.00
|
||||||
|
114.00,135.00,0.00,0.00
|
||||||
|
112.00,107.00,0.00,0.00
|
||||||
|
99.00,112.00,0.00,0.00
|
||||||
|
111.00,117.00,0.00,0.00
|
||||||
|
104.00,114.00,0.00,0.00
|
||||||
|
144.00,129.00,0.00,0.00
|
||||||
|
110.00,106.00,0.00,0.00
|
||||||
|
105.00,138.00,0.00,0.00
|
||||||
|
212.00,227.00,0.00,0.00
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
143.00,403.00,271.00,243.00
|
||||||
|
123.00,372.00,290.00,304.00
|
||||||
|
95.00,358.00,247.00,246.00
|
||||||
|
135.00,388.00,273.00,277.00
|
||||||
|
163.00,406.00,350.00,301.00
|
||||||
|
90.00,387.00,399.00,431.00
|
||||||
|
136.00,436.00,417.00,480.00
|
||||||
|
103.00,412.00,388.00,451.00
|
||||||
|
121.00,441.00,423.00,487.00
|
||||||
|
113.00,443.00,437.00,462.00
|
||||||
|
115.00,367.00,449.00,501.00
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
419.00,145.00,487.00,0.00
|
||||||
|
465.00,185.00,477.00,0.00
|
||||||
|
285.00,117.00,425.00,0.00
|
||||||
|
337.00,120.00,400.00,0.00
|
||||||
|
363.00,159.00,500.00,0.00
|
||||||
|
385.00,140.00,454.00,0.00
|
||||||
|
415.00,150.00,542.00,0.00
|
||||||
|
388.00,138.00,549.00,0.00
|
||||||
|
472.00,149.00,616.00,0.00
|
||||||
|
416.00,189.00,560.00,0.00
|
||||||
|
387.00,152.00,477.00,0.00
|
||||||
|
441.00,276.00,658.00,0.00
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
127.00,521.00,138.00,170.00
|
||||||
|
110.00,499.00,142.00,201.00
|
||||||
|
104.00,477.00,119.00,120.00
|
||||||
|
104.00,495.00,111.00,144.00
|
||||||
|
131.00,565.00,126.00,148.00
|
||||||
|
133.00,361.00,119.00,127.00
|
||||||
|
97.00,427.00,133.00,142.00
|
||||||
|
120.00,411.00,128.00,139.00
|
||||||
|
79.00,442.00,65.00,117.00
|
||||||
|
149.00,427.00,43.00,80.00
|
||||||
|
47.00,447.00,121.00,142.00
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
315.00,562.00,0.00,0.00
|
||||||
|
359.00,404.00,0.00,0.00
|
||||||
|
398.00,413.00,0.00,0.00
|
||||||
|
393.00,559.00,0.00,0.00
|
||||||
|
448.00,566.00,0.00,0.00
|
||||||
|
426.00,597.00,0.00,0.00
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
423.00,156.00,0.00,0.00
|
||||||
|
519.00,149.00,0.00,0.00
|
||||||
|
457.00,134.00,0.00,0.00
|
||||||
|
400.00,104.00,0.00,0.00
|
||||||
|
386.00,107.00,0.00,0.00
|
||||||
|
386.00,123.00,0.00,0.00
|
||||||
|
388.00,89.00,0.00,0.00
|
||||||
|
418.00,78.00,0.00,0.00
|
||||||
|
350.00,158.00,0.00,0.00
|
||||||
|
383.00,163.00,0.00,0.00
|
||||||
|
424.00,135.00,0.00,0.00
|
||||||
|
380.00,114.00,0.00,0.00
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
299.00,331.00,360.00,0.00
|
||||||
|
332.00,274.00,533.00,0.00
|
||||||
|
332.00,340.00,468.00,0.00
|
||||||
|
343.00,341.00,539.00,0.00
|
||||||
|
326.00,351.00,463.00,0.00
|
||||||
|
285.00,336.00,401.00,0.00
|
||||||
|
224.00,276.00,387.00,0.00
|
||||||
|
338.00,315.00,405.00,0.00
|
||||||
|
326.00,376.00,496.00,0.00
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
131.00,488.00,502.00,120.00
|
||||||
|
41.00,481.00,452.00,153.00
|
||||||
|
126.00,453.00,402.00,126.00
|
||||||
|
146.00,461.00,430.00,101.00
|
||||||
|
115.00,475.00,417.00,135.00
|
||||||
|
123.00,423.00,406.00,108.00
|
||||||
|
161.00,444.00,492.00,125.00
|
||||||
|
136.00,440.00,372.00,128.00
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
420.00,451.00,155.00,618.00
|
||||||
|
412.00,423.00,168.00,575.00
|
||||||
|
371.00,323.00,85.00,601.00
|
||||||
|
358.00,438.00,141.00,451.00
|
||||||
|
316.00,387.00,131.00,499.00
|
||||||
|
439.00,387.00,127.00,508.00
|
||||||
|
390.00,388.00,133.00,418.00
|
||||||
|
379.00,372.00,136.00,476.00
|
||||||
|
551.00,540.00,123.00,595.00
|
||||||
|
518.00,552.00,183.00,628.00
|
||||||
|
538.00,473.00,163.00,567.00
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
85.00,605.00,151.00,0.00
|
||||||
|
129.00,478.00,355.00,0.00
|
||||||
|
98.00,418.00,101.00,0.00
|
||||||
|
116.00,515.00,50.00,0.00
|
||||||
|
85.00,440.00,209.00,0.00
|
||||||
|
85.00,501.00,143.00,0.00
|
||||||
|
115.00,502.00,184.00,0.00
|
||||||
|
112.00,367.00,113.00,0.00
|
||||||
|
152.00,555.00,166.00,0.00
|
||||||
|
107.00,442.00,121.00,0.00
|
||||||
|
123.00,449.00,99.00,0.00
|
||||||
|
114.00,490.00,150.00,0.00
|
||||||
|
103.00,438.00,126.00,0.00
|
||||||
|
98.00,443.00,265.00,0.00
|
||||||
|
124.00,459.00,126.00,0.00
|
||||||
|
87.00,401.00,127.00,0.00
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
143.00,139.00,134.00,0.00
|
||||||
|
124.00,135.00,139.00,0.00
|
||||||
|
116.00,126.00,136.00,0.00
|
||||||
|
77.00,85.00,106.00,0.00
|
||||||
|
69.00,88.00,111.00,0.00
|
||||||
|
103.00,87.00,91.00,0.00
|
||||||
|
98.00,101.00,103.00,0.00
|
||||||
|
88.00,103.00,116.00,0.00
|
||||||
|
111.00,88.00,103.00,0.00
|
||||||
|
100.00,110.00,117.00,0.00
|
||||||
|
112.00,96.00,93.00,0.00
|
||||||
|
119.00,95.00,116.00,0.00
|
||||||
|
102.00,123.00,145.00,0.00
|
||||||
|
135.00,141.00,138.00,0.00
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
494.00,0.00,0.00,0.00
|
||||||
|
542.00,0.00,0.00,0.00
|
||||||
|
439.00,0.00,0.00,0.00
|
||||||
|
518.00,0.00,0.00,0.00
|
||||||
|
424.00,0.00,0.00,0.00
|
||||||
|
532.00,0.00,0.00,0.00
|
||||||
|
479.00,0.00,0.00,0.00
|
||||||
|
659.00,0.00,0.00,0.00
|
||||||
|
538.00,0.00,0.00,0.00
|
||||||
|
492.00,0.00,0.00,0.00
|
||||||
|
398.00,0.00,0.00,0.00
|
||||||
|
530.00,0.00,0.00,0.00
|
||||||
|
539.00,0.00,0.00,0.00
|
||||||
|
495.00,0.00,0.00,0.00
|
||||||
|
466.00,0.00,0.00,0.00
|
||||||
|
435.00,0.00,0.00,0.00
|
||||||
|
441.00,0.00,0.00,0.00
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
134.00,129.00,429.00,0.00
|
||||||
|
118.00,105.00,486.00,0.00
|
||||||
|
123.00,131.00,536.00,0.00
|
||||||
|
108.00,137.00,474.00,0.00
|
||||||
|
120.00,86.00,477.00,0.00
|
||||||
|
111.00,114.00,524.00,0.00
|
||||||
|
113.00,138.00,396.00,0.00
|
||||||
|
108.00,106.00,552.00,0.00
|
||||||
|
102.00,115.00,341.00,0.00
|
||||||
|
122.00,123.00,444.00,0.00
|
||||||
|
118.00,138.00,388.00,0.00
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
117.00,116.00,117.00,563.00
|
||||||
|
110.00,113.00,66.00,470.00
|
||||||
|
101.00,101.00,85.00,487.00
|
||||||
|
40.00,133.00,105.00,448.00
|
||||||
|
115.00,119.00,118.00,558.00
|
||||||
|
136.00,125.00,119.00,590.00
|
||||||
|
69.00,121.00,117.00,504.00
|
||||||
|
128.00,112.00,121.00,576.00
|
||||||
|
135.00,122.00,108.00,462.00
|
||||||
|
113.00,125.00,129.00,571.00
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
111.00,634.00,557.00,0.00
|
||||||
|
123.00,523.00,497.00,0.00
|
||||||
|
108.00,523.00,493.00,0.00
|
||||||
|
116.00,548.00,476.00,0.00
|
||||||
|
101.00,367.00,270.00,0.00
|
||||||
|
66.00,455.00,481.00,0.00
|
||||||
|
113.00,479.00,505.00,0.00
|
||||||
|
110.00,512.00,513.00,0.00
|
||||||
|
115.00,496.00,540.00,0.00
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
432.00,121.00,138.00,535.00
|
||||||
|
447.00,108.00,106.00,534.00
|
||||||
|
592.00,121.00,130.00,598.00
|
||||||
|
577.00,115.00,120.00,586.00
|
||||||
|
531.00,47.00,102.00,630.00
|
||||||
|
486.00,102.00,102.00,569.00
|
||||||
|
353.00,87.00,109.00,426.00
|
||||||
|
337.00,106.00,102.00,513.00
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
440.00,84.00,521.00,545.00
|
||||||
|
365.00,93.00,361.00,342.00
|
||||||
|
322.00,113.00,354.00,430.00
|
||||||
|
316.00,114.00,335.00,410.00
|
||||||
|
323.00,98.00,333.00,308.00
|
||||||
|
268.00,104.00,302.00,389.00
|
||||||
|
257.00,95.00,315.00,287.00
|
||||||
|
316.00,111.00,318.00,351.00
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
358.00,399.00,104.00,101.00
|
||||||
|
409.00,364.00,83.00,75.00
|
||||||
|
379.00,384.00,93.00,96.00
|
||||||
|
406.00,368.00,81.00,106.00
|
||||||
|
311.00,350.00,90.00,106.00
|
||||||
|
365.00,345.00,89.00,119.00
|
||||||
|
452.00,446.00,88.00,87.00
|
||||||
|
333.00,329.00,108.00,135.00
|
||||||
|
308.00,329.00,91.00,131.00
|
|
Ładowanie…
Reference in New Issue