Skip to content

Commit

Permalink
Merge pull request #49 from andreasnoack/master
Browse files Browse the repository at this point in the history
Make base_walkthrough.jl demo work on Julia 0.6
  • Loading branch information
slundberg committed Nov 23, 2017
2 parents cfb6dfd + 00ba260 commit 0334442
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions demo/basic_walkthrough.jl
@@ -1,8 +1,10 @@
using XGBoost

const DATAPATH = joinpath(dirname(@__FILE__()), "..", "data")

# we load in the agaricus dataset
# In this example, we are aiming to predict whether a mushroom can be eated
function readlibsvm(fname::ASCIIString, shape)
function readlibsvm(fname::String, shape)
dmx = zeros(Float32, shape)
label = Float32[]
fi = open(fname, "r")
Expand All @@ -13,7 +15,7 @@ function readlibsvm(fname::ASCIIString, shape)
line = line[2:end]
for itm in line
itm = split(itm, ":")
dmx[cnt, int(itm[1]) + 1] = float(int(itm[2]))
dmx[cnt, parse(Int, itm[1]) + 1] = parse(Int, itm[2])
end
cnt += 1
end
Expand All @@ -22,8 +24,8 @@ function readlibsvm(fname::ASCIIString, shape)
end

# we use auxiliary function to read LIBSVM format into julia Matrix
train_X, train_Y = readlibsvm("../data/agaricus.txt.train", (6513, 126))
test_X, test_Y = readlibsvm("../data/agaricus.txt.test", (1611, 126))
train_X, train_Y = readlibsvm(joinpath(DATAPATH, "agaricus.txt.train"), (6513, 126))
test_X, test_Y = readlibsvm(joinpath(DATAPATH, "agaricus.txt.test"), (1611, 126))

#-------------Basic Training using XGBoost-----------------
# note: xgboost naturally handles sparse input
Expand Down Expand Up @@ -53,7 +55,7 @@ dtrain = DMatrix(train_X, label = train_Y)
bst = xgboost(dtrain, num_round, eta = 1, objective = "binary:logistic")

# you can also specify data as file path to a LibSVM format input
bst = xgboost("../data/agaricus.txt.train", num_round, max_depth = 2, eta = 1,
bst = xgboost(joinpath(DATAPATH, "agaricus.txt.train"), num_round, max_depth = 2, eta = 1,
objective = "binary:logistic")

#--------------------basic prediction using XGBoost--------------
Expand All @@ -68,7 +70,7 @@ save(bst, "xgb.model")
# load binary model to julia
bst2 = Booster(model_file = "xgb.model")
preds2 = predict(bst2, test_X)
print("sum(abs(pred2-pred))=", sum(abs(preds2 .- preds)), "\n")
print("sum(abs(pred2-pred))=", sum(abs, preds2 .- preds), "\n")

#----------------Advanced features --------------
# to use advanced features, we need to put data in xgb.DMatrix
Expand Down Expand Up @@ -100,4 +102,4 @@ print("test-error=", sum((pred .> 0.5) .!= label) / float(size(pred)[1]), "\n")
# Finally, you can dump the tree you learned using dump_model into a text file
dump_model(bst, "dump.raw.txt")
# If you have feature map file, you can dump the model in a more readable way
dump_model(bst, "dump.nice.txt", fmap = "../data/featmap.txt")
dump_model(bst, "dump.nice.txt", fmap = joinpath(DATAPATH, "featmap.txt"))

0 comments on commit 0334442

Please sign in to comment.