September 2019
Intermediate to advanced
420 pages
10h 29m
English
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: we use 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()Out[12]: ...
Read now
Unlock full access