Clear Filters
Clear Filters

Can we get datetime as an input variable?

7 views (last 30 days)
Ayesha
Ayesha on 2 Sep 2024
Edited: Luca Loiacono on 6 Sep 2024
I’m trying to predict internal temperature using BPNN, NARX, ANFIS and LSTM models. Can I take ‘datetime’ as an input variable to the model because my target variable is ‘Internal Temperature’. I only consider winter data for this model. Please help me with this. I need to know whether I can take datetime as an input variable.

Answers (2)

Anshuman
Anshuman on 2 Sep 2024
Yes, you can use 'datetime' as an input variable for your models, but you will need to preprocess it appropriately. Raw datetime values are not directly useful for most machine learning models. Here are few techniques to transform datetime into useful features:
  1. Extract Temporal Features: Break down the datetime into more granular components that can capture temporal patterns. For winter data, you might consider:
  • Hour of the day
  • Day of the week
  • Day of the month
  • Week of the year
  • Whether it's a weekend or a weekday
2. Cyclical Encoding: Since time has a cyclical nature (e.g., hours in a day, days in a week), you can use cyclical encoding to represent these features:
For hours:
  • hour_sin = sin(2 * π * hour / 24)
  • hour_cos = cos(2 * π * hour / 24)
Similarly, you can encode day of the week, month, etc.
3. Seasonality Indicators: Since you are focusing on winter data, you might want to create a binary feature indicating if the datetime falls within typical winter months for your location (e.g., December, January, February).
  1 Comment
Ayesha
Ayesha on 3 Sep 2024
Hi Anshuman, thank you very much for the explanation you provided. It’s really help me to solve my problem

Sign in to comment.


Zinea
Zinea on 3 Sep 2024
Hi Ayesha,
When predicting a time-dependent variable like internal temperature, especially with a focus on winter data, the ‘datetime’ feature can be quite informative. However, directly using raw ‘datetime’ values might not be optimal for models like BPNN, NARX, ANFIS and LSTM models. You should try extracting meaningful components from the ‘datetime’ feature to use as input as mentioned by Anshuman.
But in case you need to use ‘datetime’ as an input variable, you need to convert ‘datetime’ to numerical features to be used for prediction in models. The .CSV file named 'your_data.csv' storing the datetime values looks like this and is also attached:
You may refer to the following code for the conversion of 'datetime':
% Load data
data = readtable('your_data.csv');
% Convert DateTime to numerical features
data.DayOfYear = day(data.DateTime, 'dayofyear');
data.HourOfDay = hour(data.DateTime);
% Filter for winter data
% Assuming winter is defined as certain months, e.g., December to February
data = data(month(data.DateTime) == 12 | month(data.DateTime) == 1 | month(data.DateTime) == 2, :);
% Extract features and target
features = [data.DayOfYear, data.HourOfDay];
target = data.InternalTemperature;
You may refer to the following links for conversion from datetime to ‘day’ and ‘hour’ respectively:
  1. https://www.mathworks.com/help/matlab/ref/datetime.day.html
  2. https://www.mathworks.com/help/matlab/ref/datetime.hour.html
Hope this helps!
  2 Comments
Ayesha
Ayesha on 3 Sep 2024
Hi,Thank you very much for your prompt response and valuable explanation
Luca Loiacono
Luca Loiacono on 6 Sep 2024
Edited: Luca Loiacono on 6 Sep 2024
Hi Ayesha!
Have you implemented LSTM on your project?
I am trying to make a LSTM network similar to yours, but mine has to predict the CO2 level within a structure and I need in input a datetime variable like yours but that considers even the minutes and whether it is a working day or not.
Could you share your model or give me some suggestions on how to do this?
I am new to neural networks and Matlab, it would be very helpful!
Thanks in advance!
P.s. This is my dataset, with lower values of CO2 during the weekend and a similar trend during the weekday

Sign in to comment.

Categories

Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!