ValueError: X needs to contain only non-negative integers.

for feature in short_cate_feature:
  enc.fit(data[feature].values.reshape(-1, 1))
  base_train_csr = sparse.hstack((base_train_csr, enc.transform(train_x[feature].values.reshape(-1, 1))), 'csr','bool')
  base_predict_csr = sparse.hstack((base_predict_csr, enc.transform(predict[feature].values.reshape(-1, 1))),'csr','bool')

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

报错:ValueError: X needs to contain only non-negative integers.

x不能包含负整数,因为我前面fillna(-1),

可以对dataframe,直接使用

for i in short_cate_feature:
  all_table[i] = all_table[i].astype(str)  但是因为是独热码,需要非负整数。

因此用了个骚操作:

for i in short_cate_feature:
  data[i] = data[i].map(dict(zip(data[i].unique(), range(0, data[i].nunique()))))

通过词典映射为数字

猜你喜欢

转载自www.cnblogs.com/smartwhite/p/9767867.html