Skip to content

Commit 6776292

Browse files
Juang, Yi-Linterrytangyuan
Juang, Yi-Lin
authored andcommitted
Minor cleanup (dmlc#2342)
* Clean up demo of multiclass classification * Remove extra space
1 parent f1dc82e commit 6776292

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed
+19-16
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
1-
#! /usr/bin/python
1+
#!/usr/bin/python
2+
3+
from __future__ import division
4+
25
import numpy as np
36
import xgboost as xgb
47

58
# label need to be 0 to num_class -1
6-
data = np.loadtxt('./dermatology.data', delimiter=',',converters={33: lambda x:int(x == '?'), 34: lambda x:int(x)-1 } )
9+
data = np.loadtxt('./dermatology.data', delimiter=',',
10+
converters={33: lambda x:int(x == '?'), 34: lambda x:int(x)-1})
711
sz = data.shape
812

913
train = data[:int(sz[0] * 0.7), :]
1014
test = data[int(sz[0] * 0.7):, :]
1115

12-
train_X = train[:,0:33]
16+
train_X = train[:, :33]
1317
train_Y = train[:, 34]
1418

15-
16-
test_X = test[:,0:33]
19+
test_X = test[:, :33]
1720
test_Y = test[:, 34]
1821

19-
xg_train = xgb.DMatrix( train_X, label=train_Y)
22+
xg_train = xgb.DMatrix(train_X, label=train_Y)
2023
xg_test = xgb.DMatrix(test_X, label=test_Y)
2124
# setup parameters for xgboost
2225
param = {}
@@ -29,20 +32,20 @@
2932
param['nthread'] = 4
3033
param['num_class'] = 6
3134

32-
watchlist = [ (xg_train,'train'), (xg_test, 'test') ]
35+
watchlist = [(xg_train, 'train'), (xg_test, 'test')]
3336
num_round = 5
34-
bst = xgb.train(param, xg_train, num_round, watchlist );
37+
bst = xgb.train(param, xg_train, num_round, watchlist)
3538
# get prediction
36-
pred = bst.predict( xg_test );
37-
38-
print ('predicting, classification error=%f' % (sum( int(pred[i]) != test_Y[i] for i in range(len(test_Y))) / float(len(test_Y)) ))
39+
pred = bst.predict(xg_test)
40+
error_rate = np.sum(pred != test_Y) / test_Y.shape[0]
41+
print('Test error using softmax = {}'.format(error_rate))
3942

4043
# do the same thing again, but output probabilities
4144
param['objective'] = 'multi:softprob'
42-
bst = xgb.train(param, xg_train, num_round, watchlist );
45+
bst = xgb.train(param, xg_train, num_round, watchlist)
4346
# Note: this convention has been changed since xgboost-unity
4447
# get prediction, this is in 1D array, need reshape to (ndata, nclass)
45-
yprob = bst.predict( xg_test ).reshape( test_Y.shape[0], 6 )
46-
ylabel = np.argmax(yprob, axis=1)
47-
48-
print ('predicting, classification error=%f' % (sum( int(ylabel[i]) != test_Y[i] for i in range(len(test_Y))) / float(len(test_Y)) ))
48+
pred_prob = bst.predict(xg_test).reshape(test_Y.shape[0], 6)
49+
pred_label = np.argmax(pred_prob, axis=1)
50+
error_rate = np.sum(pred != test_Y) / test_Y.shape[0]
51+
print('Test error using softprob = {}'.format(error_rate))

python-package/xgboost/core.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ def boost(self, dtrain, grad, hess):
848848

849849
def eval_set(self, evals, iteration=0, feval=None):
850850
# pylint: disable=invalid-name
851-
"""Evaluate a set of data.
851+
"""Evaluate a set of data.
852852
853853
Parameters
854854
----------

0 commit comments

Comments
 (0)