{"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)\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\n\n\nimport lightgbm as lgb\n\n\nimport gc\nimport plotly.express as px\nfrom joblib import Parallel, delayed\nfrom functools import reduce\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"_uuid":"686b6d59-3edb-42a2-a60e-3dc7c6d9f5ab","_cell_guid":"11a27c0e-ce3b-4ea8-80e9-aef25607846c","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-11-13T15:22:30.575632Z","iopub.execute_input":"2023-11-13T15:22:30.576706Z","iopub.status.idle":"2023-11-13T15:22:32.932818Z","shell.execute_reply.started":"2023-11-13T15:22:30.576649Z","shell.execute_reply":"2023-11-13T15:22:32.931509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 기존의 학습에서 기여도가 낮았던 feature부터 순서대로 나열 (피쳐 중 anglez, enmo는 제외)\n\nfeat_imp_from_low =['anglezdiffabs_amplit_720', 'anglezdiffabs_amplit_3600', 'anglezdiffabs_min_3600', 'anglezdiffabs_min_360',\n 'anglezdiffabs_min_720', 'anglezdiffabs_min_1800', 'anglezdiffabs_amplit_1800', 'anglezdiffabs_min_2880',\n 'anglezdiffabs_amplit_2880', 'anglezdiffabs_min_60', 'anglezdiffabs_amplit_60', 'anglezdiffabs_amplit_360',\n 'anglezdiffabs_max_60', 'anglez_diff_60_mean', 'anglezdiffabs', 'anglez_diff_60_max',\n 'enmo_diff_60_max', 'anglez_amplit_60', 'anglezdiffabs_median_60', 'enmo_amplit_720',\n 'anglezdiffabs_diff_60_mean', 'enmo_diff_360_mean', 'anglezdiffabs_diff_60_max', 'anglezdiffabs_max_360',\n 'anglezdiffabs_amplit_60_min', 'anglezdiffabs_diff_360_max', 'anglezdiffabs_diff_360_mean',\n 'anglezdiffabs_var_360', 'anglez_diff_360_mean', 'anglezdiffabs_var_60', 'enmo_amplit_60',\n 'enmo_mean_60', 'anglez_var_720', 'anglezdiffabs_max_720', 'anglez_diff_720_mean',\n 'anglez_diff_360_max', 'anglezdiffabs_var_720', 'enmo_var_60', 'anglez_amplit_360',\n 'anglez_mean_720', 'anglez_amplit_720', 'enmo_max_60', 'anglez_var_360',\n 'enmo_median_60', 'anglez_amplit_60_min', 'anglez_mean_360', 'enmo_max_720',\n 'anglez_mean_60', 'enmo_min_60', 'enmo_amplit_360', 'enmo_diff_720_mean',\n 'enmo_min_720', 'enmo_max_360', 'anglezdiffabs_mean_60', 'enmo_amplit_1800',\n 'enmo_diff_360_max', 'anglezdiffabs_amplit_360_min', 'anglez_max_360', 'anglez_var_60',\n 'anglez_amplit_1800', 'enmo_median_360', 'enmo_min_360', 'anglez_diff_720_max',\n 'anglezdiffabs_mean_360', 'anglez_max_60', 'enmo_diff_60_mean', 'anglez_median_60',\n 'anglez_min_360', 'anglez_max_720', 'anglezdiffabs_diff_720_mean', 'enmo_amplit_60_min',\n 'enmo_var_720', 'anglez_median_720', 'anglez_median_360', 'enmo_diff_720_max',\n 'anglezdiffabs_amplit_720_min', 'anglezdiffabs_max_1800', 'anglezdiffabs_diff_720_max', 'anglez_var_1800',\n 'enmo_median_720', 'anglez_mean_1800', 'enmo_mean_360', 'enmo_min_1800',\n 'enmo_var_1800', 'enmo_median_1800', 'enmo_max_1800', 'enmo_amplit_2880',\n 'enmo_mean_1800', 'anglezdiffabs_diff_1800_mean', 'enmo_min_2880', 'anglez_max_1800',\n 'anglezdiffabs_var_1800', 'anglez_mean_2880', 'anglez_var_2880', 'enmo_diff_1800_mean',\n 'anglezdiffabs_max_2880', 'anglez_amplit_2880', 'enmo_amplit_3600', 'enmo_mean_2880',\n 'enmo_median_2880', 'anglez_amplit_720_min', 'enmo_var_360',\n 'enmo_mean_720', 'anglez_diff_1800_mean', 'anglez_min_60', 'enmo_max_2880',\n 'anglez_min_1800', 'anglez_diff_1800_max', 'enmo_min_3600', 'anglezdiffabs_var_2880',\n 'enmo_var_2880', 'lids', 'enmo_diff_2880_mean', 'anglez_max_2880',\n 'anglez_amplit_3600', 'enmo_median_3600', 'anglez_min_2880', 'enmo_max_3600',\n 'enmo_diff_1800_max', 'anglez_mean_3600', 'anglez_amplit_1800_min', 'anglezdiffabs_diff_2880_mean',\n 'anglezdiffabs_mean_720', 'anglezdiffabs_amplit_1800_min', 'anglez_min_720', 'anglezdiffabs_median_2880',\n 'anglezdiffabs_diff_3600_mean', 'anglez_var_3600', 'anglez_median_1800', 'anglezdiffabs_median_360',\n 'anglezdiffabs_diff_1800_max', 'anglez_diff_2880_mean', 'anglez_mad_3600', 'anglez_max_3600',\n 'anglezdiffabs_mean_1800', 'enmo_amplit_720_min', 'anglez_median_3600', 'enmo_diff_2880_max',\n 'enmo_mean_3600', 'anglez_diff_3600_mean', 'anglez_diff_2880_max', 'anglez_amplit_2880_min',\n 'anglezdiffabs_amplit_2880_min', 'enmo_mad_3600', 'anglezdiffabs_diff_2880_max', 'enmo_var_3600',\n 'enmo_amplit_1800_min', 'anglezdiffabs_diff_3600_max', 'enmo_amplit_360_min', 'anglez_diff_3600_max',\n 'anglez_median_2880', 'anglez_min_3600', 'enmo_diff_3600_mean', 'anglezdiffabs_max_3600',\n 'enmo_diff_3600_max', 'anglezdiffabs_var_3600', 'enmo_amplit_2880_min', 'anglezdiffabs_mean_2880',\n 'anglez_amplit_360_min', 'anglez_amplit_3600_min', 'anglezdiffabs_amplit_3600_min', 'enmo_amplit_3600_min',\n 'anglezdiffabs_median_720', 'anglezdiffabs_median_3600', 'anglezdiffabs_median_1800', 'hour',\n 'anglezdiffabs_mean_3600', 'anglezdiffabs_mad_3600'] #168 features\n\n\n# N개의 기여도가 낮은 피쳐를 제거하는 함수\n\ndef drop_feat_poor(df, N):\n\n    N = min(N, len(feat_imp_from_low))\n\n    cols_to_remove = feat_imp_from_low[:N]\n\n    df = df.drop(columns=cols_to_remove)\n\n    return df\n\n\nw_size = 2000 # 가장 큰 윈도우 사이즈 기준 (현재 2000)\ndrop_poor_no = 148 # 총 168개의 기존 피쳐 중 제거할 피쳐의 수 (기여도 낮은 순으로 제거됨)","metadata":{"execution":{"iopub.status.busy":"2023-11-13T15:22:32.939008Z","iopub.execute_input":"2023-11-13T15:22:32.939355Z","iopub.status.idle":"2023-11-13T15:22:32.957537Z","shell.execute_reply.started":"2023-11-13T15:22:32.939323Z","shell.execute_reply":"2023-11-13T15:22:32.955585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_parquet('/kaggle/input/merged-data/merged_data.parquet')\n\ndata = data.iloc[::60] # 데이터 분량 조절\n\ndata = data.rename(columns={'series_id': 'series_id_no'}) # column 이름 변경","metadata":{"_uuid":"31f7dc84-6de7-4d23-80c4-d3f02770a0dc","_cell_guid":"19b95b4f-7738-4ada-b742-b961222cbac1","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-11-13T15:22:32.959235Z","iopub.execute_input":"2023-11-13T15:22:32.960018Z","iopub.status.idle":"2023-11-13T15:22:37.142980Z","shell.execute_reply.started":"2023-11-13T15:22:32.959974Z","shell.execute_reply":"2023-11-13T15:22:37.141137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DEV = True # True(빠른 개발용) - 모든 series_id 사용, False(제출용) - 10개의 series_id만 사용\n\nif DEV:\n    unique_series_ids = data['series_id_no'].unique()\n    selected_series_ids = unique_series_ids[:10]\n    data = data[data['series_id_no'].isin(selected_series_ids)]","metadata":{"execution":{"iopub.status.busy":"2023-11-13T15:22:37.146560Z","iopub.execute_input":"2023-11-13T15:22:37.147745Z","iopub.status.idle":"2023-11-13T15:22:37.187980Z","shell.execute_reply.started":"2023-11-13T15:22:37.147703Z","shell.execute_reply":"2023-11-13T15:22:37.186707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 기존 노트북과 동일\n\ndef feat_eng(df):\n    \n    df['series_id_no'] = df['series_id_no'].astype('category')\n    df['timestamp'] = pd.to_datetime(df['timestamp']).apply(lambda t: t.tz_localize(None))\n    df['hour'] = df[\"timestamp\"].dt.hour\n    \n    df.sort_values(['timestamp'], inplace=True)\n    df.set_index('timestamp', inplace=True)\n    \n    df['lids'] = np.maximum(0., df['enmo'] - 0.02)\n    df['lids'] = df['lids'].rolling(f'{120*5}s', center=True, min_periods=1).agg('sum')\n    df['lids'] = 100 / (df['lids'] + 1)\n    df['lids'] = df['lids'].rolling(f'{360*5}s', center=True, min_periods=1).agg('mean').astype(np.float32)\n    \n    df[\"enmo\"] = (df[\"enmo\"]*1000).astype(np.int16)\n    df[\"anglez\"] = df[\"anglez\"].astype(np.int16)\n    df[\"anglezdiffabs\"] = df[\"anglez\"].diff().abs().astype(np.float32)\n    \n    for col in ['enmo', 'anglez', 'anglezdiffabs']:\n        \n        # periods in seconds        \n        periods = [60,360,720,1800,2880,3600] \n        \n        for n in periods:\n            \n            rol_args = {'window':f'{n+5}s', 'min_periods':10, 'center':True}\n            \n            for agg in ['median', 'mean', 'max', 'min', 'var']:\n                df[f'{col}_{agg}_{n}'] = df[col].rolling(**rol_args).agg(agg).astype(np.float32).values\n                gc.collect()\n            \n            if n == max(periods):\n                df[f'{col}_mad_{n}'] = (df[col] - df[f'{col}_median_{n}']).abs().rolling(**rol_args).median().astype(np.float32)\n            \n            df[f'{col}_amplit_{n}'] = df[f'{col}_max_{n}']-df[f'{col}_min_{n}']\n            df[f'{col}_amplit_{n}_min'] = df[f'{col}_amplit_{n}'].rolling(**rol_args).min().astype(np.float32).values\n            \n            # if col in ['enmo', 'anglez']:\n            df[f'{col}_diff_{n}_max'] = df[f'{col}_max_{n}'].diff().abs().rolling(**rol_args).max().astype(np.float32)\n            df[f'{col}_diff_{n}_mean'] = df[f'{col}_max_{n}'].diff().abs().rolling(**rol_args).mean().astype(np.float32)\n\n    \n            gc.collect()\n    \n    df.reset_index(inplace=True)\n    df.dropna(inplace=True)\n\n    return df","metadata":{"execution":{"iopub.status.busy":"2023-11-13T15:22:37.189951Z","iopub.execute_input":"2023-11-13T15:22:37.190365Z","iopub.status.idle":"2023-11-13T15:22:37.210196Z","shell.execute_reply.started":"2023-11-13T15:22:37.190329Z","shell.execute_reply":"2023-11-13T15:22:37.208887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def feat_eng_by_id_no(df, idx):\n    from warnings import simplefilter \n    simplefilter(action=\"ignore\", category=pd.errors.PerformanceWarning)\n    \n    # Filter the dataframe based on 'idx'\n    filtered_df = df[df['series_id_no'] == idx].copy()\n    filtered_df['event'] = filtered_df['event'].astype(np.int8)\n    filtered_df = feat_eng(filtered_df)\n    \n    return filtered_df","metadata":{"execution":{"iopub.status.busy":"2023-11-13T15:22:37.211970Z","iopub.execute_input":"2023-11-13T15:22:37.212678Z","iopub.status.idle":"2023-11-13T15:22:37.229461Z","shell.execute_reply.started":"2023-11-13T15:22:37.212627Z","shell.execute_reply":"2023-11-13T15:22:37.227998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# weird_series = ['31011ade7c0a', 'a596ad0b82aa']\n\n# series_id = [s for s in series_id if s not in weird_series]","metadata":{"execution":{"iopub.status.busy":"2023-11-13T15:22:37.232160Z","iopub.execute_input":"2023-11-13T15:22:37.233485Z","iopub.status.idle":"2023-11-13T15:22:37.239927Z","shell.execute_reply.started":"2023-11-13T15:22:37.233404Z","shell.execute_reply":"2023-11-13T15:22:37.238680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\n\nseries_id_no = data['series_id_no'].unique()\n\n\ndata = Parallel(n_jobs=6)(delayed(feat_eng_by_id_no)(data, i) for i in series_id_no)\ndata = pd.concat(data, ignore_index=True)","metadata":{"execution":{"iopub.status.busy":"2023-11-13T15:22:37.241476Z","iopub.execute_input":"2023-11-13T15:22:37.242520Z","iopub.status.idle":"2023-11-13T15:22:50.907387Z","shell.execute_reply.started":"2023-11-13T15:22:37.242483Z","shell.execute_reply":"2023-11-13T15:22:50.905832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 피쳐 엔지니어링을 진행한 데이터프레임에서 기여도가 낮은 피쳐를 drop_poor_no의 수만큼 drop\n\ndata = drop_feat_poor(data, drop_poor_no)","metadata":{"execution":{"iopub.status.busy":"2023-11-13T15:22:50.909588Z","iopub.execute_input":"2023-11-13T15:22:50.910006Z","iopub.status.idle":"2023-11-13T15:22:50.921385Z","shell.execute_reply.started":"2023-11-13T15:22:50.909966Z","shell.execute_reply":"2023-11-13T15:22:50.920063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 각 윈도우에서 fft피쳐와 평균, 표준편차를 계산하는 함수\n\ndef extract_fft_features(conc, window_size):\n    features = []\n    series_id_count = {}  # Dictionary to keep track of counts for each series_id_no\n\n    for window in conc:\n        series_id_no = window[0][0]\n\n        # Increment count for the current series_id_no or initialize if not present\n        if series_id_no in series_id_count:\n            series_id_count[series_id_no] += 1\n        else:\n            series_id_count[series_id_no] = 0\n\n        # Create the modified series_id_no\n        modified_series_id_no = f\"{series_id_no}_{series_id_count[series_id_no]}\"\n\n        # FFT for 'enmo' and 'anglez'\n        fft_enmo = np.fft.fft(window[2])\n        fft_anglez = np.fft.fft(window[1])\n\n        half_len = len(fft_enmo) // 2\n        fft_enmo_half = fft_enmo[:half_len]\n        fft_anglez_half = fft_anglez[:half_len]\n\n        # Amplitude and angle calculations\n        amp_enmo = np.abs(fft_enmo_half)\n        angle_enmo = np.angle(fft_enmo_half)\n        amp_anglez = np.abs(fft_anglez_half)\n        angle_anglez = np.angle(fft_anglez_half)\n\n        # Top 3 amplitude and angle values\n        indices_enmo = np.argsort(amp_enmo)[::-1][:3]\n        indices_anglez = np.argsort(amp_anglez)[::-1][:3]\n\n        top_amp_enmo = amp_enmo[indices_enmo]\n        top_angle_enmo = angle_enmo[indices_enmo]\n        top_amp_anglez = amp_anglez[indices_anglez]\n        top_angle_anglez = angle_anglez[indices_anglez]\n\n        # Calculate average and standard deviation\n        enmo_avg = np.mean(window[2])\n        enmo_std = np.std(window[2])\n        anglez_avg = np.mean(window[1])\n        anglez_std = np.std(window[1])\n\n        # Append all features\n        features.append(np.concatenate(([modified_series_id_no], top_amp_enmo, top_angle_enmo, top_amp_anglez, top_angle_anglez,\n                                        [enmo_avg, enmo_std, anglez_avg, anglez_std])))\n\n    # Dynamic column names based on window size\n    columns = ['series_id_no'] + [f'{feature}_{window_size}' for feature in [\n        'enmo_amp_top1', 'enmo_amp_top2', 'enmo_amp_top3',\n        'enmo_angle_top1', 'enmo_angle_top2', 'enmo_angle_top3',\n        'anglez_amp_top1', 'anglez_amp_top2', 'anglez_amp_top3',\n        'anglez_angle_top1', 'anglez_angle_top2', 'anglez_angle_top3'\n    ]] + [f'enmo_avg_{window_size}', f'enmo_std_{window_size}', f'anglez_avg_{window_size}', f'anglez_std_{window_size}']\n\n    return pd.DataFrame(features, columns=columns)","metadata":{"_uuid":"e5358b6d-a6b6-4a27-ba6e-fd563f6df9d3","_cell_guid":"9be5c1c0-4609-492d-ba49-7e5abe8aac5c","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-11-13T15:22:50.923635Z","iopub.execute_input":"2023-11-13T15:22:50.924136Z","iopub.status.idle":"2023-11-13T15:22:50.940211Z","shell.execute_reply.started":"2023-11-13T15:22:50.924094Z","shell.execute_reply":"2023-11-13T15:22:50.938802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\n# 윈도잉을 위한 데이터프레임들 생성\ndfs = [data.query(f'series_id_no=={i}') for i in range(len(data.series_id_no.unique()))]\n\n\n# 네가지 윈도우 사이즈에 대하여 fft 계산 후 네가지 데이터프레임을 dictionary 안에 넣음\nw_sizes = [2000, 1000, 500, 250]\nX_fft_dict = {}\n\n# series_id_no, anglez, enmo column[1, 4, 5] 선택 후 anglez와 enmo에 대해 fft 계산 진행\nfor w_size in w_sizes:\n    windows = [np.lib.stride_tricks.sliding_window_view(df.iloc[:, [1, 4, 5]], w_size, axis=0) for df in dfs if len(df) > w_size]\n    conc = np.concatenate(windows)\n    X_fft_one = extract_fft_features(conc, w_size)\n    X_fft_dict[w_size] = X_fft_one","metadata":{"execution":{"iopub.status.busy":"2023-11-13T15:22:50.942007Z","iopub.execute_input":"2023-11-13T15:22:50.942863Z","iopub.status.idle":"2023-11-13T15:23:37.932067Z","shell.execute_reply.started":"2023-11-13T15:22:50.942782Z","shell.execute_reply":"2023-11-13T15:23:37.930757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# dictionary 안에 있는 네가지(2000, 1000, 500, 250) 데이터프레임을 하나로 병합하고, 윈도우 사이즈의 차이로 인해 row 수가 차이나는 부분들은 삭제\ndataframes = list(X_fft_dict.values())\n\nX_fft = reduce(lambda left, right: pd.merge(left, right, on='series_id_no', how='outer'), dataframes)\n\nX_fft.dropna(inplace=True)\n\n\n\n#값들의 type을 numeric으로 변경\ncols_to_convert = X_fft.columns.drop('series_id_no')\n\nX_fft[cols_to_convert] = X_fft[cols_to_convert].apply(pd.to_numeric, errors='coerce') ","metadata":{"execution":{"iopub.status.busy":"2023-11-13T15:23:37.933573Z","iopub.execute_input":"2023-11-13T15:23:37.934069Z","iopub.status.idle":"2023-11-13T15:23:40.356072Z","shell.execute_reply.started":"2023-11-13T15:23:37.934024Z","shell.execute_reply":"2023-11-13T15:23:40.355033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 추후 fft 데이터프레임과의 매칭을 위해 기존 피쳐 데이터프레임에서 series_id_no를 0_0, 0_1, 0_3, ... , 9_0, 9_1, 9_2 와 같은 형식으로 변경\n\nX_feat = data.copy()\nseries_id_count = {}\n\nfor index, row in X_feat.iterrows():\n    series_id_no = row['series_id_no']\n\n    if series_id_no in series_id_count:\n        series_id_count[series_id_no] += 1\n    else:\n        series_id_count[series_id_no] = 0\n\n    modified_series_id_no = f\"{series_id_no}_{series_id_count[series_id_no]}\"\n    X_feat.at[index, 'series_id_no'] = modified_series_id_no","metadata":{"execution":{"iopub.status.busy":"2023-11-13T15:23:40.361712Z","iopub.execute_input":"2023-11-13T15:23:40.362533Z","iopub.status.idle":"2023-11-13T15:23:44.882358Z","shell.execute_reply.started":"2023-11-13T15:23:40.362484Z","shell.execute_reply":"2023-11-13T15:23:44.881333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 0_0, 0_1, 0_3, ... , 9_0, 9_1, 9_2로 변환된 'series_id_no' 를 가지고 fft 데이터프레임과 기존 피쳐 데이터프레임 병합\nX = pd.merge(X_feat, X_fft, on='series_id_no', how='inner')\n\n\n# series_id_no를 다시 0, 0, 0, ... ,9, 9, 9의 형식으로 변경\nX['series_id_no'] = X['series_id_no'].astype(str).apply(lambda x: x.split('_')[0])\n\n# series_id_no의 형식을 numeric으로 변경\nX['series_id_no'] = pd.to_numeric(X['series_id_no'])","metadata":{"execution":{"iopub.status.busy":"2023-11-13T15:23:44.883887Z","iopub.execute_input":"2023-11-13T15:23:44.884282Z","iopub.status.idle":"2023-11-13T15:23:44.974741Z","shell.execute_reply.started":"2023-11-13T15:23:44.884249Z","shell.execute_reply":"2023-11-13T15:23:44.973534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"drop_cols = ['series_id_no', 'step', 'timestamp']\n\n\ny = X['event']\n\nX = X.drop(columns=drop_cols+['event'])","metadata":{"execution":{"iopub.status.busy":"2023-11-13T15:23:44.976344Z","iopub.execute_input":"2023-11-13T15:23:44.976706Z","iopub.status.idle":"2023-11-13T15:23:44.990644Z","shell.execute_reply.started":"2023-11-13T15:23:44.976675Z","shell.execute_reply":"2023-11-13T15:23:44.989528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lgb_params1 = {    \n    'boosting_type':'gbdt',\n    'num_leaves':31,# 기존 노트북의 40에서 기본값인 31로 변경\n    'max_depth':6,\n    'learning_rate':0.02,\n    'n_estimators':100,# 기존 노트북의 1000에서 기본값인 100으로 변경\n    'subsample_for_bin':200000,\n    'min_child_weight':0.001,\n    'min_child_samples':20,\n    'subsample':0.9,\n#     'colsample_bytree':0.7,\n    'reg_alpha':0.05,\n    'reg_lambda':0.05,\n             }\nimport xgboost as xgb\n\n\n# Training classifier\n\n\nmodel = lgb.LGBMClassifier(random_state=42, **lgb_params1, verbose=1) ######### 수정함\n\n\n'''\nformer parameter values\n\nlgb_params1 = {    \n    'boosting_type':'gbdt',\n    'num_leaves':40,\n    'max_depth':6,\n    'learning_rate':0.02,\n    'n_estimators':1000,\n    'subsample_for_bin':200000,\n    'min_child_weight':0.001,\n    'min_child_samples':20,\n    'subsample':0.9,\n#     'colsample_bytree':0.7,\n    'reg_alpha':0.05,\n    'reg_lambda':0.05,\n             }\nimport xgboost as xgb\n\n\ndefault parameter values\n    \n    'boosting_type': 'gbdt', # Gradient Boosting Decision Tree\n    'num_leaves': 31, # Max number of leaves in one tree\n    'max_depth': -1, # No limit, unless specified\n    'learning_rate': 0.1, # The efficiency of the model learning\n    'n_estimators': 100, # Number of boosted trees to fit\n    'subsample_for_bin': 200000, # Number of samples for constructing bins\n    'objective': None, # The loss to minimize during optimization\n    'class_weight': None, # Weights associated with classes\n    'min_split_gain': 0., # The minimum loss reduction required to make a further partition on a leaf node of the tree\n    'min_child_weight': 1e-3, # Minimum sum of instance weight (hessian) needed in a child (leaf)\n    'min_child_samples': 20, # Minimum number of data needed in a child (leaf)\n    'subsample': 1., # Subsample ratio of the training instance\n    'subsample_freq': 0, # Frequence of subsample, <=0 means no enable\n    'colsample_bytree': 1., # Subsample ratio of columns when constructing each tree\n    'reg_alpha': 0., # L1 regularization term on weights\n    'reg_lambda': 0., # L2 regularization term on weights\n    'random_state': None, # Random number seed\n    # ... there are other parameters as well which have default values\n    \n\n\nan advice from a team member\n\n추가로, lgb_params가 너무 깊습니다.\n먼저 lgb_params의 leaves와 estimators 를 좀 낮춘 후 진행하시는 걸 추천드립니다.\n파라미터에 대한 최적화는 일단 스코어링이 진행되고 나서 값을 조정해도 되니, 최대한 간단한 값으로 하시는 걸 추천드립니다.\n'''\n","metadata":{"execution":{"iopub.status.busy":"2023-11-13T15:23:44.992493Z","iopub.execute_input":"2023-11-13T15:23:44.992870Z","iopub.status.idle":"2023-11-13T15:23:45.040132Z","shell.execute_reply.started":"2023-11-13T15:23:44.992832Z","shell.execute_reply":"2023-11-13T15:23:45.038887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\nmodel.fit(X, y)","metadata":{"execution":{"iopub.status.busy":"2023-11-13T15:41:42.886833Z","iopub.execute_input":"2023-11-13T15:41:42.887348Z","iopub.status.idle":"2023-11-13T15:41:44.976707Z","shell.execute_reply.started":"2023-11-13T15:41:42.887314Z","shell.execute_reply":"2023-11-13T15:41:44.975480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.score(X, y)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feats = []\n\nfeat_imp = pd.Series(model.booster_.feature_importance(importance_type='gain'), index=X.columns).sort_values()\nfeats.append(feat_imp)","metadata":{"execution":{"iopub.status.busy":"2023-11-13T15:26:21.077305Z","iopub.execute_input":"2023-11-13T15:26:21.077848Z","iopub.status.idle":"2023-11-13T15:26:21.086687Z","shell.execute_reply.started":"2023-11-13T15:26:21.077808Z","shell.execute_reply":"2023-11-13T15:26:21.085227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feat_imp = pd.Series(pd.concat(feats, axis=1).mean(axis=1), index=feats[0].index).sort_values()\nprint('Columns with poor contribution', feat_imp[feat_imp<0.001].index, sep='\\n')\nfig = px.bar(x=feat_imp, y=feat_imp.index, orientation='h')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-13T15:26:22.216745Z","iopub.execute_input":"2023-11-13T15:26:22.217304Z","iopub.status.idle":"2023-11-13T15:26:22.841020Z","shell.execute_reply.started":"2023-11-13T15:26:22.217266Z","shell.execute_reply":"2023-11-13T15:26:22.839814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feat_imp.sort_values().tail(20)","metadata":{"execution":{"iopub.status.busy":"2023-11-13T15:26:26.385949Z","iopub.execute_input":"2023-11-13T15:26:26.386876Z","iopub.status.idle":"2023-11-13T15:26:26.401609Z","shell.execute_reply.started":"2023-11-13T15:26:26.386831Z","shell.execute_reply":"2023-11-13T15:26:26.400465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# del X, y\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-11-13T15:26:26.852011Z","iopub.execute_input":"2023-11-13T15:26:26.852497Z","iopub.status.idle":"2023-11-13T15:26:27.073143Z","shell.execute_reply.started":"2023-11-13T15:26:26.852460Z","shell.execute_reply":"2023-11-13T15:26:27.071555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-11-13T15:41:59.578047Z","iopub.execute_input":"2023-11-13T15:41:59.578506Z","iopub.status.idle":"2023-11-13T15:41:59.657266Z","shell.execute_reply.started":"2023-11-13T15:41:59.578472Z","shell.execute_reply":"2023-11-13T15:41:59.656290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}