<p>You have a <em>classification</em> problem over a <em>time-series</em>, basically. You need to group all the events with the same ID under a single sequence, like <code>[A, A, A]</code> (is one sample) and <code>[B,B]</code> (is another).</p>
<p>Your <em>targets</em> (<span class="math-container">$y$</span>) will be the class label, for each sequence and each element of the sequence. Then the <span class="math-container">$x$</span> will comprise the three flags and &quot;other feature(s)&quot; you have.</p>
<p>You can tackle the problem with three kinds of neural networks: <strong>1D CNNs</strong> (convolutional nets), <strong>RNNs</strong> (recurrent nets), and even <strong>transformers</strong> (i.e. attention-based models).</p>
<ul>
<li>Since you have sequences of the kind <span class="math-container">$(B,T,N)$</span> - where <span class="math-container">$B$</span> is the batch size, <span class="math-container">$T$</span> the num of events with same ID (you may need to zero-pad), and <span class="math-container">$N$</span> the number of features - and have a target for each <span class="math-container">$t\in T$</span>, you need to predict the class label for each timestep <span class="math-container">$T$</span>, and each sequence in <span class="math-container">$B$</span>.</li>
<li>So you're output won't be a single value for one input sequence but a sequence of values of size <span class="math-container">$T$</span>.</li>
</ul>

CC BY-SA 4.0
2023-06-01T20:28:34.337Z
Thanks for your answer - much appreciated. So, let me elaborate this a bit. If I take data as a sequence and because I don't know the number of entries ahead of time, I will always zero pad and truncate to get a fixed length of sequence (for the above example, sample "A" would look like: [1,1,0,0.1,   0,1,0,0.3,   0,0,1,3.1] and "B" would look like: [1,1,0,0.1,   0,1,0,0.3,   0,0,0,0.0]). Is this correct? In that case, I can also use something simple like XGBoost etc.?
Zaba
CC BY-SA 4.0
2023-06-02T10:20:58.430Z
You can do that, but I was thinking about representing `A` as `[[1, 1, 0, 0.1], [0, 1, 0, 0.3], [0,0,1,3.1]]` so as a `(1,3, 4)` tensor, and `B` (with zero-pad) as `[[1,1,0,0.1], [0,1,0,0.3], [0,0,0,0.0]]`. You transform your dataset accordingly, then train a RNN by sampling $N$ of such sequences to build a mini-batch, and repeat that until convergence. Actually, if you provide `masks` to the RNN is as if having varying-length sequences. For example, the mask for `B` should be `1,1,0`. To not consider the last timestep.
Luca Anzalone
CC BY-SA 4.0
2023-05-31T07:13:43.057Z
2023-05-31T07:13:43.057Z
Luca Anzalone
<p>I am not able to find an answer to how I should classify a varying number of sequence of binary flags + other features. My data looks like this (these are events, so the order is important and I may have other features in addition to sequence):</p>
<div class="s-table-container">
<table class="s-table">
<thead>
<tr>
<th>ID</th>
<th>Flag 1</th>
<th>Flag 2</th>
<th>Flag 3</th>
<th>Other Feature</th>
</tr>
</thead>
<tbody>
<tr>
<td>A</td>
<td>1</td>
<td>1</td>
<td>0</td>
<td>0.1</td>
</tr>
<tr>
<td>A</td>
<td>0</td>
<td>1</td>
<td>0</td>
<td>0.3</td>
</tr>
<tr>
<td>A</td>
<td>0</td>
<td>0</td>
<td>1</td>
<td>3.1</td>
</tr>
<tr>
<td>B</td>
<td>0</td>
<td>1</td>
<td>0</td>
<td>1.1</td>
</tr>
<tr>
<td>B</td>
<td>1</td>
<td>1</td>
<td>0</td>
<td>0.0</td>
</tr>
</tbody>
</table>
</div>
<p>Notice that ID:B does not have the same number of entries (only 2). Any suggestion on how I should organize this data and what should I use to classify? How do I better capture the sequence of Flags? During inference, I will provide the sequence of flags [[1,1,0],[0,1,0],[0,0,1]] OR [[0,1,0],[1,1,0]] and &quot;other feature&quot; to get the label since the order of sequence makes up the positive or negative label.</p>

