{"cells":[{"metadata":{},"cell_type":"markdown","source":"### Simple benchmark using wifi features and lightgbm \n\nShows the use of wifi features I made to predict phone position. There is a lot of room for improvement, and for people interested in hyperparameter etc these features are an easy way to get started on this competition. Wifi features are available in [this dataset](https://www.kaggle.com/devinanzelmo/indoor-navigation-and-location-wifi-features). See this [forum post](https://www.kaggle.com/c/indoor-location-navigation/discussion/215445) for information on the approach. The code to generate the features is available in [this notebook](https://www.kaggle.com/devinanzelmo/wifi-features)\n\n\nUpdated to work with new training data. Cross validation example is commented out, but could be used for tuning. Includes train path as the group for groupkfold cv. "},{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport lightgbm as lgb\nimport glob\nimport os","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"feature_dir = \"../input/indoor-navigation-and-location-wifi-features/wifi_features\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# the metric used in this competition\ndef comp_metric(xhat, yhat, fhat, x, y, f):\n    intermediate = np.sqrt(np.power(xhat - x,2) + np.power(yhat-y,2)) + 15 * np.abs(fhat-f)\n    return intermediate.sum()/xhat.shape[0]\n\n# get our train and test files\ntrain_files = sorted(glob.glob(os.path.join(feature_dir, 'train/*_train.csv')))\ntest_files = sorted(glob.glob(os.path.join(feature_dir, 'test/*_test.csv')))\nssubm = pd.read_csv('../input/indoor-location-navigation/sample_submission.csv', index_col=0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions = list()\n\nfor e, file in enumerate(train_files):\n    data = pd.read_csv(file, index_col=0)\n    test_data = pd.read_csv(test_files[e], index_col=0)\n\n    # simple grid search for tuning using path and groupkfold cv\n    #for i in [127]:\n    #    for n in [75]:\n    #        modely = lgb.LGBMRegressor(\n    #        n_estimators=n, num_leaves=i, n_jobs=1)\n    #        gkf = model_selection.GroupKFold()\n    #        scores = model_selection.cross_val_score(\n    #            modely, x_train, y_trainy, scoring='neg_mean_squared_error',\n    #            cv=gkf, groups=data.iloc[:,-1], n_jobs=5)\n    #        print(i, n, l, scores, scores.mean())\n    \n\n    x_train = data.iloc[:,:-4]\n    y_trainy = data.iloc[:,-3]\n    y_trainx = data.iloc[:,-4]\n    y_trainf = data.iloc[:,-2]\n\n    modely = lgb.LGBMRegressor(\n        n_estimators=125, num_leaves=90)\n    modely.fit(x_train, y_trainy)\n\n    modelx = lgb.LGBMRegressor(\n        n_estimators=125, num_leaves=90)\n    modelx.fit(x_train, y_trainx)\n\n    modelf = lgb.LGBMClassifier(\n        n_estimators=125, num_leaves=90)\n    modelf.fit(x_train, y_trainf)\n    \n    test_predsx = modelx.predict(test_data.iloc[:,:-1])\n    test_predsy = modely.predict(test_data.iloc[:,:-1])\n    test_predsf = modelf.predict(test_data.iloc[:,:-1])\n    \n    test_preds = pd.DataFrame(np.stack((test_predsf, test_predsx, test_predsy))).T\n    test_preds.columns = ssubm.columns\n    test_preds.index = test_data[\"site_path_timestamp\"]\n    test_preds[\"floor\"] = test_preds[\"floor\"].astype(int)\n    predictions.append(test_preds)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# generate prediction file \nall_preds = pd.concat(predictions)\nall_preds = all_preds.reindex(ssubm.index)\nall_preds.to_csv('submission.csv')","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}