#!/usr/bin/env python
__author__ = 'Xia Wang'

'''
If in the dataset we have a column of categorical variables composed of string
values, and we want to convert the string values to numerical representations,
we can use the following way. (imagine we have categories saved in a column
called data['category'])
'''


ls = list(set(data['category']))
#cat_dict = dict((j, i) for i, j in enumerate(ls))
cat_dict = {j: i for i, j in enumerate(ls)}
data['category'] = map(lambda x: cat_dict[x], data['category'])

@@ -309,16 +309,18 @@
 )%0A'''%0A%0A%0A
+# 
 ls = lis
@@ -344,16 +344,18 @@
 ory'%5D))%0A
+# 
 #cat_dic
@@ -397,16 +397,18 @@
 te(ls))%0A
+# 
 cat_dict
@@ -443,16 +443,18 @@
 te(ls)%7D%0A
+# 
 data%5B'ca
@@ -493,24 +493,556 @@
 t%5Bx%5D, data%5B'category'%5D)%0A
+%0A%0Adef categorical_to_num(categos):%0A    '''%0A    convert the categorical variables to dummy numbers.%0A    return a list that contains a dummy number corresponding to each%0A    categorical value.%0A%0A    parameters:%0A    ------------%0A    categos: the iterable that contains all the categorical values'''%0A    cat_dict = %7Bcate: num for num, cate in%0A                enumerate(sorted(list(set(categos))))%7D%0A    return tuple(map(lambda x: cat_dict%5Bx%5D, categos))%0A%0A%0Aif __name__ == '__main__':%0A    ls = list('ACBDE')%0A    print categorical_to_num(ls)%0A

change to a funciton