what we should use instead of 'datetime'

12 Ansichten (letzte 30 Tage)
arash rad
arash rad am 29 Sep. 2022
Kommentiert: arash rad am 30 Sep. 2022
Hello
I found a code in github and I want to use it for my data but there is problem
in that code date time is define as for example 31-Oct-2019 but my time is in seconds what should i use in that line ?
in one line he define
% x_data = datetime(jean_data.collect_day);
but when i replace it with my data i get a error :Could not recognize the format of the date/time text. You can specify a format using the 'InputFormat' parameter. If the date/time text contain day, month, or time zone names in a language foreign to the 'en_US' locale, those might not be recognized. You can specify a different locale using the 'Locale' parameter.
please help me
Thank you very much
the code is this :
clc;clear all;close all
warning off
flow_data = readtable('zafar_queue.xlsx');
% Fill the NaN value with the Nearest value.
flow_data.begin = num2str(flow_data.begin);
for i=1 : length(flow_data.begin)
flow_data.begin(i) = strip(flow_data.begin(i),"'");
end
Y = flow_data.nVehContrib;
data = Y';
%
% 0 to 6300 seconds about 105 minutes : Training Data Set
% 6300 to 7200 seconds about 15 minutes : Test Data Set
numTimeStepsTrain = floor(0.9*numel(data));
dataTrain = data(1:numTimeStepsTrain+1);
dataTest = data(numTimeStepsTrain+1:end);
% Normalize sales_price to a value between 0 and 1 (Training Data Set)
mu = mean(dataTrain);
sig = std(dataTrain);
dataTrainStandardized = (dataTrain - mu) / sig;
XTrain = dataTrainStandardized(1:end-1);
YTrain = dataTrainStandardized(2:end);
%LSTM Net Architecture Def
numFeatures = 1;
numResponses = 1;
numHiddenUnits = 200;
layers = [ ...
sequenceInputLayer(numFeatures)
lstmLayer(numHiddenUnits)
fullyConnectedLayer(numResponses)
regressionLayer];
options = trainingOptions('adam', ...
'MaxEpochs',500, ...
'GradientThreshold',1, ...
'InitialLearnRate',0.005, ...
'LearnRateSchedule','piecewise', ...
'LearnRateDropPeriod',125, ...
'LearnRateDropFactor',0.2, ...
'Verbose',0, ...
'Plots','training-progress');
%
% Train LSTM Net
net = trainNetwork(XTrain,YTrain,layers,options);
% Normalize sales_price to a value between 0 and 1 (Testing Data Set)
dataTestStandardized = (dataTest - mu) / sig;
XTest = dataTestStandardized(1:end-1);
net = predictAndUpdateState(net,XTrain);
[net,YPred] = predictAndUpdateState(net,YTrain(end));
%
% Predict as long as the test period (2019.05.07 ~ 2019.10.31)
numTimeStepsTest = numel(XTest);
for i = 2:numTimeStepsTest
[net,YPred(:,i)] = predictAndUpdateState(net,YPred(:,i-1),'ExecutionEnvironment','cpu');
end
% RMSE calculation of test data set
YTest = dataTest(2:end);
YTest = (YTest - mu) / sig;
rmse = sqrt(mean((YPred-YTest).^2))
% Denormalize Data
YPred = sig*YPred + mu;
YTest = sig*YTest + mu;
% X Label : Collect Day
x_data = datetime(flow_data.begin);
x_train = x_data(1:numTimeStepsTrain+1);
x_train = x_train';
x_pred = x_data(numTimeStepsTrain:numTimeStepsTrain+numTimeStepsTest);
% Train + Predict Plot
figure
plot(x_train(1:end-1),dataTrain(1:end-1))
hold on
plot(x_pred,[data(numTimeStepsTrain) YPred],'.-')
hold off
xlabel("Collect Day")
ylabel("Sales Price")
title("Forecast")
legend(["Observed" "Forecast"])
%
% % RMSE Plot : Test + Predict Plot
% figure
% subplot(2,1,1)
% plot(YTest)
% hold on
% plot(YPred,'.-')
% hold off
% legend(["Observed" "Forecast"])
% ylabel("Sales Price")
% title("Forecast")
%
% subplot(2,1,2)
% stem(YPred - YTest)
% xlabel("Collect Day")
% ylabel("Error")
% title("RMSE = " + rmse)
%
% % Train + Test + Predict Plot
% figure
% plot(x_data,Y)
% hold on
% plot(x_pred,[data(numTimeStepsTrain) YPred],'.-')
% hold off
% xlabel("Collect Day")
% ylabel("Sales Price")
% title("Compare Data")
% legend(["Raw" "Forecast"])

Akzeptierte Antwort

Ergin Sezgin
Ergin Sezgin am 29 Sep. 2022
Hello Arash,
As far as I understand from your case, you have an array of seconds which is a duration. If its data class is not duration yet, you can convert it as the following code:
secondsArrayDouble = (1:100000:500000)';
secondsArrayDuration = duration(0,0,secondsArrayDouble);
If you need dates like 31-Oct-2019, unfortunately you cannot convert duration to datetime without knowing at least a common fixed point in datetime where the duration takes place. Datetime is basically a point in time; whereas, duration is a length of time.
But you can create a hypothetical datetime variable and assume it as starting point in time for your duration.
hypotheticalDate = datetime("29/09/2022")
hypotheticalDate = datetime
29-Sep-2022
yourDatetime = hypotheticalDate + secondsArrayDuration
yourDatetime = 5×1 datetime array
29-Sep-2022 00:00:01 30-Sep-2022 03:46:41 01-Oct-2022 07:33:21 02-Oct-2022 11:20:01 03-Oct-2022 15:06:41
yourDatetime.Format = "dd-MMM-uuuu"
yourDatetime = 5×1 datetime array
29-Sep-2022 30-Sep-2022 01-Oct-2022 02-Oct-2022 03-Oct-2022
Using this way, you can convert your duration variable into a datetime variable and can use it in your task. However, as I said you need a fixed point in time to define datetime array with respect to your duration array.
Good luck
  3 Kommentare
Ergin Sezgin
Ergin Sezgin am 29 Sep. 2022
Hello again Arash,
Problem is related to the class of the input data and probably requires an additional step of converting. Is it possible for you to share some of your data and the regarding code that throws this error?
arash rad
arash rad am 30 Sep. 2022
I solve it thank youu very much

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by