๐ DateEncodingLayer
๐ DateEncodingLayer
๐ฏ Overview
The DateEncodingLayer takes date components (year, month, day, day of week) and encodes them into cyclical features using sine and cosine transformations. This approach preserves the cyclical nature of temporal data, which is crucial for neural networks to understand patterns like seasonality and periodicity.
This layer is particularly powerful for time series analysis where the cyclical nature of dates is important, such as seasonal patterns, weekly cycles, and daily rhythms.
๐ How It Works
The DateEncodingLayer processes date components through cyclical encoding:
- Component Extraction: Extracts year, month, day, and day of week
- Year Normalization: Normalizes year to [0, 1] range based on min/max years
- Cyclical Encoding: Applies sine and cosine transformations to each component
- Feature Combination: Combines all cyclical encodings into a single tensor
- Output Generation: Produces 8-dimensional cyclical feature vector
graph TD
A[Date Components: year, month, day, day_of_week] --> B[Year Normalization]
B --> C[Cyclical Encoding]
C --> D[Year: sin(2ฯ * year_norm), cos(2ฯ * year_norm)]
C --> E[Month: sin(2ฯ * month/12), cos(2ฯ * month/12)]
C --> F[Day: sin(2ฯ * day/31), cos(2ฯ * day/31)]
C --> G[Day of Week: sin(2ฯ * dow/7), cos(2ฯ * dow/7)]
D --> H[Combine All Encodings]
E --> H
F --> H
G --> H
H --> I[Cyclical Features: 8 dimensions]
style A fill:#e6f3ff,stroke:#4a86e8
style I fill:#e8f5e9,stroke:#66bb6a
style B fill:#fff9e6,stroke:#ffb74d
style C fill:#f3e5f5,stroke:#9c27b0
style H fill:#e1f5fe,stroke:#03a9f4
๐ก Why Use This Layer?
| Challenge | Traditional Approach | DateEncodingLayer's Solution |
|---|---|---|
| Cyclical Nature | Treats dates as linear values | ๐ฏ Preserves cyclicality with sine/cosine encoding |
| Seasonal Patterns | Misses seasonal relationships | โก Captures seasonality through cyclical encoding |
| Neural Network Understanding | Linear encoding confuses networks | ๐ง Neural-friendly cyclical representation |
| Temporal Relationships | Loses temporal proximity | ๐ Maintains temporal relationships through encoding |
๐ Use Cases
- Time Series Analysis: Encoding temporal features for neural networks
- Seasonal Pattern Recognition: Capturing seasonal and cyclical patterns
- Event Prediction: Predicting events based on temporal patterns
- Financial Analysis: Analyzing financial data with temporal components
- Weather Forecasting: Processing weather data with seasonal patterns
๐ Quick Start
Basic Usage
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
In a Sequential Model
1 2 3 4 5 6 7 8 9 10 11 | |
In a Functional Model
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
Advanced Configuration
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | |
๐ API Reference
kerasfactory.layers.DateEncodingLayer
DateEncodingLayer for encoding date components into cyclical features.
This layer takes date components (year, month, day, day of week) and encodes them into cyclical features using sine and cosine transformations.
Classes
DateEncodingLayer
1 2 3 | |
Layer for encoding date components into cyclical features.
This layer takes date components (year, month, day, day of week) and encodes them into cyclical features using sine and cosine transformations. The year is normalized to a range between 0 and 1 based on min_year and max_year.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
min_year |
int
|
Minimum year for normalization (default: 1900) |
1900
|
max_year |
int
|
Maximum year for normalization (default: 2100) |
2100
|
**kwargs |
Additional layer arguments |
{}
|
Input shape
Tensor with shape: (..., 4) containing [year, month, day, day_of_week]
Output shape
Tensor with shape: (..., 8) containing cyclical encodings:
[year_sin, year_cos, month_sin, month_cos, day_sin, day_cos, dow_sin, dow_cos]
Initialize the layer.
Source code in kerasfactory/layers/DateEncodingLayer.py
34 35 36 37 38 39 40 41 42 43 44 | |
Functions
1 | |
Compute the output shape of the layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_shape |
Shape of the input tensor |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, ...]
|
Output shape |
Source code in kerasfactory/layers/DateEncodingLayer.py
97 98 99 100 101 102 103 104 105 106 | |
๐ง Parameters Deep Dive
min_year (int)
- Purpose: Minimum year for normalization
- Default: 1900
- Impact: Affects year normalization range
- Recommendation: Set based on your data's year range
max_year (int)
- Purpose: Maximum year for normalization
- Default: 2100
- Impact: Affects year normalization range
- Recommendation: Set based on your data's year range
๐ Performance Characteristics
- Speed: โกโกโกโก Very fast - simple mathematical operations
- Memory: ๐พ Low memory usage - no additional parameters
- Accuracy: ๐ฏ๐ฏ๐ฏ๐ฏ Excellent for cyclical temporal features
- Best For: Time series data requiring cyclical encoding
๐จ Examples
Example 1: Seasonal Pattern Analysis
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | |
Example 2: Business Cycle Analysis
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | |
Example 3: Cyclical Feature Analysis
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | |
๐ก Tips & Best Practices
- Year Range: Set min_year and max_year based on your data's actual range
- Input Format: Input must be [year, month, day, day_of_week] format
- Cyclical Nature: The encoding preserves cyclical relationships
- Neural Networks: Works well with neural networks for temporal patterns
- Seasonality: Excellent for capturing seasonal and cyclical patterns
- Integration: Combines well with other temporal processing layers
โ ๏ธ Common Pitfalls
- Input Shape: Must be (..., 4) tensor with date components
- Year Range: min_year must be less than max_year
- Component Order: Must be [year, month, day, day_of_week] in that order
- Data Type: Input should be float32 tensor
- Missing Values: Doesn't handle missing values - preprocess first
๐ Related Layers
- DateParsingLayer - Date string parsing
- SeasonLayer - Seasonal information extraction
- CastToFloat32Layer - Type casting utility
- DifferentiableTabularPreprocessor - End-to-end preprocessing
๐ Further Reading
- Cyclical Encoding in Time Series - Cyclical encoding concepts
- Sine and Cosine Transformations - Trigonometric functions
- Time Series Feature Engineering - Feature engineering techniques
- KerasFactory Layer Explorer - Browse all available layers
- Data Preprocessing Tutorial - Complete guide to data preprocessing