2017-10-21 1412 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 
相關問題