When exploring machine learning, followers usually encounter fairly a number of sources centered on high-level languages resembling Python. Nonetheless, for these intrigued by the effectivity and vitality of C, the journey takes a specific path. C, known as a middle-level language, strikes a steadiness between the simplicity of high-level programming and the robust memory manipulation capabilities of low-level languages. This distinctive combination makes C an exquisite different for implementing machine learning algorithms, providing every precision and administration over the computational processes.

However, smart implementations of machine learning and deep learning algorithms in C may be elusive. On this text, we embark on an thrilling exploration of a simple linear regression model to predict scholar effectivity based mostly totally on study hours using C, strolling through the strategy step-by-step. This hands-on methodology will not solely deepen your understanding however moreover showcase the flexibleness and robustness of C throughout the realm of machine learning.

**Situations:**

- Basic understanding of Programming in C language.
- Basic understanding of Algebra.
- Basic understanding of Calculus (i.e derivatives)

**Understanding Linear Regression**

Linear regression fashions the connection between two variables, the place one (dependent variable) is predicted from the other (neutral variable) through a linear equation. In our case, the equation is represented as `y = wx + b`

, the place:

`y`

is the anticipated effectivity,`x`

is the number of study hours,`w`

is the burden (slope of the highway),`b`

is the bias (y-intercept).

**Step-by-Step Implementation**

- Initialization and Info Definition:

`static double X[] = {2, 4, 6, 8}; // Unbiased variable (study hours)`

static double y[] = {20, 40, 60, 80}; // Dependent variable (effectivity marks)

static double weight = 0;

static double bias = 0;

Proper right here, `X`

and `y`

characterize our teaching information. `weight`

and `bias`

are parameters initialized to zero, which the model might be taught all through teaching.

2.Prediction Carry out:

`static double* predict(double inputs[], int measurement, double weight, double bias) {`

double* y_predicted = (double*)malloc(measurement * sizeof(double));

// Calculate predictions y = wx + b

for (int i = 0; i < measurement; i++) {

y_predicted[i] = inputs[i] * weight + bias;

}

return y_predicted;

}

This carry out computes predicted values (`y_predicted`

) based mostly totally on current `weight`

and `bias`

.

3. Worth Carry out:

The payment carry out measures the error between the anticipated values and the exact values. On this case, we use the Suggest Squared Error (MSE).

The place:

- J(w,b) is the related payment.
- m is the number of information components.
- y^i is the anticipated price for the i-th information degree.
- yi is the exact price for the i-th information degree.

`static double value(double inputs[], double labels[], int measurement, double weight, double bias) {`

double loss_value = 0;

double sum_loss = 0;

double* y_predicted = predict(inputs, measurement, weight, bias);for (int i = 0; i < measurement; i++) {

loss_value = (labels[i] - y_predicted[i]) * (labels[i] - y_predicted[i]);

sum_loss += loss_value;

}

free(y_predicted);

return sum_loss / (2 * measurement);

}

Proper right here, `value`

carry out computes the indicate squared error, which quantifies the excellence between exact `labels`

and predicted `y_predicted`

.

4. Gradient Capabilities for Optimization:

*Gradients of Weights*

The gradient of the burden is the partial spinoff of the related payment carry out with respect to the burden. It signifies how rather a lot the burden must be adjusted to chop again the related payment.

*C Code Implementation*

`static double weight_grad(double inputs[], double labels[], int measurement) {`

double grad = 0;

double* y_predicted = predict(inputs, measurement, weight, bias);for (int i = 0; i < measurement; i++) {

grad += (y_predicted[i] - labels[i]) * inputs[i];

}

free(y_predicted);

return grad / measurement;

}

*Gradients of Bias*

The gradient of the bias is the partial spinoff of the related payment carry out with respect to the bias. It signifies how rather a lot the bias must be adjusted to chop again the related payment.

*C Code Implementation*

`static double bias_grad(double inputs[], double labels[], int measurement) {`

double grad = 0;

double* y_predicted = predict(inputs, measurement, weight, bias);for (int i = 0; i < measurement; i++) {

grad += (y_predicted[i] - labels[i]);

}

free(y_predicted);

return grad / measurement;

}

These options compute gradients (derivatives) of the related payment carry out with respect to `weight`

and `bias`

, important for updating these parameters all through teaching.

5. Teaching and Testing:

All through teaching, we change the burden and bias iteratively using the gradients. The coaching value controls the size of the steps we absorb course of minimizing the related payment.

Weight change:

Bias change:

The place:

- α is the coaching value.
- ∂J(w,b)/∂w is the gradient of the related payment carry out J(w,b) with respect to the burden w.
- ∂J(w,b)/∂b is the gradient of the related payment carry out J(w,b) with respect to the bias b.

*C Code Implementation*

`void test() {`

int measurement;printf("Let's test our linear modeln");

printf("n");

printf("Enter the size of your information (Number of information components):n");

scanf("%d", &measurement);

printf("n");

double inputs[size];

for (int i = 0; i < measurement; i++) {

printf("Enter number of hour(s) for information : %d n", i + 1);

scanf("%lf", &inputs[i]);

}

printf("n");

double* predictions = predict(inputs, measurement, weight, bias);

printf("Prediction for inputsnn");

for (int i = 0; i < measurement; i++) {

printf("%lf hrs : %lf marks(performances)n", inputs[i], predictions[i]);

}

free(predictions);

}

int most essential(void) {

int epoch = 100000;

double learning_rate = 0.0001;

int measurement = sizeof(X) / sizeof(X[0]);

double loss = 0;

double grad_w = 0;

double grad_b = 0;

for (int i = 1; i <= epoch; i++) {

loss = value(X, y, measurement, weight, bias);

grad_w = weight_grad(X, y, measurement);

grad_b = bias_grad(X, y, measurement);

weight = weight - learning_rate * grad_w;

bias = bias - learning_rate * grad_b;

printf("Epoch %d ---- Loss: %lf n", i, loss);

printf("Weight: %lf, Bias: %lf, Grad_W: %lf, Grad_B: %lfn", weight, bias, grad_w, grad_b);

}

printf("n");

printf("Model Loss: %lf n", loss);

printf("Optimum Weight: %lf n", weight);

printf("Optimum Bias: %lf n", bias);

printf("n");

test();

}

This `most essential`

carry out initializes parameters, trains the model using gradient descent, and evaluates its effectivity using value carry out, throughout the subsequent half we’re going to uncover completely different evaluation metrics. The test carry out is used to test how the model is able to appropriately predict the effectivity from the hours equipped by particular person.

Output

**Conclusion:**

Implementing machine learning algorithms in C provides a deeper understanding of their inside workings and computational effectivity. This occasion demonstrates the equipment of linear regression, from information illustration to parameter optimization, in C. By leveraging low-level languages, followers can enhance their programming experience and grasp the foundations of machine learning algorithms additional comprehensively.

By mastering such implementations, builders can leverage the robustness of low-level languages to type out superior machine learning challenges efficiently.

Hyperlinks:

github: https://github.com/nsomazr/ml-in-c/blob/main/linear-regression/example_linear_model.c