{"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-07-06T10:30:50.017391Z","iopub.execute_input":"2022-07-06T10:30:50.017951Z","iopub.status.idle":"2022-07-06T10:30:50.057222Z","shell.execute_reply.started":"2022-07-06T10:30:50.017802Z","shell.execute_reply":"2022-07-06T10:30:50.056219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n!pip install xgboost\nimport xgboost as xgb\nimport matplotlib\nmatplotlib.use('Agg')\n#matplotlib.style.use('ggplot')\nfrom sklearn.model_selection import cross_val_score\nfrom sklearn.model_selection import RepeatedStratifiedKFold\nfrom sklearn.model_selection import StratifiedKFold\nfrom sklearn.model_selection import GridSearchCV\nfrom sklearn import preprocessing\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.feature_selection import SelectFromModel\nfrom sklearn.metrics import roc_auc_score\nfrom matplotlib import pyplot\nfrom sklearn.metrics import roc_auc_score, accuracy_score, plot_roc_curve\nfrom sklearn.metrics import f1_score\n","metadata":{"execution":{"iopub.status.busy":"2022-07-06T10:30:50.091545Z","iopub.execute_input":"2022-07-06T10:30:50.094233Z","iopub.status.idle":"2022-07-06T10:31:07.486283Z","shell.execute_reply.started":"2022-07-06T10:30:50.094187Z","shell.execute_reply":"2022-07-06T10:31:07.485222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a = pd.read_csv('/kaggle/input/company-bankruptcy-prediction/train.csv')\nb = pd.read_csv('/kaggle/input/company-bankruptcy-prediction/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-06T10:31:07.488597Z","iopub.execute_input":"2022-07-06T10:31:07.489184Z","iopub.status.idle":"2022-07-06T10:31:09.140938Z","shell.execute_reply.started":"2022-07-06T10:31:07.48913Z","shell.execute_reply":"2022-07-06T10:31:09.139812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T10:31:09.14232Z","iopub.execute_input":"2022-07-06T10:31:09.143443Z","iopub.status.idle":"2022-07-06T10:31:09.185838Z","shell.execute_reply.started":"2022-07-06T10:31:09.14339Z","shell.execute_reply":"2022-07-06T10:31:09.184701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"b.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T10:31:09.188955Z","iopub.execute_input":"2022-07-06T10:31:09.194101Z","iopub.status.idle":"2022-07-06T10:31:09.217912Z","shell.execute_reply.started":"2022-07-06T10:31:09.19407Z","shell.execute_reply":"2022-07-06T10:31:09.21679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a","metadata":{"execution":{"iopub.status.busy":"2022-07-06T10:31:09.219577Z","iopub.execute_input":"2022-07-06T10:31:09.220026Z","iopub.status.idle":"2022-07-06T10:31:09.274195Z","shell.execute_reply.started":"2022-07-06T10:31:09.219987Z","shell.execute_reply":"2022-07-06T10:31:09.273216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"b","metadata":{"execution":{"iopub.status.busy":"2022-07-06T10:31:09.275489Z","iopub.execute_input":"2022-07-06T10:31:09.276254Z","iopub.status.idle":"2022-07-06T10:31:09.316709Z","shell.execute_reply.started":"2022-07-06T10:31:09.276214Z","shell.execute_reply":"2022-07-06T10:31:09.315402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a=a.apply(pd.to_numeric, errors='coerce', downcast='integer')\nb=b.apply(pd.to_numeric, errors='coerce', downcast='integer')","metadata":{"execution":{"iopub.status.busy":"2022-07-06T10:31:09.318646Z","iopub.execute_input":"2022-07-06T10:31:09.319008Z","iopub.status.idle":"2022-07-06T10:31:10.494909Z","shell.execute_reply.started":"2022-07-06T10:31:09.318971Z","shell.execute_reply":"2022-07-06T10:31:10.493831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"b.describe()\na.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T10:31:10.496565Z","iopub.execute_input":"2022-07-06T10:31:10.496933Z","iopub.status.idle":"2022-07-06T10:31:10.895465Z","shell.execute_reply.started":"2022-07-06T10:31:10.496894Z","shell.execute_reply":"2022-07-06T10:31:10.89452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T10:31:10.896924Z","iopub.execute_input":"2022-07-06T10:31:10.898442Z","iopub.status.idle":"2022-07-06T10:31:11.132159Z","shell.execute_reply.started":"2022-07-06T10:31:10.8984Z","shell.execute_reply":"2022-07-06T10:31:11.131143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"b.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T10:31:11.136062Z","iopub.execute_input":"2022-07-06T10:31:11.138455Z","iopub.status.idle":"2022-07-06T10:31:11.330705Z","shell.execute_reply.started":"2022-07-06T10:31:11.138421Z","shell.execute_reply":"2022-07-06T10:31:11.328315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%matplotlib inline\n\na['class'].value_counts().plot(kind='bar')","metadata":{"execution":{"iopub.status.busy":"2022-07-06T10:31:11.3324Z","iopub.execute_input":"2022-07-06T10:31:11.332801Z","iopub.status.idle":"2022-07-06T10:31:11.526085Z","shell.execute_reply.started":"2022-07-06T10:31:11.332762Z","shell.execute_reply":"2022-07-06T10:31:11.525119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X, y = a.iloc[:,1:66], a.iloc[:,66]\ntest = b.iloc[:,1:66]\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.2, random_state=123)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T10:31:11.52769Z","iopub.execute_input":"2022-07-06T10:31:11.528065Z","iopub.status.idle":"2022-07-06T10:31:11.557061Z","shell.execute_reply.started":"2022-07-06T10:31:11.528026Z","shell.execute_reply":"2022-07-06T10:31:11.556026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"paramGrid = {\n    \n    'max_depth': [4],\n    'alpha': [0.01],\n    'subsample': [1],\n    'learning_rate': [0.1],\n    'n_estimators': [1500],\n    'colsample_bytree': [1],\n    'colsample_bylevel': [1],\n    'scale_pos_weight': [1],\n    'objective':['binary:logistic'],\n    'eval_metric': ['logloss']\n}\n\n\nxgb_clf = xgb.XGBClassifier(tree_method='gpu_hist', gpu_id=0)\ncv = StratifiedKFold()\nxgb_rs = GridSearchCV(estimator=xgb_clf, param_grid=paramGrid, cv=cv, verbose=1, scoring='f1')\nxgb_rs.fit(X_train, y_train)\nprint(\"Best parameters found:\", xgb_rs.best_params_)\nprint(\"Best accuracy found:\", xgb_rs.best_score_)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T10:31:11.558699Z","iopub.execute_input":"2022-07-06T10:31:11.559102Z","iopub.status.idle":"2022-07-06T10:31:30.949166Z","shell.execute_reply.started":"2022-07-06T10:31:11.559063Z","shell.execute_reply":"2022-07-06T10:31:30.947982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best = xgb_rs.best_estimator_\npreds = (xgb_rs.predict_proba(X_test)[:,1] >= 0.2).astype(int)\nprint(f1_score(y_test, preds))","metadata":{"execution":{"iopub.status.busy":"2022-07-06T10:31:30.951254Z","iopub.execute_input":"2022-07-06T10:31:30.951955Z","iopub.status.idle":"2022-07-06T10:31:31.074002Z","shell.execute_reply.started":"2022-07-06T10:31:30.951912Z","shell.execute_reply":"2022-07-06T10:31:31.073219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(accuracy_score(y_test, preds))","metadata":{"execution":{"iopub.status.busy":"2022-07-06T10:31:31.077972Z","iopub.execute_input":"2022-07-06T10:31:31.080183Z","iopub.status.idle":"2022-07-06T10:31:31.088703Z","shell.execute_reply.started":"2022-07-06T10:31:31.080111Z","shell.execute_reply":"2022-07-06T10:31:31.087526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_test = xgb_rs.predict(test)\ntest['class'] = preds_test\ntest['id'] = b['id']\nfinal = test[['id','class']]","metadata":{"execution":{"iopub.status.busy":"2022-07-06T10:31:31.090056Z","iopub.execute_input":"2022-07-06T10:31:31.090779Z","iopub.status.idle":"2022-07-06T10:31:31.367179Z","shell.execute_reply.started":"2022-07-06T10:31:31.090733Z","shell.execute_reply":"2022-07-06T10:31:31.366207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final.to_csv('subklapsa.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T10:31:31.36884Z","iopub.execute_input":"2022-07-06T10:31:31.369252Z","iopub.status.idle":"2022-07-06T10:31:31.397562Z","shell.execute_reply.started":"2022-07-06T10:31:31.369214Z","shell.execute_reply":"2022-07-06T10:31:31.396521Z"},"trusted":true},"execution_count":null,"outputs":[]}]}