# Creating a car anomaly detection with Multiple field

**URL:** <https://discourse.numenta.org/t/creating-a-car-anomaly-detection-with-multiple-field/8544>\
**Category:** NuPIC\
**Tags:** question, multiple-inputs\
**Created:** [May 5, 2021, 10:40pm UTC](https://discourse.numenta.org/t/creating-a-car-anomaly-detection-with-multiple-field/8544 "2021-05-05T22:40:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![thos1996](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/thos1996/32/5586_2.png) [@thos1996](https://discourse.numenta.org/u/thos1996)\
**Post date:** [May 5, 2021, 10:40pm UTC](https://discourse.numenta.org/t/creating-a-car-anomaly-detection-with-multiple-field/8544/1 "2021-05-05T22:40:34Z")

</div>

Greetings,  
It’s been a week since I started working on HTM’s, I am a little lost. So we are working on anomaly detection for autonomous cars and we are recording the data like position, velocity, acceleration and predecessor distance fields.  
Max 5 fields with a timestamp.  
Here are my questions  
Q1. Is it better to work with individual models or create a single model with multiple inputs, we are not interested in which field cause the anomaly but would be really great if the model can take advantage of the correlation between the fields. (I was also not able to find any implementations or guide for the multifield model, so any direction will be helpful)  
Q2. I want to create anomaly windows, but I can’t figure out the best way to do that?  
I am working with python on my local with CSV files btw

---

<div class="post-metadata">

**Author:** ![sheiser1](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/sheiser1/32/766_2.png) [@sheiser1](https://discourse.numenta.org/u/sheiser1)\
**Post date:** [May 6, 2021, 6:51pm UTC](https://discourse.numenta.org/t/creating-a-car-anomaly-detection-with-multiple-field/8544/2 "2021-05-06T18:51:23Z")

</div>

Hi @thos1996, welcome!

> [@thos1996](#):
>
> Q1. Is it better to work with individual models or create a single model with multiple inputs, we are not interested in which field cause the anomaly but would be really great if the model can take advantage of the correlation between the fields.

With just 5 fields plus timestamp either way could work.

The multi-field model has the advantage of potentially finding relationships between the fields, and the single-field model has the advantage of showing different activity for each field.

I generally like the approach of multi single-field models, where an anomaly is raised when a certain % of the fields are being anomalous at the same time. I’d say it’s worth trying both tho.

> [@thos1996](#):
>
> Q2. I want to create anomaly windows, but I can’t figure out the best way to do that?

I’d create a data structure that stores the anomaly scores/likelihoods of each field for all time steps thus far (or the last n time steps). Then craft a sliding window logic which uses that info in some way. A simple baseline could just be: ‘any model is anomalous at all times where its last n anomaly likelihood vales average over 0.9’.

> [@thos1996](#):
>
> I was also not able to find any implementations or guide for the multifield model, so any direction will be helpful

Here are a couple threads about handling multiple input fields:

> [@Can HTM be used on multivarient time series problems?](https://discourse.numenta.org/t/can-htm-be-used-on-multivarient-time-series-problems/3316/24):
>
> Hey @steinroe, so I have my own python module which builds multiple NuPIC anomaly detection models from multivariate time series. It samples the first N rows to calibrate the encoders, then instantiates the models, applies them to the rest of the rows and saves them to disk. This yields a separate anomaly score for each field (csv column) at each time step, which can then be combined, thresholded or otherwise analyzed. I made it for my own research involving n-dimensional movements but general…

> [@Multi-variate data](https://discourse.numenta.org/t/multi-variate-data/8531):
>
> Hi all, I have question-related to multi-variate data. I have to find anomalies in 2000+ sensors of different characteristics, some sensors may have trend in it. Will HTM handles trend and find the anomalies? or do we need to remove trend? Also, do we need to have 2000+ HTM models? and how much time it may take for time series having 200,000 points?

Best of luck!

---

<div class="post-metadata">

**Author:** ![thos1996](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/thos1996/32/5586_2.png) [@thos1996](https://discourse.numenta.org/u/thos1996)\
**Post date:** [May 9, 2021, 10:23am UTC](https://discourse.numenta.org/t/creating-a-car-anomaly-detection-with-multiple-field/8544/3 "2021-05-09T10:23:55Z")

</div>

This is the GitHub link:- [GitHub - tushar-1996/Anamoly-Detection-using-HTM](https://github.com/tushar-1996/Anamoly-Detection-using-HTM)

I have made some changes to the hotgym example. I am able to encode the data but I am not able to figure out what to use instead of “consumption” in line 195,199 because I have multiple variables now.

I want to do unsupervised learning so it is okay if there is no other way to generate anomaly windows.  
Also timestamps are not included because the data I have, has 40 values per second so I don’t know how to encode it.

Any suggests on improving the code is welcome, right now I am doing multi-field model.

---

<div class="post-metadata">

**Author:** ![sheiser1](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/sheiser1/32/766_2.png) [@sheiser1](https://discourse.numenta.org/u/sheiser1)\
**Post date:** [May 10, 2021, 1:48am UTC](https://discourse.numenta.org/t/creating-a-car-anomaly-detection-with-multiple-field/8544/4 "2021-05-10T01:48:36Z")

</div>

> [@thos1996](#):
>
> I am not able to figure out what to use instead of “consumption” in line 195,199 because I have multiple variables now.

‘Consumption’ is the metric being predicted in the hot gym example, so in your case that would be replaced by acceleration or velocity or something – but you don’t actually need the ‘Predictor’ object at all for anomaly detection!

You can get the anomaly score and thus anomaly likelihood right from the TM object. The classifier you have (‘Predictor’ object) is only needed to get prediction output in the raw data unit of measurement – but that isn’t actually used for anomaly detection with HTM, since its based on anomaly scores.

So basically you can just skip the ‘Predictor’, and just get anomaly score from the TM. You can do this by comparing the predicted active cells from TM to the total active cells. The proportion of active cells which were NOT predicted is the anomaly score.

---

<div class="post-metadata">

**Author:** ![SongHunter](https://avatars.discourse-cdn.com/v4/letter/s/779978/32.png) [@SongHunter](https://discourse.numenta.org/u/SongHunter)\
**Post date:** [August 5, 2021, 11:51am UTC](https://discourse.numenta.org/t/creating-a-car-anomaly-detection-with-multiple-field/8544/5 "2021-08-05T11:51:50Z")

</div>

Then craft a sliding window logic which uses that info in some way. A simple baseline could just be: ‘any model is anomalous at all times where its last n anomaly likelihood vales average over 0.9’[.](https://mcdvoice.me/)

---

<div class="post-metadata">

**Author:** ![Andrew9700](https://avatars.discourse-cdn.com/v4/letter/a/9e8a1a/32.png) [@Andrew9700](https://discourse.numenta.org/u/Andrew9700)\
**Post date:** [October 8, 2021, 3:34pm UTC](https://discourse.numenta.org/t/creating-a-car-anomaly-detection-with-multiple-field/8544/6 "2021-10-08T15:34:48Z")

</div>

I want to do unsupervised learning so it is okay if there is no other way to generate anomaly windows[.](https://www.jcpenneykiosk.bid/) Also timestamps are not included because the data I have, has 40 values per second so I don’t know how to encode it.
