2017-02-16 45 views
2

我使用银行数据来预测每天的票数。我正在使用堆栈来获得更准确的结果,并使用库。蟒蛇 - 堆叠分类器:适合数据时的IndexError

以下是重要特征的样本数据集:

[enter image description here] 这里是目标属性样品:

[enter image description here]

这里是代码:

from stacked_generalization.lib.stacking import StackedClassifier 
from sklearn.ensemble import RandomForestClassifier 
from sklearn.linear_model import LogisticRegression, RidgeClassifier 
# Stage 1 model 
bclf = LogisticRegression(random_state=1) 

# Stage 0 models 
clfs = [RandomForestClassifier(n_estimators=40, criterion = 'gini', random_state=1), 
     gbm, 
     RidgeClassifier(random_state=1)] 

sl = StackedClassifier(bclf, clfs) 
sl.fit(training.select_columns(features).to_dataframe().as_matrix(), np.array(training['class'])) 

这里是训练数据格式:

[[ 21 11 2014 46 4 3] 
[ 22 11 2014 46 5 4] 
[ 24 11 2014 47 0 4] 
..., 
[ 30 9 2016 39 4 5] 
[ 3 10 2016 40 0 1] 
[ 4 10 2016 40 1 1]] 

现在,当我尝试拟合模型,它提供了以下错误: enter image description here

不过,我比较我与库中给出的示例代码,但还是无法弄清楚我在哪里我错了。请帮助我。

+0

什么是培训对象的格式? – Prophecies

+0

更新了我的答案,首先它是在sframe中,然后我将它转换为numpy nd数组 – user1584253

+0

错误与数组索引有关。 c.classes_是嵌套列表或除整数或布尔值之外的列表。他们有没有机会? – Benjamin

回答

1

我有一个类似的问题,似乎只是在酿造中的错误需要修复。问题是c.classes_(或类的数量)返回一个浮点数的numpy数组(例如,如果你有两个类,它返回[0.0,1.0]而不是整数([0,1])。使用这些花车到索引中的列,但你不能索引浮筒一个numpy的列

probas.shape =#行=#训练实例;#=列班

c.predict_proba(X)回报probabilites#每个类每个训练示例

probas[:, list(c.classes_)] = c.predict_proba(X) 

应该将每个类的每个行在X中的概率放入使用class#的probas中以指向co列在probas。

如果添加astype(INT)本会工作

probas[:, list(et.classes_.astype(int))] = et.predict_proba(X)

或只是

probas = np.copy(et.predict_proba(X))