尝试堆叠时出现TypeError

时间:2017-05-18 05:13:44

标签: python-2.7 machine-learning scikit-learn sklearn-pandas

非常感谢这里的一些帮助......我正在尝试实现堆叠,但基于此代码,我不断获得TypeError: object of type 'generator' has no len()。有谁知道如何纠正这个?非常感谢。

y_lr = clf_lr.predict(X_test)     # Linear 
y_rf = forest.predict(X_test)     # Random Forest
y_gb = clf_gb.predict(X_test)     # Gradient Boosting

dtest = xgb.DMatrix(X_test)
y_xgb = clf_xgb.predict(dtest)    # XGBoost

y_nn = model_nn.predict(X_test)   # DNN

stack = pd.DataFrame(data={'lr':y_lr, 'gb':y_gb, 'xgb':y_xgb, 'nn':y_nn, 'true':y_test})

这是我在data = {'rf':y_rf, 'gb':y_gb, 'xgb':y_xgb, 'nn':y_nn, 'true':y_test}打印data时所得到的:

{'gb': array([  5176163.73806255,   6717797.72382604,   7079943.66873864, ...,
         12224999.12632363,   6632903.39968627,   7314008.41080324]),
 'nn': <generator object _as_iterable at 0x7f535ca0a780>,
 'rf': array([  3525000. ,   6713017.2,   5577500. , ...,  11708300. ,
          6255000. ,   6290000. ]),
 'true': 1715      5200000.0
 17126     6796548.0
 28143     7300000.0
 10037    12581315.0
 16133     7500000.0
 17356     6450000.0
 28348     7300000.0
 24818     6100000.0
 2240      5000000.0
 25878     3300000.0
 8533      4058255.0
 4374      5063160.0
 29140     6200000.0
 16599     3606128.0
 5647      6500000.0
 4878      4347200.0
 30267     7500000.0
 18793     4762800.0
 22865     5850000.0
 20585     6600000.0
 1166      3000000.0
 21417     6100000.0
 13557     4200000.0
 8716     10000000.0
 1486      8000000.0
 7916      4650776.0
 28010     8600000.0
 21926     5972181.0
 3567      4498491.0
 6729      6850000.0
             ...    
 1224      5550000.0
 19234     5100000.0
 1201      9500000.0
 11412     5000000.0
 27141     6623516.0
 28107     6800000.0
 19347     3834328.0
 17965     8300000.0
 18584     6440000.0
 11473     6518400.0
 16907    11200000.0
 28412     5950000.0
 18744     5700000.0
 15247     7000000.0
 19411     6907232.0
 3185      8000000.0
 6348      3413300.0
 19544     4800000.0
 21309    12800000.0
 10733     5107200.0
 17367     6900000.0
 11761    14500000.0
 27435     7251680.0
 13039     6300000.0
 1966     12800000.0
 3664      4506800.0
 3626      4467294.0
 25682    15250000.0
 8988      1000000.0
 24637     7700000.0
 Name: price_doc, dtype: float64,
 'xgb': array([  4634399. ,   5984703. ,   6499839.5, ...,  12502588. ,
          6457020.5,   7572096. ], dtype=float32)}

1 个答案:

答案 0 :(得分:0)

尝试不同的迭代后,这是有效的代码!

y_lr = list(clf_lr.predict(X_test))     # Linear 
y_rf = list(forest.predict(X_test))     # Random Forest
y_gb = list(clf_gb.predict(X_test))     # Gradient Boosting

dtest = xgb.DMatrix(X_test)
y_xgb = list(clf_xgb.predict(dtest))    # XGBoost

y_nn = list(model_nn.predict(X_test))   # DNN

stack = pd.DataFrame({'lr':y_lr, 'gb':y_gb, 'xgb':y_xgb,'nn':y_nn, 'true':y_test})