# How to encode categorical data using CategoryEncoder

**URL:** <https://discourse.numenta.org/t/how-to-encode-categorical-data-using-categoryencoder/3661>\
**Category:** NuPIC\
**Created:** [March 20, 2018, 9:46am UTC](https://discourse.numenta.org/t/how-to-encode-categorical-data-using-categoryencoder/3661 "2018-03-20T09:46:37Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![white](https://avatars.discourse-cdn.com/v4/letter/w/eb8c5e/32.png) [@white](https://discourse.numenta.org/u/white)\
**Post date:** [March 20, 2018, 9:46am UTC](https://discourse.numenta.org/t/how-to-encode-categorical-data-using-categoryencoder/3661/1 "2018-03-20T09:46:37Z")

</div>

I am trying to use `CategoryEncoder` for categorical data for prediction.

In my case, I predict `value` based on `caller` and `value`.  
The csv header and sample data is as follows, where `caller` is categorical data, and specified as `C` in header:

```
func,caller,callee,timestamp,value
string,string,string,datetime,float
,C,,T,
ord_IInvQueryCSV_funcQ:127.0.1.1,funcK,com.gyl.scm.center.query.service.impl.InvQueryCSVImpl.funcQ:127.0.1.1,2018-01-01 03:26:13.960,48

```

Here is the corresponding encoding part for categorical data `caller` from model param in JSON format

```
            "caller": {
                "name": "caller",
                "fieldname": "caller",
                "w": 21,
                "categoryList": ["funcK","funcQ","funcM"],
                "type": "CategoryEncoder"
            }

```

It comes to an error while running program. However, the error disappear when the special flag `C` is not set in csv header. I am not sure whether the `caller` field is involved in prediction without `C` flag.

Could anyone tell me the right usage of categorical data?

Thanks

---

<div class="post-metadata">

**Author:** ![rhyolight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/rhyolight/32/3922_2.png) [@rhyolight](https://discourse.numenta.org/u/rhyolight)\
**Post date:** [March 20, 2018, 1:36pm UTC](https://discourse.numenta.org/t/how-to-encode-categorical-data-using-categoryencoder/3661/2 "2018-03-20T13:36:03Z")

</div>

What is the error you mentioned?

---

<div class="post-metadata">

**Author:** ![white](https://avatars.discourse-cdn.com/v4/letter/w/eb8c5e/32.png) [@white](https://discourse.numenta.org/u/white)\
**Post date:** [March 20, 2018, 2:35pm UTC](https://discourse.numenta.org/t/how-to-encode-categorical-data-using-categoryencoder/3661/3 "2018-03-20T14:35:49Z")

</div>

> [@rhyolight](#):
>
> What is the error you mentioned?

```
    data_source = FileRecordStream(streamID=input_path)
  File "/usr/local/lib/python2.7/dist-packages/nupic/data/file_record_stream.py", line 249, in __init__
    FieldMetaType.integer)
AssertionError

```

---

<div class="post-metadata">

**Author:** ![rhyolight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/rhyolight/32/3922_2.png) [@rhyolight](https://discourse.numenta.org/u/rhyolight)\
**Post date:** [March 20, 2018, 2:38pm UTC](https://discourse.numenta.org/t/how-to-encode-categorical-data-using-categoryencoder/3661/4 "2018-03-20T14:38:32Z")

</div>

That’s a strange error. Can you take a look at [this example](https://github.com/numenta/nupic/tree/master/examples/prediction/category_prediction) and see if it helps?

---

<div class="post-metadata">

**Author:** ![white](https://avatars.discourse-cdn.com/v4/letter/w/eb8c5e/32.png) [@white](https://discourse.numenta.org/u/white)\
**Post date:** [March 20, 2018, 3:22pm UTC](https://discourse.numenta.org/t/how-to-encode-categorical-data-using-categoryencoder/3661/5 "2018-03-20T15:22:27Z")

</div>

> [@rhyolight](#):
>
> That’s a strange error. Can you take a look at this example and see if it helps?

That should helps. It seems that the encoding subroutine w.r.t. categorical data is implemented in the user-defined snippet, rather than by nupic automatically, and `C` flag does not have to be set in the csv file as well.

---

<div class="post-metadata">

**Author:** ![rhyolight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/rhyolight/32/3922_2.png) [@rhyolight](https://discourse.numenta.org/u/rhyolight)\
**Post date:** [March 20, 2018, 4:00pm UTC](https://discourse.numenta.org/t/how-to-encode-categorical-data-using-categoryencoder/3661/6 "2018-03-20T16:00:21Z")

</div>

Those file headers are used for swarming. Other than that, some example programs might read them, but most just ignore them. Remember you don’t need a CSV file to do this, you can just feed one row at a time from anywhere.

---

<div class="post-metadata">

**Author:** ![white](https://avatars.discourse-cdn.com/v4/letter/w/eb8c5e/32.png) [@white](https://discourse.numenta.org/u/white)\
**Post date:** [March 21, 2018, 1:04am UTC](https://discourse.numenta.org/t/how-to-encode-categorical-data-using-categoryencoder/3661/7 "2018-03-21T01:04:18Z")

</div>

> [@rhyolight](#):
>
> Those file headers are used for swarming. Other than that, some example programs might read them, but most just ignore them. Remember you don’t need a CSV file to do this, you can just feed one row at a time from anywhere

Great!

---

<div class="post-metadata">

**Author:** ![white](https://avatars.discourse-cdn.com/v4/letter/w/eb8c5e/32.png) [@white](https://discourse.numenta.org/u/white)\
**Post date:** [March 21, 2018, 6:22am UTC](https://discourse.numenta.org/t/how-to-encode-categorical-data-using-categoryencoder/3661/8 "2018-03-21T06:22:35Z")

</div>

@rhyolight Thanks for your prompt reply!

I have a bunch of functions, and the task aims at predicting the elapsed time of these functions’ call. The corresponding data sample is as follows

```
function_name,timestamp,elapsed_time
funcA,2018-01-01 03:26:13.960,48
funcD,2018-01-01 04:23:16.187,51
funcB,2018-01-01 04:24:26.957,43
funcC,2018-01-01 04:25:17.428,27
funcA,2018-01-01 04:26:38.059,41
funcB,2018-01-01 04:26:19.097,31
funcC,2018-01-01 04:26:59.376,26

```

One can see that all the records w.r.t. different functions’ call are put together in one data file, I am asking whether it is possible to do the prediction for these functions in one model? or I have to create one model for each function to implement prediction respectively?

Thanks

---

<div class="post-metadata">

**Author:** ![rhyolight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/rhyolight/32/3922_2.png) [@rhyolight](https://discourse.numenta.org/u/rhyolight)\
**Post date:** [March 21, 2018, 3:40pm UTC](https://discourse.numenta.org/t/how-to-encode-categorical-data-using-categoryencoder/3661/9 "2018-03-21T15:40:05Z")

</div>

You’ll likely have more success if you split it up into different models.

---

<div class="post-metadata">

**Author:** ![white](https://avatars.discourse-cdn.com/v4/letter/w/eb8c5e/32.png) [@white](https://discourse.numenta.org/u/white)\
**Post date:** [March 21, 2018, 11:54pm UTC](https://discourse.numenta.org/t/how-to-encode-categorical-data-using-categoryencoder/3661/10 "2018-03-21T23:54:58Z")

</div>

> [@rhyolight](#):
>
> You’ll likely have more success if you split it up into different models.

The point is there are hundreds or thousands of function probably. That’s a big issue, any advice? Thanks

BTW, I found a interesting phenomenon that the anomalyScore is big even though the difference between the actual value and predicted value is small sometimes. How to explain it?

---

<div class="post-metadata">

**Author:** ![rhyolight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/rhyolight/32/3922_2.png) [@rhyolight](https://discourse.numenta.org/u/rhyolight)\
**Post date:** [March 22, 2018, 4:36pm UTC](https://discourse.numenta.org/t/how-to-encode-categorical-data-using-categoryencoder/3661/11 "2018-03-22T16:36:04Z")

</div>

> [@white](#):
>
> the anomalyScore is big even though the difference between the actual value and predicted value is small sometimes. How to explain it?

See: [Clarification on anomaly score and anomalylikelihood](https://discourse.numenta.org/t/clarification-on-anomaly-score-and-anomalylikelihood/2643)
