如何处理sklearn GradientBoostingClassifier中的分类变量?

时间:2014-07-11 21:15:58

标签: python machine-learning scikit-learn decision-tree ensemble-learning

我正在尝试使用分类变量训练GradientBoostingClassifier的模型。

以下是原始代码示例,仅用于尝试将分类变量输入GradientBoostingClassifier

from sklearn import datasets
from sklearn.ensemble import GradientBoostingClassifier
import pandas

iris = datasets.load_iris()
# Use only data for 2 classes.
X = iris.data[(iris.target==0) | (iris.target==1)]
Y = iris.target[(iris.target==0) | (iris.target==1)]

# Class 0 has indices 0-49. Class 1 has indices 50-99.
# Divide data into 80% training, 20% testing.
train_indices = list(range(40)) + list(range(50,90))
test_indices = list(range(40,50)) + list(range(90,100))
X_train = X[train_indices]
X_test = X[test_indices]
y_train = Y[train_indices]
y_test = Y[test_indices]

X_train = pandas.DataFrame(X_train)

# Insert fake categorical variable. 
# Just for testing in GradientBoostingClassifier.
X_train[0] = ['a']*40 + ['b']*40

# Model.
clf = GradientBoostingClassifier(learning_rate=0.01,max_depth=8,n_estimators=50).fit(X_train, y_train)

出现以下错误:

ValueError: could not convert string to float: 'b'

从我收集的内容来看,在GradientBoostingClassifier构建模型之前,似乎需要One Hot Encoding分类变量。

GradientBoostingClassifier可以使用分类变量构建模型而无需进行一次热编码吗?

R gbm包能够处理上面的样本数据。我正在寻找具有同等功能的Python库。

2 个答案:

答案 0 :(得分:11)

pandas.get_dummiesstatsmodels.tools.tools.categorical可用于将分类变量转换为虚拟矩阵。然后我们可以将虚拟矩阵合并回训练数据。

以下是执行上述程序的问题的示例代码。

from sklearn import datasets
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.metrics import roc_curve,auc
from statsmodels.tools import categorical
import numpy as np

iris = datasets.load_iris()
# Use only data for 2 classes.
X = iris.data[(iris.target==0) | (iris.target==1)]
Y = iris.target[(iris.target==0) | (iris.target==1)]

# Class 0 has indices 0-49. Class 1 has indices 50-99.
# Divide data into 80% training, 20% testing.
train_indices = list(range(40)) + list(range(50,90))
test_indices = list(range(40,50)) + list(range(90,100))
X_train = X[train_indices]
X_test = X[test_indices]
y_train = Y[train_indices]
y_test = Y[test_indices]


###########################################################################
###### Convert categorical variable to matrix and merge back with training
###### data.

# Fake categorical variable.
catVar = np.array(['a']*40 + ['b']*40)
catVar = categorical(catVar, drop=True)
X_train = np.concatenate((X_train, catVar), axis = 1)

catVar = np.array(['a']*10 + ['b']*10)
catVar = categorical(catVar, drop=True)
X_test = np.concatenate((X_test, catVar), axis = 1)
###########################################################################

# Model and test.
clf = GradientBoostingClassifier(learning_rate=0.01,max_depth=8,n_estimators=50).fit(X_train, y_train)

prob = clf.predict_proba(X_test)[:,1]   # Only look at P(y==1).

fpr, tpr, thresholds = roc_curve(y_test, prob)
roc_auc_prob = auc(fpr, tpr)

print(prob)
print(y_test)
print(roc_auc_prob)

感谢Andreas Muller指示大熊猫Dataframe不应该用于scikit-learn估算器。

答案 1 :(得分:-4)

当然它可以处理它,你只需要将分类变量编码为管道上的一个单独步骤。 Sklearn完全能够处理分类变量以及R或任何其他ML包。 R包仍然(可能)在幕后进行一次热编码,在这种情况下它并没有将编码和拟合的问题分开(正如它可能应该这样)。