Auto generate table input type for predictFcn exported model

1 次查看(过去 30 天)
Hi
I have already read the documentaion about the methods requiervedVariable and hotToPredict of the exported model.
I also manually created the required input format for predictFcn.
My dataset is a table with lots of categorical and double data. I select a few of the columns as feature in regression lerarner app.
I am looking for an easier way to create a input data table for testing and predicting with my model after exporing. Its a little hard to each time create a table manually with the variable names and variable types.
This is my Method now:
if trained == true
load trainedModel.mat
varNames = ["BlockTime","FlightTime","dayOfYear","hourOfDay","payloadKg","totalWeightKg"];
varTypes = ["double","double","double","double","double","double"];
inT = table('Size',size(varNames),'VariableTypes',varTypes,'VariableNames',varNames);
intT(1,:) = array2table([BlockTime FlightTime dayofYear PayloadKG TotalKG]);
fuelPredict = trainedModel.predictFcn(inT)
end
but my main data has lots of other columns so I cant use that for input template
opts.Sheet = "Sheet1";
opts.DataRange = "A2:AM5115";
% Specify column names and types
opts.VariableNames = ["ACType", "ACReg", "FlightDateM", "FlightNo", "Origin", "Destination", "Airborn", "OffBlock", "TouchDown", "OnBlock", "BaggageKG", "BaggagePD", "AdultCount", "InfantCount", "ChildCount", "FlightTime", "BlockTime", "SitaFlightTime", "RemainingFuelKG", "RemainingFuelLt", "RemainingFuelPD", "UpliftFuelKG", "UpliftFuelLT", "UpliftFuelPD", "ArrFuelKG", "ArrFuelLT", "ArrFuelPD", "DiffFuelKG", "DiffFuelLT", "DiffFuelPD", "RampFuelKG", "RampFuelLT", "RampFuelPD", "SitaFuelKG", "SitaFuelLT", "SitaFuelPD", "TaxiFuelKG", "TaxiFuelLT", "TaxiFuelPD"];
opts.VariableTypes = ["categorical", "categorical", "datetime", "string", "categorical", "categorical", "datetime", "datetime", "datetime", "datetime", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double"];
  1 个评论
Siddharth Bhutiya
Siddharth Bhutiya 2022-7-22
Can you share a sample MAT file of what the initial data looks like and what you want the final table to look like ?

请先登录,再进行评论。

采纳的回答

Alireza Ghaderi
Alireza Ghaderi 2023-5-11
I wrote this function for myself once for ever:)
just share it here if anyone need it:
function prediction = smartPredict(model, inputTable)
% smartPredict Predict the output using the given model and input table
%
% Syntax: prediction = smartPredict(model, inputTable)
%
% Inputs:
% model: A regression learner model created by the Regression Learner App
% inputTable: A table containing the input data, which may or may not have
% all the required variables
%
% Outputs:
% prediction: A scalar or vector containing the predicted output(s)
% based on the given input data
%
% Example:
% mdl = fitlm(X, y); % Create a linear regression model
% input_data = table(X1, X2); % Create an input table
% pred = smartPredict(mdl, input_data); % Get the prediction
%
% Notes:
% - This function will report missing required variables and ignore
% any extra columns in the input table.
% - The output is a scalar if the input data has one row and a vector if
% the input data has multiple rows.
% Get the required variables from the model
requiredVariables = model.RequiredVariables;
% Check if the input table contains all the required variables
missingVariables = setdiff(requiredVariables, inputTable.Properties.VariableNames);
if ~isempty(missingVariables)
% Report missing variables
error('smartPredict:missingVariables', ...
'The input table is missing the following required variables: %s', ...
strjoin(missingVariables, ', '));
end
% Select only the required variables from the input table
selectedInputTable = inputTable(:, requiredVariables);
% Make the prediction
try
prediction = model.predictFcn(selectedInputTable);
catch e
% Report errors during prediction
error('smartPredict:predictionError', ...
'An error occurred during prediction: %s', e.message);
end
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Support Vector Machine Regression 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by