### MQL5: Perform Singular Value Decomposition (SVD) Source: https://github.com/megajoctan/male5/wiki/Linear-Algebra-Library Shows an example of performing Singular Value Decomposition (SVD) on a matrix M using the LinAlg::svd function from the linalg.mqh library. It decomposes M into U, V, and singular_values. The code checks for the success of the SVD operation. ```mql5 matrix M = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; matrix U, V; vector singular_values; bool svd_success = LinAlg::svd(M, U, V, singular_values); if (svd_success) { Print("Singular value decomposition successful:"); // Use the decomposed components (U, V, singular_values) for further calculations } else { Print("SVD failed!"); } ``` -------------------------------- ### MQL5 Kernel Function Implementations Source: https://github.com/megajoctan/male5/wiki/Kernels-Library This snippet shows an example of how to instantiate and use the __kernels__ class from the kernels.mqh library in MQL5. It demonstrates initializing the RBF kernel with a specific sigma value and then calculating a kernel matrix between two data matrices. The resulting kernel matrix can be used for further machine learning tasks. ```mql5 // Example: Using the RBF kernel __kernels__ kernel(KERNEL_RADIAL_BASIS_FUNCTION_RBF, sigma=0.5); // Set sigma for RBF kernel matrix data1, data2; // ... populate data matrices ... matrix kernel_matrix = kernel.KernelFunction(data1, data2); // Use the kernel matrix for further machine learning tasks (e.g., SVM training) ``` -------------------------------- ### MQL5 K-Fold Cross-Validation Usage Example Source: https://github.com/megajoctan/male5/wiki/Cross-Validation-Library Example of how to use the CCrossValidation class in MQL5 to perform K-Fold Cross-Validation. Demonstrates initializing the class, calling KFoldCV, accessing fold data, and optional memory cleanup. ```MQL5 // Example: Performing K-Fold Cross-Validation (K=5) matrix my_data; // ... populate data matrix ... CCrossValidation cv; CTensors* folds = cv.KFoldCV(my_data, 5); // 5 folds // Access data for each fold (example: accessing data for fold 2) matrix fold_data = folds.Get(2); // ... use fold_data for training and validation ... cv.MemoryClear(); // Optionally, release memory used by the cross-validation object ``` -------------------------------- ### MQL5 CTensors Class Constructor and Basic Usage Source: https://github.com/megajoctan/male5/wiki/Tensor-Library Demonstrates the creation of a CTensors object for storing two matrices using the constructor and the Add method. It initializes a 2D tensor, adds two matrix objects, prints the tensor's content, and then properly deallocates the memory. This example is useful for understanding the basic instantiation and data population of the CTensors class. ```MQL5 // Example: Creating a 2D tensor and adding data #include CTensors *tensor; //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { //--- tensor = new CTensors(2); //Tensor for storing two matrices matrix matrix1 = {{1,2}, {103,23}, {42,10}}; matrix matrix2 = { {10,20}, {20,100}}; tensor.Add(matrix1, 0); //adding the first matrix to the first index of the tensor tensor.Add(matrix2, 1); //adding the second matrix to the second index of the tensor Print("Tensor"); tensor.Print_(); delete (tensor); //Delete the tensor once you are done with it } ``` -------------------------------- ### Import MALE5 Regression Libraries (MQL5) Source: https://github.com/megajoctan/male5/blob/MQL5-ML/README.md Imports necessary MALE5 libraries for regression tasks, including Decision Tree Regressor, preprocessing utilities, matrix operations, and performance metrics. Standard libraries like tree.mqh, preprocessing.mqh, MatrixExtend.mqh, and metrics.mqh are included. ```MQL5 #include #include #include //helper functions for data manipulations #include //for measuring the performance StandardizationScaler scaler; //standardization scaler from preprocessing.mqh CDecisionTreeRegressor *decision_tree; //a decision tree regressor model ``` -------------------------------- ### Collect and Prepare Regression Data (MQL5) Source: https://github.com/megajoctan/male5/blob/MQL5-ML/README.md Gathers historical price data (open, high, low, close) for a specified period and constructs the feature matrix (X) and target vector (y). The input features (open, high, low) are used to predict the target variable (close price). ```MQL5 vector open, high, low, close; int data_size = 1000; //--- Getting the open, high, low, and close values for the past 1000 bars, starting from the recent closed bar of 1 open.CopyRates(Symbol(), PERIOD_D1, COPY_RATES_OPEN, 1, data_size); high.CopyRates(Symbol(), PERIOD_D1, COPY_RATES_HIGH, 1, data_size); low.CopyRates(Symbol(), PERIOD_D1, COPY_RATES_LOW, 1, data_size); close.CopyRates(Symbol(), PERIOD_D1, COPY_RATES_CLOSE, 1, data_size); matrix X(data_size, 3); //creating the X matrix //--- Assigning the open, high, and low price values to the X matrix X.Col(open, 0); X.Col(high, 1); X.Col(low, 2); vector y = close; // The target variable is the close price ``` -------------------------------- ### Initialize Decision Tree Regressor (MQL5) Source: https://github.com/megajoctan/male5/blob/MQL5-ML/README.md Initializes a Decision Tree Regressor model from the MALE5 library. This step involves specifying parameters for the regressor, such as the criterion (e.g., Gini impurity or mean squared error) and the maximum depth of the tree. ```MQL5 //--- Model selection decision_tree = new CDecisionTreeRegressor(2, 5); //a decision tree regressor from DecisionTree class ``` -------------------------------- ### AdaBoost Constructor and Core Methods (MQL5) Source: https://github.com/megajoctan/male5/blob/MQL5-ML/Sklearn/Ensemble/README.md This snippet shows the constructor and core methods for the AdaBoost class in MQL5, which are common to both decision tree and logistic regression implementations. It covers initialization, training, and prediction. ```MQL5 class AdaBoost { public: AdaBoost(uint n_estimators = 50, int random_state = 42, bool bootstrapping = true); ~AdaBoost(void); void fit(matrix &x, vector &y); int predict(vector &x); vector predict(matrix &x); private: // Common members for both weak learner types }; ``` -------------------------------- ### Initialize MALE5 Classification Libraries and Model Source: https://github.com/megajoctan/male5/blob/MQL5-ML/README.md This snippet initializes the necessary MALE5 libraries for decision tree classification and data preprocessing. It sets up the StandardizationScaler and declares a pointer for the CDecisionTreeClassifier. ```MQL5 #include #include #include //helper functions for data manipulations #include //fo measuring the performance StandardizationScaler scaler; //standardization scaler from preprocessing.mqh CDecisionTreeClassifier *decision_tree; ``` -------------------------------- ### CKohonenMaps Class Constructor and Core Methods in MQL5 Source: https://github.com/megajoctan/male5/blob/MQL5-ML/Neural Networks/README.md Demonstrates the constructor and core methods of the CKohonenMaps class in MQL5 for training and predicting clusters. The constructor initializes hyperparameters like the number of clusters, learning rate, epochs, and random seed. The `fit` method trains the model, and `predict` methods return cluster assignments for single vectors or matrices. ```mql5 class CKohonenMaps { public: // Constructor CKohonenMaps(uint clusters=2, double alpha=0.01, uint epochs=100, int random_state=42); // Destructor ~CKohonenMaps(void); // Train the model void fit(const matrix &x); // Predict cluster label for a single data point int predict(const vector &x); // Predict cluster labels for multiple data points vector predict(const matrix &x); private: // Internal helper functions (e.g., distance calculation) double Euclidean_distance(const vector &v1, const vector &v2); // ... other private members and functions }; // Example Usage (Conceptual): /* int OnInit() { // ... matrix training_data; // Populate training_data CKohonenMaps som(10, 0.05, 500); som.fit(training_data); vector test_point; // Populate test_point int cluster_label = som.predict(test_point); // ... return(INIT_SUCCEEDED); } */ ``` -------------------------------- ### Split and Normalize Data for Regression Training (MQL5) Source: https://github.com/megajoctan/male5/blob/MQL5-ML/README.md Splits the collected data into training and testing sets using a specified ratio (70% for training) and a random state for shuffling. It then normalizes the independent variables (features) using StandardizationScaler to prepare them for model training. ```MQL5 //--- We split the data into training and testing samples for training and evaluation matrix X_train, X_test; vector y_train, y_test; double train_size = 0.7; //70% of the data should be used for training, the rest for testing int random_state = 42; //we put a random state to shuffle the data MatrixExtend::TrainTestSplitMatrices(X, y, X_train, y_train, X_test, y_test, train_size, random_state); // we split the X and y data into training and testing samples //--- Normalizing the independent variables X_train = scaler.fit_transform(X_train); // fit the scaler on the training data and transform the data X_test = scaler.transform(X_test); // transform the test data // Print the processed data for verification //Print("X_train\n",X_train,"\nX_test\n",X_test,"\ny_train\n",y_train,"\ny_test\n",y_test); ``` -------------------------------- ### Collect and Prepare Price Data for Classification Source: https://github.com/megajoctan/male5/blob/MQL5-ML/README.md This code collects historical Open, High, Low, and Close prices for a specified number of bars. It then structures this data into a matrix (X) which will serve as input features for the classification model. ```MQL5 vector open, high, low, close; int data_size = 1000; //--- Getting the open, high, low and close values for the past 1000 bars, starting from the recent closed bar of 1 open.CopyRates(Symbol(), PERIOD_D1, COPY_RATES_OPEN, 1, data_size); high.CopyRates(Symbol(), PERIOD_D1, COPY_RATES_HIGH, 1, data_size); low.CopyRates(Symbol(), PERIOD_D1, COPY_RATES_LOW, 1, data_size); close.CopyRates(Symbol(), PERIOD_D1, COPY_RATES_CLOSE, 1, data_size); decision_tree = new CDecisionTreeClassifier(2, 5); matrix X(data_size, 3); //creating the x matrix //--- Assigning the open, high, and low price values to the x matrix X.Col(open, 0); X.Col(high, 1); X.Col(low, 2); ``` -------------------------------- ### CRandomForestClassifier Constructor and Fit Method in MQL5 Source: https://github.com/megajoctan/male5/blob/MQL5-ML/Sklearn/Ensemble/README.md Initializes the CRandomForestClassifier with customizable hyperparameters and trains the model using provided data. The 'fit' method supports bootstrapping with or without replacement and allows specifying the error metric for training evaluation. ```MQL5 CRandomForestClassifier(uint n_trees=100, uint minsplit=NULL, uint max_depth=NULL, int random_state=-1) void fit(matrix &x, vector &y, bool replace=true, errors_classifier err=ERR_ACCURACY) ``` -------------------------------- ### Include linalg.mqh Library in MQL5 Source: https://github.com/megajoctan/male5/wiki/Linear-Algebra-Library This snippet shows how to include the linalg.mqh library into your MQL5 project. Ensure the 'linalg.mqh' file is in the correct include path for your project. ```mql5 #include "linalg.mqh" ``` -------------------------------- ### CPatternNets Constructor and Destructor in MQL5 Source: https://github.com/megajoctan/male5/blob/MQL5-ML/Neural Networks/README.md Defines the constructor for initializing the CPatternNets class with input data, target labels, hidden layer node configurations, activation function, and an optional SoftMax layer flag. It also includes the destructor for cleanup. ```MQL5 CPatternNets(matrix &xmatrix, vector &yvector,vector &HL_NODES, activation ActivationFx, bool SoftMaxLyr=false); ~CPatternNets(void); ``` -------------------------------- ### MQL5 Tensor Operations for 3D and 2D Data Source: https://context7.com/megajoctan/male5/llms.txt Demonstrates the usage of C3DTensor and C2DTensor classes from the MALE5 library for handling multi-dimensional data. Shows initialization, appending matrices or vectors, accessing elements, and printing tensor contents. Includes memory cleanup. ```mql5 #include // Create 3D tensor (matrix tensor) C3DTensor *tensor3d = new C3DTensor(); tensor3d.Init(3); // Initialize with 3 slots // Add matrices to tensor matrix m1 = {{1,2,3}, {4,5,6}}; matrix m2 = {{7,8,9}, {10,11,12}}; matrix m3 = {{13,14,15}, {16,17,18}}; tensor3d.Append(m1); tensor3d.Append(m2); tensor3d.Append(m3); // Access matrices CMatrix *mat_obj = tensor3d[0]; Print("First matrix:\n", mat_obj.Matrix); // Print entire tensor tensor3d.Print_(); // Get tensor size uint size = tensor3d.Size(); printf("Tensor contains %d matrices", size); // 2D tensor (vector tensor) C2DTensor *tensor2d = new C2DTensor(); tensor2d.Init(2); vector v1 = {1.0, 2.0, 3.0}; vector v2 = {4.0, 5.0, 6.0}; tensor2d.Append(v1); tensor2d.Append(v2); CVectors *vec_obj = tensor2d[0]; Print("First vector: ", vec_obj.Vector); // Cleanup delete tensor3d; delete tensor2d; ``` -------------------------------- ### CRandomForestRegressor Constructor and Fit Method in MQL5 Source: https://github.com/megajoctan/male5/blob/MQL5-ML/Sklearn/Ensemble/README.md Initializes the CRandomForestRegressor, similar to the classifier, with customizable hyperparameters for regression tasks. The 'fit' method trains the model on regression data, defaulting to R2 score for evaluation. ```MQL5 CRandomForestRegressor(uint n_trees=100, uint minsplit=NULL, uint max_depth=NULL, int random_state=-1) void fit(matrix &x, vector &y, bool replace=true, errors_regressor err=ERR_R2_SCORE) ``` -------------------------------- ### MQL5: Calculate Dot Product of Matrices Source: https://github.com/megajoctan/male5/wiki/Linear-Algebra-Library Demonstrates calculating the dot product (matrix multiplication) of two matrices, A and B, using the LinAlg::dot function from the linalg.mqh library. The result is stored in matrix C. This function supports numpy-like behavior for matrix multiplication. ```mql5 matrix A = {{1, 2}, {3, 4}}; matrix B = {{5, 6}, {7, 8}}; matrix C = LinAlg::dot(A, B); // Matrix multiplication Print("Dot product (matrix multiplication):"); Print(C); ``` -------------------------------- ### Predict Classes using MALE5 Classifier Source: https://github.com/megajoctan/male5/blob/MQL5-ML/README.md Demonstrates how to use the `predict_bin` function of the MALE5 Decision Tree Classifier to obtain binary class predictions (e.g., bullish or bearish) for new data. ```MQL5 //--- Prediction Unlike the `predict` function which is used to obtain predictions in regressors. The predictive functions for the classifiers have different slightly for different name starting with the word predict. - **predict_bin** - This function can be used to predict the classes in as integer values for classification models. For example: The next candle will be ***bullish*** or ***bearish*** ``` -------------------------------- ### Train Decision Tree Regression Model (MQL5) Source: https://github.com/megajoctan/male5/blob/MQL5-ML/README.md Trains the initialized Decision Tree Regressor model using the preprocessed training data (X_train and y_train). The model learns the relationship between the input features and the target variable during this phase. ```MQL5 //--- Training the model decision_tree.fit(X_train, y_train); // The training function ``` -------------------------------- ### Predict Next Closing Price in Real-time (MQL5) Source: https://github.com/megajoctan/male5/blob/MQL5-ML/README.md Implements a real-time prediction mechanism within the OnTick function to forecast the next closing price of a financial instrument. It fetches the latest market data, formats it as input features, and uses the trained decision tree model to predict the price. ```MQL5 void OnTick() { //--- Making predictions live from the market CopyRates(Symbol(), PERIOD_D1, 1, 1, rates); // Get the very recent information from the market vector x = {rates[0].open, rates[0].high, rates[0].low}; // Assigning data from the recent candle in a similar way to the training data double predicted_close_price = decision_tree.predict(x); Comment("Next closing price predicted is = ", predicted_close_price); } ``` -------------------------------- ### Include kernels.mqh Library in MQL5 Source: https://github.com/megajoctan/male5/wiki/Kernels-Library This code snippet demonstrates how to include the kernels.mqh library into your MQL5 project. Ensure the 'kernels.mqh' file is in the include path of your project for this to work. ```mql5 #include "kernels.mqh" ``` -------------------------------- ### Predict Probabilities for Next Candle (MQL5) Source: https://github.com/megajoctan/male5/blob/MQL5-ML/README.md This function predicts the probabilities of a certain class for the next market candle using a decision tree model. It takes recent market data (open, high, low) as input and returns a double value representing the predicted probability. The prediction is made live within the OnTick() function. ```MQL5 void OnTick() { //--- Making predictions live from the market CopyRates(Symbol(), PERIOD_D1, 1, 1, rates); //Get the very recent information from the market vector x = {rates[0].open, rates[0].high, rates[0].low}; //Assigning data from the recent candle in a similar way to the training data double predicted_close_price = decision_tree.predict_bin(x); Comment("Next closing price predicted is = ",predicted_close_price); } ``` -------------------------------- ### MQL5 Data Manipulation Utilities Source: https://context7.com/megajoctan/male5/llms.txt Provides helper functions for data transformation, including random data generation, finding unique values, searching, vector-matrix conversions, sorting, and file I/O for CSV files. These functions operate on MQL5's native vector and matrix types. ```mql5 #include // Generate random data vector rand_vec = CUtils::Random(0.0, 1.0, 100, 42); // min, max, size, random_state matrix rand_mat = CUtils::Random(-1.0, 1.0, 50, 10, 42); // min, max, rows, cols, seed Print("Random vector sample: ", rand_vec); // Find unique values vector data = {1, 2, 2, 3, 1, 4, 3, 2}; vector unique_vals = CUtils::Unique(data); Print("Unique values: ", unique_vals); // Output: [1, 2, 3, 4] // Count unique values vector counts = CUtils::Unique_count(data); Print("Counts: ", counts); // Output: [2, 3, 2, 1] for values [1, 2, 3, 4] // Search for value vector search_indices = CUtils::Search(data, 2.0); Print("Indices where value=2: ", search_indices); // Output: [1, 2, 6] // Vector to matrix conversion vector v = {1,2,3,4,5,6}; matrix m = CUtils::VectorToMatrix(v, 2); // 2 columns Print("Matrix:\n", m); // Output: [[1,2], [3,4], [5,6]] // Matrix to vector conversion matrix mat = {{1,2}, {3,4}}; vector vec = CUtils::MatrixToVector(mat); Print("Vector: ", vec); // Output: [1, 2, 3, 4] // Sort vector vector unsorted = {3.5, 1.2, 4.8, 2.1}; vector sorted = CUtils::Sort(unsorted, SORT_ASCENDING); Print("Sorted: ", sorted); // Get sorted indices vector indices = CUtils::ArgSort(unsorted); Print("Sorted indices: ", indices); // Write matrix to CSV matrix data_out = {{1.5, 2.5}, {3.5, 4.5}}; string headers[] = {"Feature1", "Feature2"}; CUtils::WriteCsv("output.csv", data_out, headers, false, 2); // Read CSV to matrix string col_names; matrix data_in = CUtils::ReadCsv("data.csv", col_names, ",", false, false); Print("Loaded data:\n", data_in); ``` -------------------------------- ### CLogisticRegression Constructor and Destructor (MQL5) Source: https://github.com/megajoctan/male5/blob/MQL5-ML/Sklearn/Linear Models/README.md Initializes the CLogisticRegression model with configurable hyperparameters for training, such as epochs, learning rate (alpha), and tolerance (tol). The destructor ensures proper cleanup of resources. ```MQL5 CLogisticRegression(uint epochs=10, double alpha=0.01, double tol=1e-8); ~CLogisticRegression(void); ``` -------------------------------- ### Train Decision Tree Classifier and Predict Market Signals in MQL5 Source: https://context7.com/megajoctan/male5/llms.txt Trains a decision tree classifier using historical MQL5 price data for binary market signal classification. It includes data collection, feature engineering, train-test split, standardization, model training, and evaluation using classification reports. The snippet also demonstrates live prediction on new ticks. ```mql5 #include #include #include #include StandardizationScaler scaler; CDecisionTreeClassifier *decision_tree; MqlRates rates[]; int OnInit() { // Initialize model with min_samples_split=2, max_depth=5 decision_tree = new CDecisionTreeClassifier(2, 5); // Collect price data vector open, high, low, close; int data_size = 1000; open.CopyRates(Symbol(), PERIOD_D1, COPY_RATES_OPEN, 1, data_size); high.CopyRates(Symbol(), PERIOD_D1, COPY_RATES_HIGH, 1, data_size); low.CopyRates(Symbol(), PERIOD_D1, COPY_RATES_LOW, 1, data_size); close.CopyRates(Symbol(), PERIOD_D1, COPY_RATES_CLOSE, 1, data_size); // Create feature matrix matrix X(data_size, 3); X.Col(open, 0); X.Col(high, 1); X.Col(low, 2); // Create target labels (1=bullish, 0=bearish) vector y(data_size); for (int i=0; i open[i]) ? 1 : 0; // Train-test split matrix X_train, X_test; vector y_train, y_test; double train_size = 0.7; int random_state = 42; MatrixExtend::TrainTestSplitMatrices(X, y, X_train, y_train, X_test, y_test, train_size, random_state); // Standardize features X_train = scaler.fit_transform(X_train); X_test = scaler.transform(X_test); // Train model decision_tree.fit(X_train, y_train); // Evaluate vector train_predictions = decision_tree.predict_bin(X_train); Print("Training results:"); Metrics::classification_report(y_train, train_predictions); vector test_predictions = decision_tree.predict_bin(X_test); Print("Testing results:"); Metrics::classification_report(y_test, test_predictions); return(INIT_SUCCEEDED); } void OnTick() { // Live prediction CopyRates(Symbol(), PERIOD_D1, 1, 3, rates); vector x = {rates[0].open, rates[0].high, rates[0].low}; x = scaler.transform(x); int signal = (int)decision_tree.predict_bin(x); Comment("Signal = ", signal==1 ? "BUY" : "SELL"); } void OnDeinit(const int reason) { delete(decision_tree); } ``` -------------------------------- ### Logistic Regression Weak Learner in AdaBoost (MQL5) Source: https://github.com/megajoctan/male5/blob/MQL5-ML/Sklearn/Ensemble/README.md This snippet demonstrates the namespace-specific members for employing Logistic Regression as weak learners within the AdaBoost class in MQL5. It shows the handling of Logistic Regression model pointers. ```MQL5 namespace LogisticRegression { class AdaBoost : public EnsembleBase { public: // Constructor and other methods... private: CLogisticRegression *weak_learners[]; CLogisticRegression *weak_learner; }; } ``` -------------------------------- ### MQL5 Decision Tree Regressor for Price Prediction Source: https://context7.com/megajoctan/male5/llms.txt Trains a decision tree regressor to predict continuous values like closing prices using OHLC data. It includes data preparation, normalization, model training, and evaluation using R² scores. Dependencies include custom MQL5 include files for the decision tree, preprocessing, matrix extensions, and metrics. ```mql5 #include #include #include #include StandardizationScaler scaler; CDecisionTreeRegressor *decision_tree; MqlRates rates[]; int OnInit() { // Initialize regressor decision_tree = new CDecisionTreeRegressor(2, 5); // Collect historical data vector open, high, low, close; int data_size = 1000; open.CopyRates(Symbol(), PERIOD_D1, COPY_RATES_OPEN, 1, data_size); high.CopyRates(Symbol(), PERIOD_D1, COPY_RATES_HIGH, 1, data_size); low.CopyRates(Symbol(), PERIOD_D1, COPY_RATES_LOW, 1, data_size); close.CopyRates(Symbol(), PERIOD_D1, COPY_RATES_CLOSE, 1, data_size); // Create feature matrix (X) and target vector (y) matrix X(data_size, 3); X.Col(open, 0); X.Col(high, 1); X.Col(low, 2); vector y = close; // Split data matrix X_train, X_test; vector y_train, y_test; double train_size = 0.7; int random_state = 42; MatrixExtend::TrainTestSplitMatrices(X, y, X_train, y_train, X_test, y_test, train_size, random_state); // Normalize features X_train = scaler.fit_transform(X_train); X_test = scaler.transform(X_test); // Train and evaluate decision_tree.fit(X_train, y_train); vector train_predictions = decision_tree.predict(X_train); printf("Training R² score = %.3f", Metrics::RegressionMetric(y_train, train_predictions, METRIC_R_SQUARED)); vector test_predictions = decision_tree.predict(X_test); printf("Testing R² score = %.3f", Metrics::r_squared(y_test, test_predictions)); return(INIT_SUCCEEDED); } void OnTick() { CopyRates(Symbol(), PERIOD_D1, 1, 3, rates); vector x = {rates[0].open, rates[0].high, rates[0].low}; x = scaler.transform(x); double predicted_close = decision_tree.predict(x); Comment("Predicted close price = ", predicted_close); } void OnDeinit(const int reason) { delete(decision_tree); } ``` -------------------------------- ### MQL5 StandardizationScaler for Data Preprocessing Source: https://context7.com/megajoctan/male5/llms.txt Implements data standardization to give features a mean of zero and unit variance. This is crucial for improving the performance and convergence of machine learning models. It utilizes custom MQL5 include files for preprocessing and matrix extensions. ```mql5 #include #include // Create scaler instance StandardizationScaler scaler; // Prepare sample data matrix X_train = {{1.5, 2.0, 3.5}, {2.5, 3.0, 4.5}, {3.5, 4.0, 5.5}, {4.5, 5.0, 6.5}}; matrix X_test = {{1.8, 2.3, 3.8}, {3.2, 3.8, 5.2}}; // Fit on training data and transform matrix X_train_scaled = scaler.fit_transform(X_train); Print("Scaled training data:\n", X_train_scaled); // Transform test data using training statistics matrix X_test_scaled = scaler.transform(X_test); Print("Scaled test data:\n", X_test_scaled); // Transform single vector vector x_new = {2.0, 2.5, 4.0}; vector x_new_scaled = scaler.transform(x_new); Print("Scaled vector: ", x_new_scaled); // Output: // Features are scaled to mean=0, std=1 // Test data uses training set's mean and std ``` -------------------------------- ### Decision Tree Weak Learner in AdaBoost (MQL5) Source: https://github.com/megajoctan/male5/blob/MQL5-ML/Sklearn/Ensemble/README.md This snippet illustrates the namespace-specific members for using Decision Trees as weak learners within the AdaBoost class in MQL5. It highlights the management of Decision Tree classifier pointers. ```MQL5 namespace DecisionTree { class AdaBoost : public EnsembleBase { public: // Constructor and other methods... private: CDecisionTreeClassifier *weak_learners[]; CDecisionTreeClassifier *weak_learner; }; } ``` -------------------------------- ### MQL5 Label Encoding for Categorical Data Source: https://context7.com/megajoctan/male5/llms.txt Implements label encoding to convert categorical string labels into numerical values, suitable for machine learning model input. It supports fitting the encoder on a set of labels, transforming labels to integers, and inverse transforming integers back to labels. Handles unknown labels by returning -1. ```mql5 #include // Initialize label encoder CLabelEncoder encoder; // Fit encoder on training labels string labels[] = {"cat", "dog", "cat", "bird", "dog", "bird", "cat"}; encoder.fit(labels); // Check learned classes Print("Classes: ", encoder.m_classes); // Output: ["bird", "cat", "dog"] (sorted) // Transform labels to integers vector encoded = encoder.transform(labels); Print("Encoded labels: ", encoded); // Output: [1, 2, 1, 0, 2, 0, 1] // Transform single label int encoded_label = encoder.transform("dog"); printf("Encoded 'dog': %d", encoded_label); // Output: Encoded 'dog': 2 // Inverse transform string decoded_labels[]; encoder.inverse_transform(encoded, decoded_labels); ArrayPrint(decoded_labels); // Output: ["cat", "dog", "cat", "bird", "dog", "bird", "cat"] // Error handling for unknown labels int unknown = encoder.transform("elephant"); // Output: Warning: Unknown label 'elephant' found in transform // Returns: -1 ``` -------------------------------- ### MQL5 Matrix Utility Functions for Data Manipulation Source: https://context7.com/megajoctan/male5/llms.txt Provides essential matrix and vector manipulation functions for data preparation and transformation. Includes functionalities like train-test splitting, matrix concatenation, one-hot encoding, column removal, and slicing operations, all within the MQL5 framework. ```mql5 #include // Train-test split matrix X = {{1,2,3}, {4,5,6}, {7,8,9}, {10,11,12}}; vector y = {0, 1, 0, 1}; matrix X_train, X_test; vector y_train, y_test; MatrixExtend::TrainTestSplitMatrices(X, y, X_train, y_train, X_test, y_test, 0.75, 42); Print("Train size: ", X_train.Rows(), " Test size: ", X_test.Rows()); // Concatenate matrices matrix A = {{1,2}, {3,4}}; matrix B = {{5,6}, {7,8}}; matrix C = MatrixExtend::concatenate(A, B, 0); // axis=0 (rows) Print("Concatenated matrix:\n", C); // Output: [[1,2], [3,4], [5,6], [7,8]] // One-hot encoding vector labels = {0, 1, 2, 0, 1}; matrix encoded = MatrixExtend::OneHotEncoding(labels); Print("One-hot encoded:\n", encoded); // Output: [[1,0,0], [0,1,0], [0,0,1], [1,0,0], [0,1,0]] // Remove column from matrix matrix data = {{1,2,3}, {4,5,6}}; MatrixExtend::RemoveCol(data, 1); // Remove column 1 Print("After removing column:\n", data); // Output: [[1,3], [4,6]] // Slice operations matrix M = {{1,2,3}, {4,5,6}, {7,8,9}}; matrix sliced = MatrixExtend::Slice(M, 1, 3, 0); // rows 1 to 3 Print("Sliced matrix:\n", sliced); ``` -------------------------------- ### Initialize Decision Tree Classifier Model Source: https://github.com/megajoctan/male5/blob/MQL5-ML/README.md This code snippet explicitly initializes a Decision Tree Classifier model. It is part of the model selection phase in a classification pipeline. ```MQL5 //--- Model selection decision_tree = new CDecisionTreeClassifier(2, 5); //a decision tree classifier from DecisionTree class ``` -------------------------------- ### Split and Normalize Data for MALE5 Classification Source: https://github.com/megajoctan/male5/blob/MQL5-ML/README.md This section splits the prepared data into training and testing sets using a specified ratio and random state for reproducibility. It then normalizes the training data using StandardizationScaler and applies the same transformation to the test data. ```MQL5 //--- We split the data into training and testing samples for training and evaluation matrix X_train, X_test; vector y_train, y_test; double train_size = 0.7; //70% of the data should be used for training the rest for testing int random_state = 42; //we put a random state to shuffle the data so that a machine learning model understands the patterns and not the order of the dataset, this makes the model durable MatrixExtend::TrainTestSplitMatrices(X, y, X_train, y_train, X_test, y_test, train_size, random_state); // we split the x and y data into training and testing samples //--- Normalizing the independent variables X_train = scaler.fit_transform(X_train); // we fit the scaler on the training data and transform the data alltogether X_test = scaler.transform(X_test); // we transform the new data this way //Print("X_train\n",X_train,"\nX_test\n",X_test,"\ny_train\n",y_train,"\ny_test\n",y_test); ``` -------------------------------- ### Predict Class Labels for Multiple Data Points (MQL5) Source: https://github.com/megajoctan/male5/blob/MQL5-ML/Sklearn/Linear Models/README.md Predicts binary class labels for multiple new data points contained in the input matrix 'x', where each row is a data point. The model must be trained prior to calling this method. ```MQL5 vector predict(matrix &x); ``` -------------------------------- ### Train MALE5 Decision Tree Classifier Source: https://github.com/megajoctan/male5/blob/MQL5-ML/README.md This code trains the initialized Decision Tree Classifier model using the preprocessed training data (X_train) and corresponding labels (y_train). The `fit` function learns the patterns necessary for classification. ```MQL5 //--- Training the model decision_tree.fit(X_train, y_train); //The training function ``` -------------------------------- ### MQL5 K-Fold Cross-Validation Implementation Source: https://github.com/megajoctan/male5/wiki/Cross-Validation-Library Implements K-Fold Cross-Validation for a given data matrix in MQL5. It splits the data into a specified number of folds, returning a CTensors object containing the data for each fold. Ensure proper memory management after use. ```MQL5 CTensors* KFoldCV(matrix &data, uint n_spilts=5) { // Memory Allocation ArrayResize(tensors, ArraySize(tensors) + 1); tensors[ArraySize(tensors) - 1] = new CTensors(n_spilts); // Splitting Data uint fold_size = data.Rows() / n_spilts; for (uint i = 0; i < n_spilts; i++) { uint start_row = i * fold_size; uint rows_in_fold = (i == n_spilts - 1) ? (data.Rows() - start_row) : fold_size; matrix fold_data; if (MatrixExtend::Get(data, fold_data, start_row, rows_in_fold) == -1) { Print("Error getting data for fold ", i); continue; } tensors[ArraySize(tensors) - 1].Add(fold_data); } // Returning Results return tensors[ArraySize(tensors) - 1]; } ``` -------------------------------- ### MQL5 Regression Metrics Calculation Source: https://context7.com/megajoctan/male5/llms.txt Computes common regression metrics like R-squared, MSE, RMSE, and MAE using the MALE5 metrics library. It takes true and predicted values as input and returns evaluation scores. Includes support for adjusted R-squared and generic metric calculation. ```mql5 #include vector y_true = {3.0, -0.5, 2.0, 7.0}; vector y_pred = {2.5, 0.0, 2.0, 8.0}; // R-squared score double r2 = Metrics::r_squared(y_true, y_pred); printf("R² Score: %.4f", r2); // Output: R² Score: 0.9486 // Mean Squared Error double mse = Metrics::mse(y_true, y_pred); printf("MSE: %.4f", mse); // Output: MSE: 0.3750 // Root Mean Squared Error double rmse = Metrics::rmse(y_true, y_pred); printf("RMSE: %.4f", rmse); // Output: RMSE: 0.6124 // Mean Absolute Error double mae = Metrics::mae(y_true, y_pred); printf("MAE: %.4f", mae); // Output: MAE: 0.5000 // Adjusted R² for multiple features double adj_r2 = Metrics::adjusted_r(y_true, y_pred, 3); printf("Adjusted R²: %.4f", adj_r2); // Using generic metric function double metric = Metrics::RegressionMetric(y_true, y_pred, METRIC_R_SQUARED); printf("Generic R²: %.4f", metric); ``` -------------------------------- ### CRandomForestClassifier Prediction Methods in MQL5 Source: https://github.com/megajoctan/male5/blob/MQL5-ML/Sklearn/Ensemble/README.md Provides methods to predict class labels for single data points or multiple data points using a trained CRandomForestClassifier model. The prediction can be for a single input vector or a matrix of input vectors. ```MQL5 double predict(vector &x) vector predict(matrix &x) ``` -------------------------------- ### MQL5 Classification Metrics and Reports Generation Source: https://context7.com/megajoctan/male5/llms.txt Calculates essential classification metrics including accuracy, precision, recall, and F1-score using the MALE5 metrics library. It supports per-class metrics, generates a comprehensive classification report, and computes ROC curve data for probability predictions. ```mql5 #include vector y_true = {1, 0, 1, 1, 0, 1, 0, 0}; vector y_pred = {1, 0, 1, 0, 0, 1, 0, 1}; // Overall accuracy double acc = Metrics::accuracy_score(y_true, y_pred); printf("Accuracy: %.4f", acc); // Output: Accuracy: 0.7500 // Per-class metrics vector precision = Metrics::precision(y_true, y_pred); Print("Precision per class: ", precision); vector recall = Metrics::recall(y_true, y_pred); Print("Recall per class: ", recall); vector f1 = Metrics::f1_score(y_true, y_pred); Print("F1-score per class: ", f1); // Comprehensive classification report Metrics::classification_report(y_true, y_pred); // Output includes: // - Precision, Recall, F1-score for each class // - Support (number of samples per class) // - Overall accuracy // - Macro and weighted averages // ROC curve for probability predictions vector y_proba = {0.9, 0.1, 0.8, 0.4, 0.2, 0.95, 0.15, 0.6}; roc_curve_struct roc = Metrics::roc_curve(y_true, y_proba, true); Print("TPR: ", roc.TPR); Print("FPR: ", roc.FPR); Print("Thresholds: ", roc.Thresholds); ```