2017-10-21 1413 views
0

试图实现XGBoost来确定最重要的变量,我有一些数组的错误。类型str没有定义__round__方法错误

我的完整代码如下

from numpy import loadtxt 
from numpy import sort 
import pandas as pd 
from xgboost import XGBClassifier 
from sklearn.model_selection import train_test_split 
from sklearn.metrics import accuracy_score 
from sklearn.feature_selection import SelectFromModel 


df = pd.read_csv('data.txt') 
array=df.values 
X= array[:,0:330] 
Y = array[:,330] 

X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.33, random_state=7) 


model = XGBClassifier() 
model.fit(X_train, y_train) 


y_pred = model.predict(X_test) 
predictions = [round(value) for value in y_pred] 

,我得到以下错误:

TypeError: type str doesn't define __round__ method 

我能做些什么?

+0

如果您可以发布整个错误消息 – 0TTT0

回答

2

您在y_train中拥有的某些标签实际上可能是字符串而不是数字。 sklearnxgboost不要求标签是数字。

尝试检查y_pred的类型。

from collections import Counter 

Counter([type(value) for value in y_pred]) 

这里是我的意思与数字标签

import numpy as np 
from sklearn.ensemble import GradientBoostingClassifier 

# test with numeric labels 
x = np.vstack([np.arange(100), np.sort(np.random.normal(10, size=100))]).T 
y = np.hstack([np.zeros(50, dtype=int), np.ones(50, dtype=int)]) 
model = GradientBoostingClassifier() 
model.fit(x,y) 
model.predict([[10,7]]) 
# returns an array with a numeric 
array([0]) 

,并在这里与串标记(同一x数据)

y = ['a']*50 + ['b']*50 
model.fit(x,y) 
model.predict([[10,7]]) 
# returns an array with a string label 
array(['a'], dtype='<U1') 

两者都是值标签的例子。但是,当您尝试对字符串变量使用round时,您会看到完全错误。

round('a') 

TypeError: type str doesn't define __round__ method 
相关问题