From a few variables to our first data

From a few variables to our first data#

Before asking a machine to learn, let’s first give it something to learn. Let’s start with two very simple measurements: temperature and humidity.

Our Arduino UNO Q can run Python on Linux. We have already presented the architecture of the board in the Arduino UNO Q section.

For this first experiment, let’s simply use Python to handle a few pieces of data.

  • A few variables
  • Temperature and humidity data
  • A simple rule for making a decision

And that is deliberate.


1. Hello UNO Q!#

Before writing the program, let’s simply describe the algorithm:

Pseudocode
DISPLAY “Hello UNO Q!”

STORE 27 in temperature
STORE 42 in humidity

DISPLAY temperature
DISPLAY humidity

Our first program is particularly simple:

print("Hello UNO Q!")

temperature = 27
humidity = 42

print("Temperature:", temperature, "°C")
print("Humidity:", humidity, "%")

The machine now knows two values:

temperature = 27 °C
humidity    = 42 %

We have simply stored information in variables and displayed it.

For now, the machine observes these values.


2. Let the machine make a decision#

Let’s now add a rule.

Pseudocode
IF temperature is greater than 25
   AND humidity is less than 50
THEN
   decide “water”
ELSE
   decide “do not water”
END IF

DISPLAY the temperature
DISPLAY the humidity
DISPLAY the decision

Let’s ask Python to implement this algorithm:

temperature = 27
humidity = 42

if temperature > 25 and humidity < 50:
    decision = "water"
else:
    decision = "do not water"

print("Temperature:", temperature, "°C")
print("Humidity:", humidity, "%")
print("Decision:", decision)

This time, the program produces a decision.

The rule is simple:

  • temperature > 25
  • humidity < 50
  • decision: water

We wrote this rule ourselves. The machine therefore applies a programmed rule.


3. What if we had several observations?#

Let’s now take several measurements:

observations = [
    (22, 70),
    (24, 65),
    (27, 42),
    (29, 35),
    (25, 55),
]

Each element contains:

(temperature, humidity)

Pseudocode
FOR EACH observation in the observations
   retrieve temperature and humidity

   IF temperature is greater than 25
      AND humidity is less than 50
   THEN
      decide “water”
   ELSE
      decide “do not water”
   END IF

   DISPLAY temperature, humidity and decision
END FOR

Let’s iterate over these observations:

for temperature, humidity in observations:
    if temperature > 25 and humidity < 50:
        decision = "water"
    else:
        decision = "do not water"

    print(
        "Temperature:", temperature,
        "°C | Humidity:", humidity,
        "% | Decision:", decision
    )

We now have a small dataset. The rule is still programmed by us.


4. What if the data could help us?#

Let’s give each observation a label indicating the expected result:

  • [22, 70] → no
  • [24, 65] → no
  • [27, 42] → yes
  • [29, 35] → yes
  • [25, 55] → no

We can represent our dataset like this:

data = [
    ([22, 70], "no"),
    ([24, 65], "no"),
    ([27, 42], "yes"),
    ([29, 35], "yes"),
    ([25, 55], "no"),
]

Pseudocode
FOR EACH observation in the data
   read the features
   read the expected result
END FOR

IDENTIFY the features
IDENTIFY the expected result

We now have the first elements we will encounter throughout Machine Learning:

  • data
  • observations
  • features
  • expected result

The two features are temperature and humidity. The expected result is yes or no.


5. A first calculation from the data#

Let’s look only at temperatures.

For yes answers:

  • 27
  • 29

For no answers:

  • 22
  • 24
  • 25

Let’s calculate the average of each group.

Pseudocode
CREATE a list for “yes” temperatures
CREATE a list for “no” temperatures

FOR EACH observation in the data
   retrieve temperature and label

   IF the label is “yes”
   THEN
      add temperature to the “yes” list
   ELSE
      add temperature to the “no” list
   END IF
END FOR

CALCULATE the average of the “yes” temperatures
CALCULATE the average of the “no” temperatures

CALCULATE the threshold between the two averages

temperatures_yes = []
temperatures_no = []

for features, label in data:
    temperature = features[0]

    if label == "yes":
        temperatures_yes.append(temperature)
    else:
        temperatures_no.append(temperature)

average_yes = sum(temperatures_yes) / len(temperatures_yes)
average_no = sum(temperatures_no) / len(temperatures_no)

threshold = (average_yes + average_no) / 2

The program obtains:

  • Temperatures yes: [27, 29]
  • Temperatures no: [22, 24, 25]
  • Average yes: 28.0
  • Average no: 23.666...
  • Threshold: about 25.83 °C

For a new observation of 28 °C, 40 %, the program compares 28 > 25.83 and predicts yes.


6. Is this already artificial intelligence?#

We are getting closer to Machine Learning.

We used the data to calculate a threshold automatically. The threshold therefore comes from the examples rather than from a value written directly into the rule.

The model remains limited: humidity does not yet participate in the threshold calculation.

Let’s take another step.


7. Toward learned weights#

We have followed this progression:

  • program
  • hand-written rule
  • data
  • calculated threshold

Why not let the model determine the importance of each measurement?

For example:

  • temperature → weight?
  • humidity → weight?

We could then calculate:

Pseudocode
score = temperature × temperature weight
   + humidity × humidity weight
   + bias

This time, we want the model to learn the weights from the data.

If we no longer give the weights directly to the program, how will it manage to find them?