{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":53666,"databundleVersionId":6589269,"sourceType":"competition"},{"sourceId":149739889,"sourceType":"kernelVersion"}],"dockerImageVersionId":30558,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport polars as pl\nimport gc\nfrom tqdm.auto import tqdm \nimport joblib\n\n\nimport xgboost as xgb\nimport lightgbm as lgb\nimport pickle\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import confusion_matrix\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-19T17:32:26.813495Z","iopub.execute_input":"2023-11-19T17:32:26.813977Z","iopub.status.idle":"2023-11-19T17:32:26.848040Z","shell.execute_reply.started":"2023-11-19T17:32:26.813944Z","shell.execute_reply":"2023-11-19T17:32:26.846452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pl.read_parquet('/kaggle/input/make-data/train_series.parquet')\n","metadata":{"execution":{"iopub.status.busy":"2023-11-19T17:32:26.850467Z","iopub.execute_input":"2023-11-19T17:32:26.850828Z","iopub.status.idle":"2023-11-19T17:32:41.686644Z","shell.execute_reply.started":"2023-11-19T17:32:26.850798Z","shell.execute_reply":"2023-11-19T17:32:41.685094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data","metadata":{"execution":{"iopub.status.busy":"2023-11-19T17:32:41.688431Z","iopub.execute_input":"2023-11-19T17:32:41.688952Z","iopub.status.idle":"2023-11-19T17:32:41.752482Z","shell.execute_reply.started":"2023-11-19T17:32:41.688919Z","shell.execute_reply":"2023-11-19T17:32:41.751129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def split_train_test(df, n_train):\n    ids = df['series_id'].unique(maintain_order = True)\n    dict_ids2data = {}\n\n    # for i in range(2):\n    for i in tqdm(range(len(ids))):\n        print (i,': ', ids[i])\n        dict_ids2data[ids[i]] = df.filter(pl.col(\"series_id\") == ids[i])\n\n    train_ = pl.concat([i for i, i_id in zip(dict_ids2data.values(), range(len(ids))) if i_id <= n_train], rechunk=True)\n    test_ = pl.concat([i for i, i_id in zip(dict_ids2data.values(), range(len(ids))) if i_id > n_train], rechunk=True)\n\n    del dict_ids2data\n    gc.collect()\n    \n    return train_, test_\n\ndef split_Xy_train(df):\n    if ('step' in df.columns):\n        df = df.drop(\"step\")\n    if ('series_id' in df.columns):\n        df = df.drop(\"series_id\")\n    if ('timestamp' in df.columns):\n        df = df.drop(\"timestamp\")\n    return df.drop(\"awake\"), df['awake']\n\ntrain, test = split_train_test(data, 30)\n\nX_train, y_train = split_Xy_train(train)\n\ngc.collect()\n","metadata":{"execution":{"iopub.status.busy":"2023-11-19T17:32:41.754017Z","iopub.execute_input":"2023-11-19T17:32:41.754479Z","iopub.status.idle":"2023-11-19T17:32:53.527752Z","shell.execute_reply.started":"2023-11-19T17:32:41.754450Z","shell.execute_reply":"2023-11-19T17:32:53.526380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(X_train.columns)","metadata":{"execution":{"iopub.status.busy":"2023-11-19T17:32:53.532078Z","iopub.execute_input":"2023-11-19T17:32:53.532446Z","iopub.status.idle":"2023-11-19T17:32:53.540074Z","shell.execute_reply.started":"2023-11-19T17:32:53.532417Z","shell.execute_reply":"2023-11-19T17:32:53.538797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lgb_opt =  {\n    'num_leaves': 204,\n    'learning_rate': 0.07649523437092402,\n    'random_state': 42\n}\n\nmy_classifier = lgb.LGBMClassifier(**lgb_opt)\nmy_classifier.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2023-11-19T17:32:53.542092Z","iopub.execute_input":"2023-11-19T17:32:53.542602Z","iopub.status.idle":"2023-11-19T17:41:39.633937Z","shell.execute_reply.started":"2023-11-19T17:32:53.542567Z","shell.execute_reply":"2023-11-19T17:41:39.631825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# my_classifier.save_model('LGBMClassifier.txt')\n\njoblib.dump(my_classifier, 'LGBMClassifier.pkl')","metadata":{"execution":{"iopub.status.busy":"2023-11-19T17:41:39.636445Z","iopub.execute_input":"2023-11-19T17:41:39.636828Z","iopub.status.idle":"2023-11-19T17:41:39.716112Z","shell.execute_reply.started":"2023-11-19T17:41:39.636798Z","shell.execute_reply":"2023-11-19T17:41:39.714807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Create a DataFrame to store feature names and their importances\nfeature_importance_df = pd.DataFrame({'Feature': X_train.columns, 'Importance': my_classifier.feature_importances_})\n\n# Sort the DataFrame by importance in descending order\nfeature_importance_df = feature_importance_df.sort_values(by='Importance', ascending=False)\n\n# Create a bar plot to visualize feature importances\nplt.figure(figsize=(10, 20))\nplt.barh(feature_importance_df['Feature'], feature_importance_df['Importance'], color='skyblue')\nplt.xlabel('Feature Importance')\nplt.ylabel('Feature')\nplt.title('LightXGBoost Feature Importances')\nplt.gca().invert_yaxis()  # Invert the y-axis to display the most important features at the top\nplt.show()\ndisplay(feature_importance_df)","metadata":{"execution":{"iopub.status.busy":"2023-11-19T17:41:39.718034Z","iopub.execute_input":"2023-11-19T17:41:39.718364Z","iopub.status.idle":"2023-11-19T17:41:41.095944Z","shell.execute_reply.started":"2023-11-19T17:41:39.718336Z","shell.execute_reply":"2023-11-19T17:41:41.094730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test, y_test = split_Xy_train(test)\n\ny_predict_prob = my_classifier.predict_proba(X_test)\ny_predict = my_classifier.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-11-19T17:41:41.099651Z","iopub.execute_input":"2023-11-19T17:41:41.100788Z","iopub.status.idle":"2023-11-19T17:41:50.381384Z","shell.execute_reply.started":"2023-11-19T17:41:41.100752Z","shell.execute_reply":"2023-11-19T17:41:50.380165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = y_predict\nconf_matrix = confusion_matrix(y_true=y_test, y_pred=y_pred)\n\nfig, ax = plt.subplots(figsize=(4, 4))\nax.matshow(conf_matrix, cmap=plt.cm.Blues, alpha=0.3)\nfor i in range(conf_matrix.shape[0]):\n    for j in range(conf_matrix.shape[1]):\n        ax.text(x=j, y=i,s=conf_matrix[i, j], va='center', ha='center', size='xx-large')\n \nplt.xlabel('Predictions')\nplt.ylabel('Actuals')\n# plt.title('Confusion Matrix', fontsize=18)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-19T17:41:50.382874Z","iopub.execute_input":"2023-11-19T17:41:50.383224Z","iopub.status.idle":"2023-11-19T17:41:50.932168Z","shell.execute_reply.started":"2023-11-19T17:41:50.383195Z","shell.execute_reply":"2023-11-19T17:41:50.930608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print (f'accuracy {(conf_matrix[0][0] + conf_matrix[1][1]) / (conf_matrix[0][1] + conf_matrix[1][0] + conf_matrix[0][0] + conf_matrix[1][1])}')\nprint (f'recall {conf_matrix[1][1] / (conf_matrix[1][0] + conf_matrix[1][1])}')\nprint (f'precision {conf_matrix[1][1] / (conf_matrix[0][1] + conf_matrix[1][1])}')\n","metadata":{"execution":{"iopub.status.busy":"2023-11-19T17:47:02.150935Z","iopub.execute_input":"2023-11-19T17:47:02.152252Z","iopub.status.idle":"2023-11-19T17:47:02.160488Z","shell.execute_reply.started":"2023-11-19T17:47:02.152210Z","shell.execute_reply":"2023-11-19T17:47:02.159271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,8))\nplt.plot(y_predict_prob[:70000,1], color = 'r')\nplt.plot(y_test[:70000], color = 'b', linestyle = 'dashed')\nplt.show()\n\nplt.figure(figsize=(20,8))\nplt.plot(y_predict_prob[:170000,1], color = 'r')\nplt.plot(y_test[:170000], color = 'b', linestyle = 'dashed')\nplt.show()\n\nplt.figure(figsize=(20,8))\nplt.plot(y_predict_prob[:,1], color = 'r')\nplt.plot(y_test[:], color = 'b', linestyle = 'dashed')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-19T17:41:50.956612Z","iopub.execute_input":"2023-11-19T17:41:50.956965Z","iopub.status.idle":"2023-11-19T17:41:52.404320Z","shell.execute_reply.started":"2023-11-19T17:41:50.956935Z","shell.execute_reply":"2023-11-19T17:41:52.403389Z"},"trusted":true},"execution_count":null,"outputs":[]}]}