July 2017
Intermediate to advanced
382 pages
9h 13m
English
In order for our data to be understood by the decision tree algorithm, we need to convert all categorical features ('sex', 'BP', and 'cholesterol') into numerical features. What is the best way to do that?
Exactly, using scikit-learn's DictVectorizer. Like we did in the previous chapter, we feed the dataset that we want to convert to the fit_transform method:
In [10]: from sklearn.feature_extraction import DictVectorizer... vec = DictVectorizer(sparse=False)... data_pre = vec.fit_transform(data)
Then, data_pre contains the preprocessed data. If we want to look at the first data point (that is, the first row of data_pre), we match the feature names with the corresponding feature values:
In [12]: vec.get_feature_names() ...
Read now
Unlock full access