-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearRegression.cpp
More file actions
57 lines (46 loc) · 1.68 KB
/
LinearRegression.cpp
File metadata and controls
57 lines (46 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <vector>
class LinearRegression {
private:
double weight;
double bias;
double learningRate;
public:
LinearRegression(double lr = 0.01) : weight(0), bias(0), learningRate(lr) {}
// دالة التوقع
double predict(double x) {
return weight * x + bias;
}
// دالة التدريب
void train(const std::vector<double>& X, const std::vector<double>& y, int epochs) {
for (int i = 0; i < epochs; ++i) {
double totalErrorW = 0;
double totalErrorB = 0;
for (size_t j = 0; j < X.size(); ++j) {
double prediction = predict(X[j]);
double error = prediction - y[j];
totalErrorW += error * X[j];
totalErrorB += error;
}
// تحديث المعاملات
weight -= learningRate * (totalErrorW / X.size());
bias -= learningRate * (totalErrorB / X.size());
// طباعة الخطأ في كل حقبة تدريبية
if (i % 100 == 0) {
std::cout << "Epoch " << i << " - Error: " << totalErrorW << std::endl;
}
}
}
};
int main() {
// بيانات تدريب بسيطة (يمكن أن تكون هذه البيانات من عالم حقيقي)
std::vector<double> X = {1, 2, 3, 4, 5};
std::vector<double> y = {2, 4, 6, 8, 10}; // y = 2 * x
// إعداد النموذج
LinearRegression model(0.01);
model.train(X, y, 1000);
// اختبار النموذج
double testValue = 6.0;
std::cout << "Prediction for " << testValue << ": " << model.predict(testValue) << std::endl;
return 0;
}