{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Install Julia","metadata":{}},{"cell_type":"code","source":"%%bash\n\nJULIA_VERSION=\"1.7.1\"\nJULIA_PACKAGES=\"DataFrames CSV Pipe Plots MLJ DecisionTree\"\n\nJULIA_VER=`cut -d '.' -f -2 <<< \"$JULIA_VERSION\"`\nBASE_URL=\"https://julialang-s3.julialang.org/bin/linux/x64\"\nURL=\"$BASE_URL/$JULIA_VER/julia-$JULIA_VERSION-linux-x86_64.tar.gz\"\nwget -nv $URL -O /tmp/julia.tar.gz\ntar -x -f /tmp/julia.tar.gz -C /usr/local --strip-components 1\nrm /tmp/julia.tar.gz\nfor PKG in `echo $JULIA_PACKAGES`; do\n    echo \"Installing Julia package $PKG...\"\n    julia -e 'using Pkg; pkg\"add '$PKG'\"'\ndone","metadata":{"execution":{"iopub.status.busy":"2022-08-07T18:19:07.781702Z","iopub.execute_input":"2022-08-07T18:19:07.782164Z","iopub.status.idle":"2022-08-07T18:24:26.850444Z","shell.execute_reply.started":"2022-08-07T18:19:07.782131Z","shell.execute_reply":"2022-08-07T18:24:26.845644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# set the environment variable for number of threads\n\nimport os\nos.environ['JULIA_NUM_THREADS'] = \"4\"","metadata":{"execution":{"iopub.status.busy":"2022-08-07T18:24:26.855430Z","iopub.execute_input":"2022-08-07T18:24:26.856552Z","iopub.status.idle":"2022-08-07T18:24:26.867770Z","shell.execute_reply.started":"2022-08-07T18:24:26.856489Z","shell.execute_reply":"2022-08-07T18:24:26.865184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Test the instalation","metadata":{}},{"cell_type":"code","source":"%%bash\n\njulia -e 'println(\"Hello Julia.\")'\njulia -e 'using Base.Threads; println(nthreads())'","metadata":{"execution":{"iopub.status.busy":"2022-08-07T18:24:26.870180Z","iopub.execute_input":"2022-08-07T18:24:26.871173Z","iopub.status.idle":"2022-08-07T18:24:27.821253Z","shell.execute_reply.started":"2022-08-07T18:24:26.871128Z","shell.execute_reply":"2022-08-07T18:24:27.819676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Running Julia in Notebook cells","metadata":{}},{"cell_type":"markdown","source":"This creates a single Julia session to use the notebooks","metadata":{}},{"cell_type":"code","source":"%%time\n!pip install --quiet julia\nimport julia\nfrom julia.api import Julia\njulia.install()\njl = Julia(compiled_modules=False)  # cannot use precompiled packages with pyjulia on linux :-(\n%load_ext julia.magic","metadata":{"execution":{"iopub.status.busy":"2022-08-07T18:24:52.928997Z","iopub.execute_input":"2022-08-07T18:24:52.929559Z","iopub.status.idle":"2022-08-07T18:26:35.086020Z","shell.execute_reply.started":"2022-08-07T18:24:52.929522Z","shell.execute_reply":"2022-08-07T18:26:35.084310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Install the libraries to use:","metadata":{}},{"cell_type":"code","source":"%%bash\n\njulia -e 'using Pkg; pkg\"add MLJ\"'\njulia -e 'using Pkg; pkg\"add DecisionTree\"'\njulia -e 'using Pkg; pkg\"add MLJDecisionTreeInterface\"'\njulia -e 'using Pkg; pkg\"add XGBoost\"'\njulia -e 'using Pkg; pkg\"add MLJXGBoostInterface\"'\n\n","metadata":{"execution":{"iopub.status.busy":"2022-08-07T19:07:52.787393Z","iopub.execute_input":"2022-08-07T19:07:52.787905Z","iopub.status.idle":"2022-08-07T19:08:35.331372Z","shell.execute_reply.started":"2022-08-07T19:07:52.787867Z","shell.execute_reply":"2022-08-07T19:08:35.329069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load data and basic exploration","metadata":{}},{"cell_type":"code","source":"%%julia\n\nusing CSV, DataFrames, MLJ, DecisionTree\n\ntrain_data = CSV.read(\"../input/titanic/train.csv\", DataFrame);\n","metadata":{"execution":{"iopub.status.busy":"2022-08-07T18:37:32.453652Z","iopub.execute_input":"2022-08-07T18:37:32.454158Z","iopub.status.idle":"2022-08-07T18:37:32.971754Z","shell.execute_reply.started":"2022-08-07T18:37:32.454121Z","shell.execute_reply":"2022-08-07T18:37:32.970424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%julia\n\nfirst(train_data, 2)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T18:37:35.477551Z","iopub.execute_input":"2022-08-07T18:37:35.477990Z","iopub.status.idle":"2022-08-07T18:37:44.335383Z","shell.execute_reply.started":"2022-08-07T18:37:35.477955Z","shell.execute_reply":"2022-08-07T18:37:44.333626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%julia\n\nfunction value_counts(data, column_name)\n    combine(groupby(data, column_name), nrow)\nend","metadata":{"execution":{"iopub.status.busy":"2022-08-07T18:37:46.126535Z","iopub.execute_input":"2022-08-07T18:37:46.127719Z","iopub.status.idle":"2022-08-07T18:37:46.233260Z","shell.execute_reply.started":"2022-08-07T18:37:46.127678Z","shell.execute_reply":"2022-08-07T18:37:46.231924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%julia\n\nvalue_counts(train_data, \"Pclass\")","metadata":{"execution":{"iopub.status.busy":"2022-08-07T18:37:47.014987Z","iopub.execute_input":"2022-08-07T18:37:47.015833Z","iopub.status.idle":"2022-08-07T18:37:53.471849Z","shell.execute_reply.started":"2022-08-07T18:37:47.015794Z","shell.execute_reply":"2022-08-07T18:37:53.470426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%julia\n\nnames(train_data)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T18:37:53.475705Z","iopub.execute_input":"2022-08-07T18:37:53.476512Z","iopub.status.idle":"2022-08-07T18:37:53.601260Z","shell.execute_reply.started":"2022-08-07T18:37:53.476462Z","shell.execute_reply":"2022-08-07T18:37:53.599659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%julia \n\nfor col_name in names(train_data)\n    show(value_counts(train_data, col_name))\nend","metadata":{"execution":{"iopub.status.busy":"2022-08-07T18:37:54.012165Z","iopub.execute_input":"2022-08-07T18:37:54.012608Z","iopub.status.idle":"2022-08-07T18:37:58.945966Z","shell.execute_reply.started":"2022-08-07T18:37:54.012572Z","shell.execute_reply":"2022-08-07T18:37:58.944720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%julia \n\nschema(train_data)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T18:37:58.948329Z","iopub.execute_input":"2022-08-07T18:37:58.948680Z","iopub.status.idle":"2022-08-07T18:38:02.705728Z","shell.execute_reply.started":"2022-08-07T18:37:58.948648Z","shell.execute_reply":"2022-08-07T18:38:02.704776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%julia \nDataFrames.describe(train_data)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T19:25:58.438478Z","iopub.execute_input":"2022-08-07T19:25:58.438960Z","iopub.status.idle":"2022-08-07T19:26:01.842189Z","shell.execute_reply.started":"2022-08-07T19:25:58.438910Z","shell.execute_reply":"2022-08-07T19:26:01.840521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Coercion to avoid errors using Decision Tree","metadata":{}},{"cell_type":"code","source":"%%julia \ntrain_data_coerced = coerce(train_data, :SibSp=>Continuous, :Parch=>Continuous,\n                            :Sex=>Multiclass,\n                            :Pclass=>OrderedFactor, :Survived=>Multiclass);","metadata":{"execution":{"iopub.status.busy":"2022-08-07T18:38:06.225591Z","iopub.execute_input":"2022-08-07T18:38:06.226521Z","iopub.status.idle":"2022-08-07T18:38:12.313980Z","shell.execute_reply.started":"2022-08-07T18:38:06.226472Z","shell.execute_reply":"2022-08-07T18:38:12.312779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%julia \nschema(train_data_coerced)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T18:38:12.316030Z","iopub.execute_input":"2022-08-07T18:38:12.316391Z","iopub.status.idle":"2022-08-07T18:38:12.722337Z","shell.execute_reply.started":"2022-08-07T18:38:12.316358Z","shell.execute_reply":"2022-08-07T18:38:12.720838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%julia \ny, X =  MLJ.unpack(train_data_coerced, ==(:Survived), rng=123);","metadata":{"execution":{"iopub.status.busy":"2022-08-07T18:48:50.505914Z","iopub.execute_input":"2022-08-07T18:48:50.507163Z","iopub.status.idle":"2022-08-07T18:48:50.624912Z","shell.execute_reply.started":"2022-08-07T18:48:50.507118Z","shell.execute_reply":"2022-08-07T18:48:50.623836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%julia \nfirst(X)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T18:48:51.127875Z","iopub.execute_input":"2022-08-07T18:48:51.128335Z","iopub.status.idle":"2022-08-07T18:48:51.268021Z","shell.execute_reply.started":"2022-08-07T18:48:51.128289Z","shell.execute_reply":"2022-08-07T18:48:51.266619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%julia\n\nX = select(X, \"Age\", \"SibSp\", \"Parch\", \"Pclass\", \"Sex\");","metadata":{"execution":{"iopub.status.busy":"2022-08-07T18:48:52.817019Z","iopub.execute_input":"2022-08-07T18:48:52.818068Z","iopub.status.idle":"2022-08-07T18:48:53.071703Z","shell.execute_reply.started":"2022-08-07T18:48:52.818025Z","shell.execute_reply":"2022-08-07T18:48:53.070379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%julia\n\n(X_train, X_test), (y_train, y_test)  = partition((X, y), 0.6, multi=true,  rng=123);","metadata":{"execution":{"iopub.status.busy":"2022-08-07T18:53:26.705839Z","iopub.execute_input":"2022-08-07T18:53:26.706323Z","iopub.status.idle":"2022-08-07T18:53:26.883552Z","shell.execute_reply.started":"2022-08-07T18:53:26.706282Z","shell.execute_reply":"2022-08-07T18:53:26.882341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Pipeline","metadata":{}},{"cell_type":"code","source":"%%julia\npipe = (FillImputer() |> OneHotEncoder(drop_last = true))\n\nmach_transf = machine(pipe, X_train) |> MLJ.fit!","metadata":{"execution":{"iopub.status.busy":"2022-08-07T18:53:27.879861Z","iopub.execute_input":"2022-08-07T18:53:27.880395Z","iopub.status.idle":"2022-08-07T18:53:28.477523Z","shell.execute_reply.started":"2022-08-07T18:53:27.880357Z","shell.execute_reply":"2022-08-07T18:53:28.476686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%julia\nX_train = MLJ.transform(mach_transf, X_train);\nX_test = MLJ.transform(mach_transf, X_test);\nschema(X_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T18:53:28.659711Z","iopub.execute_input":"2022-08-07T18:53:28.660177Z","iopub.status.idle":"2022-08-07T18:53:28.824669Z","shell.execute_reply.started":"2022-08-07T18:53:28.660144Z","shell.execute_reply":"2022-08-07T18:53:28.823461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%julia\n\nfirst(X_train, 5)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T18:59:10.879958Z","iopub.execute_input":"2022-08-07T18:59:10.880506Z","iopub.status.idle":"2022-08-07T18:59:11.049731Z","shell.execute_reply.started":"2022-08-07T18:59:10.880465Z","shell.execute_reply":"2022-08-07T18:59:11.048777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"See the available models to solve the problem:","metadata":{}},{"cell_type":"code","source":"%%julia\nms = MLJ.models(matching(X_train, y_train))","metadata":{"execution":{"iopub.status.busy":"2022-08-07T18:53:30.198227Z","iopub.execute_input":"2022-08-07T18:53:30.198712Z","iopub.status.idle":"2022-08-07T18:53:30.322780Z","shell.execute_reply.started":"2022-08-07T18:53:30.198676Z","shell.execute_reply":"2022-08-07T18:53:30.321378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Random Forest","metadata":{}},{"cell_type":"code","source":"%%julia\n\nTree = @load RandomForestClassifier pkg=DecisionTree\n\nrf = Tree(max_depth=4)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T19:10:10.061243Z","iopub.execute_input":"2022-08-07T19:10:10.061775Z","iopub.status.idle":"2022-08-07T19:10:10.200245Z","shell.execute_reply.started":"2022-08-07T19:10:10.061735Z","shell.execute_reply":"2022-08-07T19:10:10.198975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%julia\n\nmach = machine(rf, X_train, y_train)\n\nMLJ.fit!(mach, verbosity=2)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T19:10:11.398338Z","iopub.execute_input":"2022-08-07T19:10:11.398812Z","iopub.status.idle":"2022-08-07T19:10:11.526834Z","shell.execute_reply.started":"2022-08-07T19:10:11.398777Z","shell.execute_reply":"2022-08-07T19:10:11.525602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%julia\n\nevaluate!(mach, resampling=CV(nfolds=5, shuffle=true, rng=1234),\n          measure=[LogLoss(), Accuracy()])","metadata":{"execution":{"iopub.status.busy":"2022-08-07T19:10:16.347582Z","iopub.execute_input":"2022-08-07T19:10:16.348285Z","iopub.status.idle":"2022-08-07T19:10:16.477825Z","shell.execute_reply.started":"2022-08-07T19:10:16.348244Z","shell.execute_reply":"2022-08-07T19:10:16.476423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%julia\n\npred_test = MLJ.predict_mode(mach, X_test);","metadata":{"execution":{"iopub.status.busy":"2022-08-07T19:10:17.747533Z","iopub.execute_input":"2022-08-07T19:10:17.748694Z","iopub.status.idle":"2022-08-07T19:10:17.873916Z","shell.execute_reply.started":"2022-08-07T19:10:17.748650Z","shell.execute_reply":"2022-08-07T19:10:17.872156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%julia\n\nfirst(pred_test, 5)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T19:10:18.684855Z","iopub.execute_input":"2022-08-07T19:10:18.685714Z","iopub.status.idle":"2022-08-07T19:10:18.819317Z","shell.execute_reply.started":"2022-08-07T19:10:18.685662Z","shell.execute_reply":"2022-08-07T19:10:18.817989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%julia\n\nfirst(y_test, 5)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T19:10:19.628826Z","iopub.execute_input":"2022-08-07T19:10:19.629372Z","iopub.status.idle":"2022-08-07T19:10:19.762494Z","shell.execute_reply.started":"2022-08-07T19:10:19.629333Z","shell.execute_reply":"2022-08-07T19:10:19.760913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Xgboost","metadata":{}},{"cell_type":"code","source":"%%julia\n\nbase_model = @load XGBoostClassifier pkg=XGBoost\n\nxgb_model = base_model(max_depth = 4, num_round = 200)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T19:16:02.857341Z","iopub.execute_input":"2022-08-07T19:16:02.857811Z","iopub.status.idle":"2022-08-07T19:16:02.994791Z","shell.execute_reply.started":"2022-08-07T19:16:02.857777Z","shell.execute_reply":"2022-08-07T19:16:02.993187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%julia\n\nmach_xgb = machine(xgb_model, X_train, y_train)\n\nMLJ.fit!(mach_xgb, verbosity=2)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T19:16:03.376978Z","iopub.execute_input":"2022-08-07T19:16:03.377495Z","iopub.status.idle":"2022-08-07T19:16:03.777003Z","shell.execute_reply.started":"2022-08-07T19:16:03.377457Z","shell.execute_reply":"2022-08-07T19:16:03.775432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%julia\n\nprintln(evaluate!(mach_xgb, resampling=CV(nfolds=5, shuffle=true, rng=1234),\n          measure=[LogLoss(), Accuracy(), AUC()]))","metadata":{"execution":{"iopub.status.busy":"2022-08-07T19:21:39.556351Z","iopub.execute_input":"2022-08-07T19:21:39.556818Z","iopub.status.idle":"2022-08-07T19:21:40.293745Z","shell.execute_reply.started":"2022-08-07T19:21:39.556782Z","shell.execute_reply":"2022-08-07T19:21:40.292131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### References","metadata":{}},{"cell_type":"markdown","source":"* Source to configure julia on kaggle: https://www.kaggle.com/code/marketneutral/julia-live-on-kaggle\n* MLJ: https://alan-turing-institute.github.io/MLJ.jl/dev/common_mlj_workflows/#Common-MLJ-Workflows","metadata":{}}]}