CC BY-SA 4.0
2023-06-01T08:35:21.960Z
Can you please put your **specific question** in the title? Thanks. "Classify sequence of flags" is not really a question.
nbro
<convolutional-neural-networks><classification><deep-neural-networks>
Classify sequence of flags
Zaba
# Classify sequence of flags

(Asked by: username_0 on 2023-05-29)

I am not able to find an answer to how I should classify a varying number of sequence of binary flags + other features. My data looks like this (these are events, so the order is important and I may have other features in addition to sequence):

| ID | Flag 1 | Flag 2 | Flag 3 | Other Feature |
| --- | --- | --- | --- | --- |
| A | 1 | 1 | 0 | 0.1 |
| A | 0 | 1 | 0 | 0.3 |
| A | 0 | 0 | 1 | 3.1 |
| B | 0 | 1 | 0 | 1.1 |
| B | 1 | 1 | 0 | 0.0 |

Notice that ID:B does not have the same number of entries (only 2). Any suggestion on how I should organize this data and what should I use to classify? How do I better capture the sequence of Flags? During inference, I will provide the sequence of flags [[1,1,0],[0,1,0],[0,0,1]] OR [[0,1,0],[1,1,0]] and "other feature" to get the label since the order of sequence makes up the positive or negative label.

**Tags:** convolutional-neural-networks, classification, deep-neural-networks

> **Comments:**
> **username_1**: Can you please put your **specific question** in the title? Thanks. "Classify sequence of flags" is not really a question.  

---

## 1 answers

### Answer by username_2 on 2023-05-31. Score: 0

You have a *classification* problem over a *time-series*, basically. You need to group all the events with the same ID under a single sequence, like `[A, A, A]` (is one sample) and `[B,B]` (is another).

Your *targets* ($y$) will be the class label, for each sequence and each element of the sequence. Then the $x$ will comprise the three flags and "other feature(s)" you have.

You can tackle the problem with three kinds of neural networks: **1D CNNs** (convolutional nets), **RNNs** (recurrent nets), and even **transformers** (i.e. attention-based models).

* Since you have sequences of the kind $(B,T,N)$ - where $B$ is the batch size, $T$ the num of events with same ID (you may need to zero-pad), and $N$ the number of features - and have a target for each $t\in T$, you need to predict the class label for each timestep $T$, and each sequence in $B$.
* So you're output won't be a single value for one input sequence but a sequence of values of size $T$.

> **Comments:**
> **username_0**: Thanks for your answer - much appreciated. So, let me elaborate this a bit. If I take data as a sequence and because I don't know the number of entries ahead of time, I will always zero pad and truncate to get a fixed length of sequence (for the above example, sample "A" would look like: [1,1,0,0.1,   0,1,0,0.3,   0,0,1,3.1] and "B" would look like: [1,1,0,0.1,   0,1,0,0.3,   0,0,0,0.0]). Is this correct? In that case, I can also use something simple like XGBoost etc.?  
> **username_2**: You can do that, but I was thinking about representing `A` as `[[1, 1, 0, 0.1], [0, 1, 0, 0.3], [0,0,1,3.1]]` so as a `(1,3, 4)` tensor, and `B` (with zero-pad) as `[[1,1,0,0.1], [0,1,0,0.3], [0,0,0,0.0]]`. You transform your dataset accordingly, then train a RNN by sampling $N$ of such sequences to build a mini-batch, and repeat that until convergence. Actually, if you provide `masks` to the RNN is as if having varying-length sequences. For example, the mask for `B` should be `1,1,0`. To not consider the last timestep.  

---

