Expecting 2 dimensional numpy.ndarray

Viewed 58

I'm trying to use XGBClassifier and having to convert old code that used Decision Classifier. I see that you need to send it into XGBoost using a DMatrix but I can't seem to convert my list into one. I have tried using

d_train = xgb.DMatrix(X_train, labels=y_train)

But that didn't work either. Any help is appreciated.

from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer

from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
from sklearn.metrics import accuracy_score


from sklearn.model_selection import cross_val_score
from sklearn.model_selection import cross_val_predict
from sklearn.model_selection import train_test_split


from sklearn.svm import LinearSVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.tree import export_text
from sklearn import tree

from numpy import loadtxt
from xgboost import XGBClassifier
from xgboost import DMatrix

from xgboost import plot_tree
import xgboost as xgb
import matplotlib.pyplot as plt

with open("/Users/XX/Working Directory/tickets.txt") as f:
    tickets = f.read().strip().split("\n")

with open("/Users/XX/Working Directory/labels_2.txt") as f:
    labels = f.read().strip().split("\n")
    

# split data into train and test sets
seed = 7
test_size = 0.33
X_train, X_test, y_train, y_test = train_test_split(labels, tickets, test_size=test_size, random_state=seed)
# fit model no training data

d_train = xgb.DMatrix(X_train, y_train)
d_test = xgb.DMatrix(X_test, y_test)

print(d_train)

params_1 = {
    'booster': 'gbtree',
    'max_depth': 5, 
    'learning_rate': 0.1,
    'sample_type': 'uniform',
    'normalize_type': 'tree',
    'objective': 'binary:hinge',
    'rate_drop': 0.1,
    'n_estimators': 500
}

xgb_clf = xgb.train(params_1, d_train,20)


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))

The error I'm getting is:

ValueError: ('Expecting 2 dimensional numpy.ndarray, got: ', (2655,))

But it's strange because when I look at the variables X_train and y_train both have a size of 2655.

0 Answers
Related