In [1]:
from numpy import loadtxt,array
from xgboost import XGBClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.utils import shuffle
import xgboost as xgb
import time

In [2]:
trainingdata = loadtxt('/media/aymen/Seagate Backup Plus Drive/trainnig data/CNN/uecfood100-cropped-1664features-cnn-finetuned-food101.svm')

In [3]:
trainingdata.shape


Out[3]:
(14358, 1665)

In [22]:
data = shuffle(trainingdata)

In [23]:
X = array(data[:, 1:], dtype="float")
Y = array(data[:, 0], dtype="int")

In [ ]:


In [24]:
# 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)

In [7]:
xg_train = xgb.DMatrix(X_train, label=y_train)
xg_test = xgb.DMatrix(X_test, label=y_test)

In [27]:
xgb_param = {'objective': 'multi:softmax', # Specify multiclass classification
         'tree_method': 'gpu_hist'}
model = XGBClassifier(silent=False,**xgb_param)

In [ ]:
model.fit(X_train, y_train)

In [16]:
# num_round = 5

# # Leave most parameters as default
# param = {'objective': 'multi:softmax', # Specify multiclass classification
#          'tree_method': 'gpu_hist' # Use GPU accelerated algorithm
#          }
# # use softmax multi-class classification
# # scale weight of positive examples
# param['eta'] = 0.1
# param['max_depth'] = 6
# param['silent'] = 1
# param['nthread'] = 4
# param['num_class'] = 100
# gpu_res = {} # Store accuracy result
# tmp = time.time()
# # Train model
# model = xgb.train(param, xg_train, num_round, evals=[(xg_test, 'test')], evals_result=gpu_res)
# print("GPU Training Time: %s seconds" % (str(time.time() - tmp)))


[0]	test-merror:0.613632
[1]	test-merror:0.535134
[2]	test-merror:0.485756
[3]	test-merror:0.458958
[4]	test-merror:0.436801
GPU Training Time: 81.7755138874 seconds

In [ ]:
# model.fit(X_train, y_train)

In [25]:
y_pred_tr = model.predict(X_train)
predictions_tr = [round(value) for value in y_pred_tr]
y_pred_ts = model.predict(X_test)
predictions_ts = [round(value) for value in y_pred_ts]

In [26]:
accuracy_tr = accuracy_score(y_train, predictions_tr)
print("Accuracy: %.2f%%" % (accuracy_tr * 100.0))
accuracy_ts = accuracy_score(y_test, predictions_ts)
print("Accuracy: %.2f%%" % (accuracy_ts * 100.0))


Accuracy: 90.69%
Accuracy: 91.14%

In [ ]: