我尝试使用 python 和 sklearn 启动决策树。工作方法是这样的:
import pandas as pd
from sklearn import tree
for col in set(train.columns):
if train[col].dtype == np.dtype('object'):
s = np.unique(train[col].values)
mapping = pd.Series([x[0] for x in enumerate(s)], index = s)
train_fea = train_fea.join(train[col].map(mapping))
else:
train_fea = train_fea.join(train[col])
dt = tree.DecisionTreeClassifier(min_samples_split=3,
compute_importances=True,max_depth=5)
dt.fit(train_fea, labels)
现在我尝试用 DictVectorizer 做同样的事情,但我的代码不起作用:
from sklearn.feature_extraction import DictVectorizer
vec = DictVectorizer(sparse=False)
train_fea = vec.fit_transform([dict(enumerate(sample)) for sample in train])
dt = tree.DecisionTreeClassifier(min_samples_split=3,
compute_importances=True,max_depth=5)
dt.fit(train_fea, labels)
我在最后一行出现错误:“ValueError:标签数=332448 与样本数=55 不匹配”。正如我从文档中了解到的那样,DictVectorize 旨在将名义特征转换为数字特征。我做错了什么?
更正(感谢 ogrisel 推动我举一个完整的例子):
import pandas as pd
import numpy as np
from sklearn import tree
##################################
# working example
train = pd.DataFrame({'a' : ['a', 'b', 'a'], 'd' : ['e', 'e', 'f'],
'b' : [0, 1, 1], 'c' : ['b', 'c', 'b']})
columns = set(train.columns)
columns.remove('b')
train_fea = train[['b']]
for col in columns:
if train[col].dtype == np.dtype('object'):
s = np.unique(train[col].values)
mapping = pd.Series([x[0] for x in enumerate(s)], index = s)
train_fea = train_fea.join(train[col].map(mapping))
else:
train_fea = train_fea.join(train[col])
dt = tree.DecisionTreeClassifier(min_samples_split=3,
compute_importances=True,max_depth=5)
dt.fit(train_fea, train['c'])
##########################################
# example with DictVectorizer and error
from sklearn.feature_extraction import DictVectorizer
vec = DictVectorizer(sparse=False)
train_fea = vec.fit_transform([dict(enumerate(sample)) for sample in train])
dt = tree.DecisionTreeClassifier(min_samples_split=3,
compute_importances=True,max_depth=5)
dt.fit(train_fea, train['c'])
最后的代码是在 ogrisel 的帮助下修复的:
import pandas as pd
from sklearn import tree
from sklearn.feature_extraction import DictVectorizer
from sklearn import preprocessing
train = pd.DataFrame({'a' : ['a', 'b', 'a'], 'd' : ['e', 'x', 'f'],
'b' : [0, 1, 1], 'c' : ['b', 'c', 'b']})
# encode labels
labels = train[['c']]
le = preprocessing.LabelEncoder()
labels_fea = le.fit_transform(labels)
# vectorize training data
del train['c']
train_as_dicts = [dict(r.iteritems()) for _, r in train.iterrows()]
train_fea = DictVectorizer(sparse=False).fit_transform(train_as_dicts)
# use decision tree
dt = tree.DecisionTreeClassifier()
dt.fit(train_fea, labels_fea)
# transform result
predictions = le.inverse_transform(dt.predict(train_fea).astype('I'))
predictions_as_dataframe = train.join(pd.DataFrame({"Prediction": predictions}))
print predictions_as_dataframe
一切正常