I want to classify linear, parabolic and exponential functions using machine learning.

조회 수: 2 (최근 30일)
I need to use machine learning for this project. 10 x and y values ​​of the functions will be given and using these values, it will be understood whether the other functions to be written are linear parabolic or exponential functions. I need help with where to start and where to go after. Not sure what to do.

채택된 답변

Zinea
Zinea 2024년 5월 17일
편집: Zinea 2024년 5월 17일
I understand that the task at hand is to classify a given set of 10 (x, y) pairs into one of three categories: linear, parabolic, or exponential. Here is the workflow using MATLAB:
Step 1: Feature Engineering
  1. Slope Features: Calculate slopes between consecutive points. For linear functions, slopes should be relatively constant; for quadratic functions, they should increase or decrease linearly; for exponential functions, the rate of change will be more variable and potentially higher.
  2. Y value Ratios: Calculate the ratio of consecutive y values (y[i+1]/y[i]). These ratios might be nearly constant for exponential functions, but not for linear or quadratic functions.
  3. Curve Fitting errors: Fit each set of (x,y) pairs with linear, parabolic, and exponential models and calculate the fit error (e.g., R2 score, mean squared error). The model with the lowest error indicates the function type. The Curve Fitting Toolbox (cftool) can be used like below for fitting each function type:
[fitLin, gofLin] = fit(x, y, 'poly1'); //Linear fit
[fitQuad, gofQuad] = fit(x, y, 'poly2'); // Parabolic fit
[fitExp, gofExp] = fit(x, y, 'exp1'); // exponential fit
Here, “gofLin”,gofQuad” and “gofExpcontain goodness-of-fit” statistics, including the R-squared value (“gofLin.rsquare”), which can be used as features.
4. Statistical Features: Calculate statistical features like the mean, variance, skewness, and kurtosis of both x and y values, as they might reveal the distribution and spread of data points indicative of the underlying function, using the Statistics and Machine Learning Toolbox in MATLAB.
Step 2: Model Selection:
MATLAB’s Statistics and Machine Learning Toolbox provides various algorithms for classification. Some of these are:
  • fitctree” for Decision trees
  • TreeBagger” for Random Forests.
  • fitcsvm” for SVM
Step 3: Preparing the Dataset:
  • Label each set of (x,y) pairs according to their function type.
  • Divide the dataset into training and testing sets. A common split is 80% for training and 20% for testing.
Step 4: Training the model:
Train the model using the chosen model and the training dataset in Steps 2 and 3.
Step 5: Model Evaluation:
The accuracy, F1 score, and confusion matrix (using “confusionmat”) are calculated using MATLAB functions to evaluate the model’s performance.
Step 6: Prediction:
With this trained model, you can now predict the function type of new sets of (x,y) pairs.
You may refer to the following documentation links for more information:
Hope it helps!
  댓글 수: 1
Hasan Yilmaz
Hasan Yilmaz 2024년 5월 25일
Hey mate, i have something like this. Can you show me or tell me what do i need to do after this ? You said model selection but i couldn't use classification learner. I also have another code that generates random (x, y) pairs. Do i need to use specific (x,y) pairs or can i generate random for exmp :
------------------------------------------------------------------------------
linear = cell(1, 10); %
for index = 1:10
x = 0:0.1:randi(10)+10;
m = randi(50) - 25;
n = randi(50) - 25;
linear{index} = x .* m + n ; %
end
------------------------------------------------------------------------------
//My main code
% Örnek veri (kendi verilerinizi buraya yükleyebilirsiniz)
% x = [...];
% y = [...];
% Test verileri (kendinizin sağlaması gerekir)
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]';
y = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]'; % Bu örneği değiştirin
% Lineer model
linear_fit = fit(x, y, 'poly1');
y_pred_linear = feval(linear_fit, x);
mse_linear = immse(y, y_pred_linear);
% Parabolik model
parabolic_fit = fit(x, y, 'poly2');
y_pred_parabolic = feval(parabolic_fit, x);
mse_parabolic = immse(y, y_pred_parabolic);
% Üstel model
exp_fit = fit(x, y, 'exp1');
y_pred_exponential = feval(exp_fit, x);
mse_exponential = immse(y, y_pred_exponential);
% MSE değerlerini karşılaştırma
mse_values = [mse_linear, mse_parabolic, mse_exponential];
model_names = {'Lineer', 'Parabolik', 'Üstel'};
[~, best_model_index] = min(mse_values);
best_fit = model_names{best_model_index};
% Sonuçları yazdırma
fprintf('Lineer model MSE: %f\n', mse_linear);
fprintf('Parabolik model MSE: %f\n', mse_parabolic);
fprintf('Üstel model MSE: %f\n', mse_exponential);
fprintf('En iyi model: %s\n', best_fit);
% Grafik çizme
figure;
scatter(x, y, 'k', 'DisplayName', 'Veri');
hold on;
plot(x, y_pred_linear, 'b', 'DisplayName', 'Lineer model'); // linear
plot(x, y_pred_parabolic, 'r', 'DisplayName', 'Parabolik model'); // parabolic
plot(x, y_pred_exponential, 'g', 'DisplayName', 'Üstel model'); // exp
legend show;
hold off;

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Shivani
Shivani 2024년 5월 16일
As mentioned in the MathWorks documentation, the Statistics and Machine Learning Toolbox provides functions and apps to describe, analyze, and model data. Kindly refer to the following documentation link for more details regarding this: https://www.mathworks.com/products/statistics.html
Regarding the use case as given in the question, it looks like a classifier is necessary to accurately identify the function type. The 'x' and 'y' coordinates will serve as the input or training data, while the function type will be used as the testing data. This approach enables the model to be trained to predict labels for new data accurately.
For further insights on how to accomplish this task, kindly refer to the following links:
1. To pre-process the x-y coordinate data
2. To classify and train your model on the most accurate classifier:
Hope this helps!

카테고리

Help CenterFile Exchange에서 Smoothing에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by