Scaling sensor data on the device

I’m working on running ML models from TF on microcontrollers. For scaling the data: Is there a way getting the calculated values for ‘mean’ and ‘std’ from the StandardScaler? Or is there an easy way of scaling my sensor data using the same scaling in Arduino code on the device?

Thank you for your advice.

1 Like

I’ve found a solution, if anyone wants to know:

In tensorflow scale the data normally:

from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaler.fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)

To get the mean type: scaler.mean_
To get the std type: scaler.scale_

Copy and paste these figures into your Arduino code using the formula below:

The standard score of a sample x is calculated as:

z = (x - u) / s

where u is the mean of the training samples or zero if with_mean=False, and s is the standard deviation of the training samples or one if with_std=False.

Example code below:

float Sensor1 = (sensorReading1 - mean_) / scale_;

1 Like