{"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":"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\nimport random\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-12T16:06:28.376418Z","iopub.execute_input":"2023-11-12T16:06:28.376928Z","iopub.status.idle":"2023-11-12T16:06:31.616650Z","shell.execute_reply.started":"2023-11-12T16:06:28.376859Z","shell.execute_reply":"2023-11-12T16:06:31.615358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pl.read_parquet('/kaggle/input/feature-engineering-prepare-data/train_series.parquet')\n\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-12T16:06:31.619128Z","iopub.execute_input":"2023-11-12T16:06:31.619559Z","iopub.status.idle":"2023-11-12T16:06:47.062678Z","shell.execute_reply.started":"2023-11-12T16:06:31.619519Z","shell.execute_reply":"2023-11-12T16:06:47.061433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ids = data['series_id'].unique(maintain_order = True)\nprint (list(ids))\n\n#     dict_id2nrows = {}\ns = 0\n\nfor i in tqdm(range(len(ids))):\n    tmp_len = len(data.filter(pl.col(\"series_id\") == ids[i]))\n    s += tmp_len\n#         dict_id2nrows[i] = tmp_len\n    print (i,':\\t', ids[i],'\\t',tmp_len,'\\t', s)\n    \ndel s\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-11-12T16:08:27.429712Z","iopub.execute_input":"2023-11-12T16:08:27.430976Z","iopub.status.idle":"2023-11-12T16:08:32.689467Z","shell.execute_reply.started":"2023-11-12T16:08:27.430929Z","shell.execute_reply":"2023-11-12T16:08:32.688330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def randomselect_ids_training(df, k=4, min_test_rate = 0.1, ids = None):\n    if not(ids):\n        ids = df['series_id'].unique(maintain_order = True)\n#         print (list(ids))\n    ids = list(ids)\n    \n    ids_choices = []\n    tmp_test_rate = 0\n    \n    while (tmp_test_rate < min_test_rate):\n        tmp_test_rate = 0\n        ids_choices = random.choices(list(ids), k = 4)\n        print(f'ids_choices {ids_choices}')\n        \n        tmp_sum = sum([len(df.filter(pl.col(\"series_id\") == i)) for i in ids_choices])\n        tmp_test_rate = tmp_sum / len(df)\n        \n        print (f'test_sum {tmp_sum}, test_rate {tmp_test_rate}')\n        \n    gc.collect()\n    return ids_choices\n\n\ndef split_train_test(df, k_test=4, min_test_rate = 0.1, ids = None, israndom = False):\n    if not(ids):\n        ids = df['series_id'].unique(maintain_order = True)\n        \n    ids = list(ids)\n            \n    ids_test = []\n    if (israndom):\n        ids_test = randomselect_ids_training(df, k=k_test, min_test_rate = min_test_rate, ids = ids)\n    else:\n        ids_test = ids[-k_test:]\n            \n    print (ids_test)\n    dict_ids2data = {}\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(), ids) if i_id not in ids_test], rechunk=True)\n    test_ = pl.concat([i for i, i_id in zip(dict_ids2data.values(), ids) if i_id in ids_test], rechunk=True)\n\n    del dict_ids2data\n    gc.collect()\n    \n    return train_, test_\n    \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        \n        \ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-11-12T16:06:53.148970Z","iopub.execute_input":"2023-11-12T16:06:53.149606Z","iopub.status.idle":"2023-11-12T16:06:53.281599Z","shell.execute_reply.started":"2023-11-12T16:06:53.149574Z","shell.execute_reply":"2023-11-12T16:06:53.280475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.columns","metadata":{"execution":{"iopub.status.busy":"2023-11-12T16:06:53.283154Z","iopub.execute_input":"2023-11-12T16:06:53.283488Z","iopub.status.idle":"2023-11-12T16:06:53.300491Z","shell.execute_reply.started":"2023-11-12T16:06:53.283460Z","shell.execute_reply":"2023-11-12T16:06:53.299341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"columns_drop = [\n 'hour',\n 'minute',\n'enmo_30m_mean',\n 'enmo_30m_max',\n 'enmo_30m_std',\n 'enmo_1v_30m_mean',\n 'enmo_1v_30m_max',\n 'enmo_1v_30m_std',\n 'anglez_30m_mean',\n 'anglez_30m_max',\n 'anglez_30m_std',\n 'anglez_1v_30m_mean',\n 'anglez_1v_30m_max',\n 'anglez_1v_30m_std',\n 'enmo_120m_mean',\n 'enmo_120m_max',\n 'enmo_120m_std',\n 'enmo_1v_120m_mean',\n 'enmo_1v_120m_max',\n 'enmo_1v_120m_std',\n 'anglez_120m_mean',\n 'anglez_120m_max',\n 'anglez_120m_std',\n 'anglez_1v_120m_mean',\n 'anglez_1v_120m_max',\n 'anglez_1v_120m_std',\n 'enmo_480m_mean',\n 'enmo_480m_max',\n 'enmo_480m_std',\n 'enmo_1v_480m_mean',\n 'enmo_1v_480m_max',\n 'enmo_1v_480m_std',\n 'anglez_480m_mean',\n 'anglez_480m_max',\n 'anglez_480m_std',\n 'anglez_1v_480m_mean',\n 'anglez_1v_480m_max',\n 'anglez_1v_480m_std'\n]","metadata":{"execution":{"iopub.status.busy":"2023-11-12T16:06:53.301742Z","iopub.execute_input":"2023-11-12T16:06:53.302097Z","iopub.status.idle":"2023-11-12T16:06:53.310685Z","shell.execute_reply.started":"2023-11-12T16:06:53.302068Z","shell.execute_reply":"2023-11-12T16:06:53.309418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"k_id_test = 4\nnum_loop = 10\n\n# data = data.drop(columns_drop)\ngc.collect()\n\nfor i in range(0, num_loop):\n    curr_train, curr_test = split_train_test(data, israndom =True, k_test = k_id_test)\n    curr_X_train, curr_y_train = split_Xy_train(curr_train)\n    \n    del curr_train\n    gc.collect()\n    \n#     break\n    lgb_opt =  {\n        'num_leaves': 204,\n        'learning_rate': 0.07649523437092402,\n        'random_state': 42\n    }\n\n    my_classifier = lgb.LGBMClassifier(**lgb_opt)\n    my_classifier.fit(curr_X_train, curr_y_train)\n    \n    ##\n    X_test, y_test = split_Xy_train(curr_test)\n    del curr_test\n    del curr_X_train\n    del curr_y_train\n    \n    gc.collect()\n\n    y_predict_prob = my_classifier.predict_proba(X_test)\n    y_predict = my_classifier.predict(X_test)\n    \n    ##\n    y_pred = y_predict\n    conf_matrix = confusion_matrix(y_true=y_test, y_pred=y_pred)\n    print(conf_matrix )\n    \n    fig, ax = plt.subplots(figsize=(4, 4))\n    ax.matshow(conf_matrix, cmap=plt.cm.Blues, alpha=0.3)\n    for 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\n    plt.xlabel('Predictions')\n    plt.ylabel('Actuals')\n    # plt.title('Confusion Matrix', fontsize=18)\n    plt.show()\n    \n    plt.figure(figsize=(20,6))\n    plt.plot(y_predict_prob[:70000,1], color = 'r')\n    plt.plot(y_test[:70000], color = 'b', linestyle = 'dashed')\n    plt.show()\n\n    plt.figure(figsize=(20,6))\n    plt.plot(y_predict_prob[:170000,1], color = 'r')\n    plt.plot(y_test[:170000], color = 'b', linestyle = 'dashed')\n    plt.show()\n\n    plt.figure(figsize=(20,6))\n    plt.plot(y_predict_prob[:,1], color = 'r')\n    plt.plot(y_test[:], color = 'b', linestyle = 'dashed')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-12T16:11:30.913091Z","iopub.execute_input":"2023-11-12T16:11:30.914612Z","iopub.status.idle":"2023-11-12T17:14:12.835894Z","shell.execute_reply.started":"2023-11-12T16:11:30.914537Z","shell.execute_reply":"2023-11-12T17:14:12.834670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"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_test.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-12T17:22:04.705015Z","iopub.execute_input":"2023-11-12T17:22:04.705532Z","iopub.status.idle":"2023-11-12T17:22:05.680344Z","shell.execute_reply.started":"2023-11-12T17:22:04.705495Z","shell.execute_reply":"2023-11-12T17:22:05.679186Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\njoblib.dump(['7822ee8fe3ec', '29c75c018220', '9ee455e4770d', '08db4255286f'], 'ids_test.pkl')\njoblib.dump(columns_drop, 'columns_drop.pkl')\n\njoblib.dump(my_classifier, 'LGBMClassifier_lastfold.pkl')\n\njoblib.dump(y_test, 'y_test.pkl')\njoblib.dump(X_test, 'X_test.pkl')\n\njoblib.dump(conf_matrix, 'conf_matrix.pkl')\njoblib.dump(y_predict_prob, 'y_predict_prob.pkl')\njoblib.dump(y_predict, 'y_predict.pkl')\n\n","metadata":{"execution":{"iopub.status.busy":"2023-11-12T17:38:02.072961Z","iopub.execute_input":"2023-11-12T17:38:02.073464Z","iopub.status.idle":"2023-11-12T17:38:03.643160Z","shell.execute_reply.started":"2023-11-12T17:38:02.073429Z","shell.execute_reply":"2023-11-12T17:38:03.642144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test","metadata":{"execution":{"iopub.status.busy":"2023-11-12T17:18:14.105313Z","iopub.execute_input":"2023-11-12T17:18:14.105765Z","iopub.status.idle":"2023-11-12T17:18:14.119026Z","shell.execute_reply.started":"2023-11-12T17:18:14.105730Z","shell.execute_reply":"2023-11-12T17:18:14.117826Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]}]}