Supervised_machine_learning_0.pdf (123891Bytes)
For better readability I have attached a pdf, which explains how linear regression is used for supervised machine learning. In this paper I also derived the general equation of linear regression. You do not need to understand the derivation (but you should) to use linear regression. Mathematically a single artificial neuron (perceptron) is just a special case of linear regression.
You can use linear regression to approximate linear or close to linear functions. It can be for example used for color or other pattern recognition. Color recognition is even for an human no easy task. My 3 years old daughter still can't distinguish between all colors. Yellow, orange and pink objects are still just red in her opinion. I might update this entry showing an actual example using a color sensor, if you are interested. Arduino sample code below.
1 // Linear regression calculator, written by Markus Bindhammer
2 // Online linear regression calculator:
3 // http://www.alcula.com/calculators/statistics/linear-regression/
4
5 int n = 0;
6 float x_n[100];
7 float y_n[100];
8
9 void setup() {
10 Serial.begin(9600);
11 }
12
13 void loop() {
14 Serial.println("Submit x-value or 'ESC'");
15 char input_x[50];
16 get_input(input_x, 50);
17 if (strcmp (input_x, "ESC") == 0 && n > 0) {
18 // now we ask for x_n+1
19 Serial.println("");
20 Serial.println("Submit x_n+1-value");
21 char input_xnpo[50];
22 get_input(input_xnpo, 50);
23 float xnpo_value = atof(input_xnpo);
24 Serial.print("x_");
25 Serial.print(n);
26 Serial.print(" = ");
27 Serial.println(xnpo_value, 4);
28 Serial.println("");
29 // now we compute y_n+1
30 float SUM_A = 0.0;
31 float SUM_B = 0.0;
32 float X = 0.0;
33 float Y = 0.0;
34 for (int i = 0; i < n; i ++) {
35 SUM_A = SUM_A + (x_n[i] * y_n[i]);
36 SUM_B = SUM_B + pow(x_n[i], 2.0);
37 X = X + x_n[i];
38 Y = Y + y_n[i];
39 }
40 float floatn = float(n);
41 X = X / floatn;
42 Y = Y / floatn;
43 float ynpo = Y - (((SUM_A - (floatn * X * Y))/(SUM_B - floatn * pow(X, 2.0))) * (X - xnpo_value));
44 Serial.print("y_n+1 = ");
45 Serial.println(ynpo, 4);
46 Serial.println("");
47 n = 0;
48 } else {
49 float x_value = atof(input_x);
50 x_n[n] = x_value;
51 Serial.print("x_");
52 Serial.print(n);
53 Serial.print(" = ");
54 Serial.println(x_value, 4);
55 Serial.println("");
56 Serial.println("Submit y-value");
57 char input_y[50];
58 get_input(input_y, 50);
59 float y_value = atof(input_y);
60 y_n[n] = y_value;
61 Serial.print("y_");
62 Serial.print(n);
63 Serial.print(" = ");
64 Serial.println(y_value, 4);
65 Serial.println("");
66 n ++;
67 }
68 }
69
70 void get_input(char pd[], int pd_size) {
71 while (Serial.available() == 0){}
72 String inputBuffer;
73 while (Serial.available() > 0) {
74 char ch = Serial.read();
75 inputBuffer += ch;
76 delay (10);
77 }
78 if (inputBuffer.length() > 0) {
79 inputBuffer.toCharArray(pd, pd_size);
80 }
81 }