{"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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-11-30T01:42:46.508646Z","iopub.execute_input":"2022-11-30T01:42:46.509875Z","iopub.status.idle":"2022-11-30T01:42:46.522548Z","shell.execute_reply.started":"2022-11-30T01:42:46.509818Z","shell.execute_reply":"2022-11-30T01:42:46.521372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub_path = \"/kaggle/input/amex-default-prediction/sample_submission.csv\"\ndf_train_path = \"/kaggle/input/amex-default-prediction/train_data.csv\"\ndf_test_path = \"/kaggle/input/amex-default-prediction/test_data.csv\"\ndf_labels_path = \"/kaggle/input/amex-default-prediction/train_labels.csv\"\n\nsample_size = 10000\nbatch_size = 200\n\ntrain_reader = pd.read_csv(df_train_path, dtype=str, chunksize=batch_size)\nlabels_reader = pd.read_csv(df_labels_path, dtype=str, chunksize=batch_size)\ntest_reader = pd.read_csv(df_test_path, dtype=str, chunksize=batch_size)\n\n\ndf_train = train_reader.get_chunk(sample_size)\ndf_labels = labels_reader.get_chunk(sample_size)\ndf_test = test_reader.get_chunk(sample_size)","metadata":{"execution":{"iopub.status.busy":"2022-11-30T01:42:46.528811Z","iopub.execute_input":"2022-11-30T01:42:46.529670Z","iopub.status.idle":"2022-11-30T01:42:48.427255Z","shell.execute_reply.started":"2022-11-30T01:42:46.529626Z","shell.execute_reply":"2022-11-30T01:42:48.425974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-30T01:42:48.429305Z","iopub.execute_input":"2022-11-30T01:42:48.429672Z","iopub.status.idle":"2022-11-30T01:42:48.454951Z","shell.execute_reply.started":"2022-11-30T01:42:48.429637Z","shell.execute_reply":"2022-11-30T01:42:48.453590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_Xy = pd.merge(df_train, df_labels, on='customer_ID')\ndf_train_Xy = df_train_Xy.drop(columns=['S_2'])\ncategorical_cols = ['B_30', 'B_38', 'D_114', 'D_116', 'D_117', 'D_120', 'D_126', 'D_63', 'D_64', 'D_66', 'D_68','target']\nall_cols = list(df_train_Xy.columns)\ncont_cols = list(set(all_cols)-set(categorical_cols))\ncont_cols = cont_cols[:5] + ['target']","metadata":{"execution":{"iopub.status.busy":"2022-11-30T01:42:48.456740Z","iopub.execute_input":"2022-11-30T01:42:48.457056Z","iopub.status.idle":"2022-11-30T01:42:48.650218Z","shell.execute_reply.started":"2022-11-30T01:42:48.457027Z","shell.execute_reply":"2022-11-30T01:42:48.649005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cont_cols","metadata":{"execution":{"iopub.status.busy":"2022-11-30T01:42:48.652324Z","iopub.execute_input":"2022-11-30T01:42:48.653239Z","iopub.status.idle":"2022-11-30T01:42:48.659697Z","shell.execute_reply.started":"2022-11-30T01:42:48.653205Z","shell.execute_reply":"2022-11-30T01:42:48.658598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_Xy = df_train_Xy[cont_cols]\ndf_train_Xy['target'] = df_train_Xy['target'].astype('category')\nX,y = df_train_Xy.iloc[:,:-1], df_train_Xy.iloc[:,-1]","metadata":{"execution":{"iopub.status.busy":"2022-11-30T01:42:48.663498Z","iopub.execute_input":"2022-11-30T01:42:48.664020Z","iopub.status.idle":"2022-11-30T01:42:48.690200Z","shell.execute_reply.started":"2022-11-30T01:42:48.663978Z","shell.execute_reply":"2022-11-30T01:42:48.688890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-30T01:42:48.691617Z","iopub.execute_input":"2022-11-30T01:42:48.692346Z","iopub.status.idle":"2022-11-30T01:42:48.705819Z","shell.execute_reply.started":"2022-11-30T01:42:48.692302Z","shell.execute_reply":"2022-11-30T01:42:48.704729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = X.fillna(0)","metadata":{"execution":{"iopub.status.busy":"2022-11-30T01:42:48.707301Z","iopub.execute_input":"2022-11-30T01:42:48.707686Z","iopub.status.idle":"2022-11-30T01:42:48.719384Z","shell.execute_reply.started":"2022-11-30T01:42:48.707655Z","shell.execute_reply":"2022-11-30T01:42:48.718158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=123)","metadata":{"execution":{"iopub.status.busy":"2022-11-30T01:42:48.721727Z","iopub.execute_input":"2022-11-30T01:42:48.722068Z","iopub.status.idle":"2022-11-30T01:42:48.735905Z","shell.execute_reply.started":"2022-11-30T01:42:48.722038Z","shell.execute_reply":"2022-11-30T01:42:48.734835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\nmodel = RandomForestClassifier(n_estimators=5, max_depth=2, random_state=0)\nmodel.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-11-30T01:42:48.737253Z","iopub.execute_input":"2022-11-30T01:42:48.737605Z","iopub.status.idle":"2022-11-30T01:42:48.789827Z","shell.execute_reply.started":"2022-11-30T01:42:48.737575Z","shell.execute_reply":"2022-11-30T01:42:48.788974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.score(X_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2022-11-30T01:42:48.790845Z","iopub.execute_input":"2022-11-30T01:42:48.791700Z","iopub.status.idle":"2022-11-30T01:42:48.809183Z","shell.execute_reply.started":"2022-11-30T01:42:48.791669Z","shell.execute_reply":"2022-11-30T01:42:48.807936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test1 = df_test.copy()\ndf_test = df_test[cont_cols[:5]].fillna(0)","metadata":{"execution":{"iopub.status.busy":"2022-11-30T01:42:48.810604Z","iopub.execute_input":"2022-11-30T01:42:48.810913Z","iopub.status.idle":"2022-11-30T01:42:48.881117Z","shell.execute_reply.started":"2022-11-30T01:42:48.810885Z","shell.execute_reply":"2022-11-30T01:42:48.879815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-30T01:42:48.882651Z","iopub.execute_input":"2022-11-30T01:42:48.883696Z","iopub.status.idle":"2022-11-30T01:42:48.897313Z","shell.execute_reply.started":"2022-11-30T01:42:48.883651Z","shell.execute_reply":"2022-11-30T01:42:48.896097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.predict(df_test)","metadata":{"execution":{"iopub.status.busy":"2022-11-30T01:42:48.898834Z","iopub.execute_input":"2022-11-30T01:42:48.899327Z","iopub.status.idle":"2022-11-30T01:42:48.923606Z","shell.execute_reply.started":"2022-11-30T01:42:48.899296Z","shell.execute_reply":"2022-11-30T01:42:48.922427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = model.predict(df_test)","metadata":{"execution":{"iopub.status.busy":"2022-11-30T01:42:48.928377Z","iopub.execute_input":"2022-11-30T01:42:48.929511Z","iopub.status.idle":"2022-11-30T01:42:48.951041Z","shell.execute_reply.started":"2022-11-30T01:42:48.929464Z","shell.execute_reply":"2022-11-30T01:42:48.949909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction","metadata":{"execution":{"iopub.status.busy":"2022-11-30T01:42:48.953227Z","iopub.execute_input":"2022-11-30T01:42:48.953692Z","iopub.status.idle":"2022-11-30T01:42:48.961284Z","shell.execute_reply.started":"2022-11-30T01:42:48.953649Z","shell.execute_reply":"2022-11-30T01:42:48.960366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'customer_ID':df_test1['customer_ID'],'prediction':prediction})","metadata":{"execution":{"iopub.status.busy":"2022-11-30T01:42:48.963254Z","iopub.execute_input":"2022-11-30T01:42:48.963837Z","iopub.status.idle":"2022-11-30T01:42:48.973869Z","shell.execute_reply.started":"2022-11-30T01:42:48.963792Z","shell.execute_reply":"2022-11-30T01:42:48.972703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv',index = False)\nsubmission","metadata":{"execution":{"iopub.status.busy":"2022-11-30T01:42:48.975822Z","iopub.execute_input":"2022-11-30T01:42:48.976337Z","iopub.status.idle":"2022-11-30T01:42:49.014091Z","shell.execute_reply.started":"2022-11-30T01:42:48.976293Z","shell.execute_reply":"2022-11-30T01:42:49.012964Z"},"trusted":true},"execution_count":null,"outputs":[]}]}