Skip to content

Commit 880a2fd

Browse files
committed
TEMP: runs and values look plausible
1 parent 74cc2c9 commit 880a2fd

2 files changed

Lines changed: 43 additions & 6 deletions

File tree

examples/multiregression_trees/multiregression_run.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def __init__(self, max_trees=10, max_nodes=1000, max_leaves=1000):
2323
def load(self, path):
2424
"""Load a directory of model files"""
2525

26-
for filename in os.listdir(path):
26+
for filename in sorted(os.listdir(path)):
2727
if not filename.endswith('.csv'):
2828
print('Warning: Ignoring unknown file in model directory', filename)
2929
continue
@@ -41,7 +41,7 @@ def load(self, path):
4141
def predict(self, features : array.array, outputs : array.array):
4242
assert len(self.models), 'no models'
4343

44-
for i, model in self.models():
44+
for i, model in enumerate(self.models):
4545
model.predict(features, self._output)
4646
outputs[i] = self._output[0]
4747

@@ -54,14 +54,14 @@ def main():
5454
outputs = array.array('f', [0.0 for _ in range(len(model.models))])
5555

5656
import npyfile
57-
(n_samples, n_features), data = npyfile.load('data.npy')
57+
(n_samples, n_features), data = npyfile.load('input.npy')
5858

5959
# TODO: write output to a file
6060
for row in range(n_samples):
6161
offset = row*n_features
6262
f = data[offset:offset+n_features]
6363
model.predict(f, outputs)
64-
64+
print(f, outputs)
6565

6666

6767
if __name__ == '__main__':

examples/multiregression_trees/multiregression_train.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import urllib.request
1010
import zipfile
1111
import os.path
12+
import tempfile
1213

1314
from sklearn.multioutput import MultiOutputRegressor
1415

@@ -76,6 +77,27 @@ def convert_multiregressor(multi, out_dir, format=None, prefix='regressor', **kw
7677
converted.save(file=p, **kwargs)
7778

7879

80+
def predict(data, model_dir):
81+
"""
82+
Make predictions using MicroPython model
83+
"""
84+
85+
with tempfile.TemporaryDirectory() as temp_dir:
86+
#temp_dir = d.name
87+
temp_dir = '' # XXX: temp
88+
89+
input_path = os.path.join(temp_dir, 'input.npy')
90+
output_path = os.path.join(temp_dir, 'output.npy')
91+
92+
arr = np.ascontiguousarray(data.values).astype(np.int16)
93+
assert len(arr.shape) == 2
94+
np.save(input_path, arr, allow_pickle=False)
95+
96+
#subprocess.check_output()
97+
98+
out = np.load(output_path)
99+
return outs
100+
79101

80102
def main():
81103

@@ -102,12 +124,27 @@ def main():
102124

103125
print("Performance Metrics:")
104126
print("-" * 60)
105-
y_pred = pd.DataFrame(pipeline.predict(X_test), columns=y_train.columns)
127+
128+
129+
y_pred_orig = pd.DataFrame(pipeline.predict(X_test), columns=y_train.columns)
130+
131+
132+
X_test_scaled = pipeline.named_steps['scaler'].transform(X_test)
133+
134+
print(X_test.head())
135+
136+
print(y_pred_orig.head())
137+
138+
y_pred_converted = pd.DataFrame(predict(X_test_scaled, model_dir), columns=y_train.columns)
139+
140+
y_pred = y_pred_converted
141+
106142
for i, target in enumerate(y.columns):
143+
107144
rmse = np.sqrt(mean_squared_error(y_test[target], y_pred[target]))
108145
r2 = r2_score(y_test[target], y_pred[target])
109146
mape = mean_absolute_percentage_error(y_test[target], y_pred[target]) * 100
110-
147+
111148
print(f"{target:12} | RMSE: {rmse:8.3f} | MAPE: {mape:6.2f}% | R²: {r2:6.3f}")
112149

113150
if __name__ == '__main__':

0 commit comments

Comments
 (0)