{"cells":[{"metadata":{},"cell_type":"markdown","source":"**This notebook only changes the number of estimators of the lgb model. Please upvote the original [notebook](https://www.kaggle.com/devinanzelmo/wifi-features-lightgbm-starter).**\n\n**Hats off to [Devin](https://www.kaggle.com/devinanzelmo) for his generous sharing!**"},{"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\nProper cross validation should be added. Currently don't have access to the path file names for the individual training examples which means it is not possible to do stratify by path file. \n"},{"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.csv')))\ntest_files = sorted(glob.glob(os.path.join(feature_dir, '*_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    np.random.shuffle(data.values)\n\n    cutoff = int(data.shape[0] * 0.95)\n\n    x_train = data.iloc[:cutoff,:-3]\n    y_trainy = data.iloc[:cutoff,-2]\n    y_trainx = data.iloc[:cutoff,-3]\n    y_trainf = data.iloc[:cutoff,-1]\n\n    x_test = data.iloc[cutoff:, :-3]\n    y_testy = data.iloc[cutoff:, -2]\n    y_testx = data.iloc[cutoff:, -3]\n    y_testf = data.iloc[cutoff:, -1]\n\n    # change 75 to 150\n    modely = lgb.LGBMRegressor(\n        n_estimators=150, num_leaves=127)\n    modely.fit(x_train, y_trainy)\n\n    modelx = lgb.LGBMRegressor(\n        n_estimators=150, num_leaves=127)\n    modelx.fit(x_train, y_trainx)\n\n    modelf = lgb.LGBMClassifier(\n        n_estimators=150, num_leaves=127)\n    modelf.fit(x_train, y_trainf)\n\n    predsx = modelx.predict(x_test)\n    predsy = modely.predict(x_test)\n    predsf = modelf.predict(x_test)\n\n    print(file)\n    print(data.shape)\n    print(comp_metric(predsx, predsy, predsf, y_testx.values, y_testy.values, y_testf.values))\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}