ImportError:无法导入名称XGBClassifier

时间:2017-05-25 05:27:47

标签: python-2.7

我一直在尝试使用以下代码预测时间序列:

# First XGBoost model for Pima Indians dataset
from numpy import loadtxt
from xgboost import XGBClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# load data
dataset = loadtxt(``'pima-indians-diabetes.csv', delimiter=",")
# split data into X and y
X = dataset[:,0:8]
Y = dataset[:,8]
# split data into train and test sets
seed = 7
test_size = 0.33
X_train, X_test, y_train, y_test = train_test_split(X, Y,test_size=test_size, random_state=seed)
# fit model no training data  
model = XGBClassifier()
model.fit(X_train, y_train)
# make predictions for test data
y_pred = model.predict(X_test)
predictions = [round(value) for value in y_pred]
# evaluate predictions
accuracy = accuracy_score(y_test, predictions)
print("Accuracy: %.2f%%" % (accuracy * 100.0))
  

我收到以下错误:

AttributeError: 'module' object has no attribute 'DMatrix'

如何更正此错误?

1 个答案:

答案 0 :(得分:0)

您的X和Y数据集似乎重叠:
两者都包括输入数据源的第8列。

X =数据集[:,0:8] Y =数据集[:,8]

X =数据集[:,0:7]应该是吗?