{"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":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport seaborn as sns\n\nimport os\nfrom tensorflow.keras import layers\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.metrics import r2_score\nfrom sklearn import metrics \n\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-11T14:49:49.805230Z","iopub.execute_input":"2022-07-11T14:49:49.805817Z","iopub.status.idle":"2022-07-11T14:50:01.190193Z","shell.execute_reply.started":"2022-07-11T14:49:49.805689Z","shell.execute_reply":"2022-07-11T14:50:01.188807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Scale Helper Funcations**","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler,RobustScaler\nsc = StandardScaler()\nrb = RobustScaler()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T14:50:01.191934Z","iopub.execute_input":"2022-07-11T14:50:01.192544Z","iopub.status.idle":"2022-07-11T14:50:01.197901Z","shell.execute_reply.started":"2022-07-11T14:50:01.192501Z","shell.execute_reply":"2022-07-11T14:50:01.196765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Reading the Data**","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('../input/railofy-challenge/Railofy_training_data_for_model.csv')\ntest = pd.read_csv(\"../input/railofy-challenge/Railofy_testing_data_for_model.csv\")\n\n#targets = tf.keras.utils.to_categorical(train['Class'], num_classes=11, dtype='float32')\ntargets = train['target']","metadata":{"execution":{"iopub.status.busy":"2022-07-11T14:50:01.199356Z","iopub.execute_input":"2022-07-11T14:50:01.200834Z","iopub.status.idle":"2022-07-11T14:50:01.545238Z","shell.execute_reply.started":"2022-07-11T14:50:01.200790Z","shell.execute_reply":"2022-07-11T14:50:01.543842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### **Checking Null Value**","metadata":{}},{"cell_type":"code","source":"train.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T14:50:01.547362Z","iopub.execute_input":"2022-07-11T14:50:01.547709Z","iopub.status.idle":"2022-07-11T14:50:01.563146Z","shell.execute_reply.started":"2022-07-11T14:50:01.547679Z","shell.execute_reply":"2022-07-11T14:50:01.561898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Data Visualization**","metadata":{}},{"cell_type":"code","source":"train['target'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T14:50:01.564389Z","iopub.execute_input":"2022-07-11T14:50:01.564932Z","iopub.status.idle":"2022-07-11T14:50:01.578350Z","shell.execute_reply.started":"2022-07-11T14:50:01.564895Z","shell.execute_reply":"2022-07-11T14:50:01.576863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-07-11T14:50:01.579733Z","iopub.execute_input":"2022-07-11T14:50:01.580128Z","iopub.status.idle":"2022-07-11T14:50:01.627815Z","shell.execute_reply.started":"2022-07-11T14:50:01.580095Z","shell.execute_reply":"2022-07-11T14:50:01.626878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(data= train, x=\"target\")","metadata":{"execution":{"iopub.status.busy":"2022-07-11T14:50:01.628954Z","iopub.execute_input":"2022-07-11T14:50:01.629707Z","iopub.status.idle":"2022-07-11T14:50:01.820301Z","shell.execute_reply.started":"2022-07-11T14:50:01.629672Z","shell.execute_reply":"2022-07-11T14:50:01.818954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#correlation matrix\ncorrmat= train.corr()\nplt.figure(figsize=(15,15))  \n\ncmap = sns.diverging_palette(250, 10, s=80, l=55, n=9, as_cmap=True)\n\nsns.heatmap(corrmat,annot=True, cmap=cmap, center=0)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T14:50:01.821752Z","iopub.execute_input":"2022-07-11T14:50:01.822557Z","iopub.status.idle":"2022-07-11T14:50:04.164094Z","shell.execute_reply.started":"2022-07-11T14:50:01.822516Z","shell.execute_reply":"2022-07-11T14:50:04.163092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set(rc = {'figure.figsize':(15,8)})\nsns.heatmap(train.corr(),annot=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T14:50:04.165631Z","iopub.execute_input":"2022-07-11T14:50:04.165959Z","iopub.status.idle":"2022-07-11T14:50:06.768795Z","shell.execute_reply.started":"2022-07-11T14:50:04.165930Z","shell.execute_reply":"2022-07-11T14:50:06.767663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.drop(['pk','target'], axis=1, inplace=True)\ntest.drop(['pk'], axis=1, inplace=True)\n\ntrain['QT'].replace({'GN': 0, 'RL': 1, 'PQ': 2}, inplace=True)\ntest['QT'].replace({'GN': 0, 'RL': 1, 'PQ': 2}, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T14:50:35.837788Z","iopub.execute_input":"2022-07-11T14:50:35.838241Z","iopub.status.idle":"2022-07-11T14:50:35.868184Z","shell.execute_reply.started":"2022-07-11T14:50:35.838206Z","shell.execute_reply":"2022-07-11T14:50:35.867193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape, test.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-11T14:50:36.566816Z","iopub.execute_input":"2022-07-11T14:50:36.567686Z","iopub.status.idle":"2022-07-11T14:50:36.575062Z","shell.execute_reply.started":"2022-07-11T14:50:36.567634Z","shell.execute_reply":"2022-07-11T14:50:36.573620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Scale the Data** ","metadata":{}},{"cell_type":"code","source":"rb.fit(train)\ntrain = rb.transform(train)\ntest = rb.transform(test)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T14:50:37.736749Z","iopub.execute_input":"2022-07-11T14:50:37.737548Z","iopub.status.idle":"2022-07-11T14:50:37.792245Z","shell.execute_reply.started":"2022-07-11T14:50:37.737491Z","shell.execute_reply":"2022-07-11T14:50:37.790965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_valid, y_train, y_valid = train_test_split(train, targets, test_size = 0.2, random_state = 2021)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T14:50:38.530810Z","iopub.execute_input":"2022-07-11T14:50:38.531225Z","iopub.status.idle":"2022-07-11T14:50:38.545852Z","shell.execute_reply.started":"2022-07-11T14:50:38.531192Z","shell.execute_reply":"2022-07-11T14:50:38.544572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape, y_train.shape, X_valid.shape, y_valid.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-11T14:50:41.244138Z","iopub.execute_input":"2022-07-11T14:50:41.244738Z","iopub.status.idle":"2022-07-11T14:50:41.252634Z","shell.execute_reply.started":"2022-07-11T14:50:41.244694Z","shell.execute_reply":"2022-07-11T14:50:41.251596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Model Training**","metadata":{}},{"cell_type":"code","source":"model = RandomForestClassifier(random_state = 2022, n_jobs = -1, n_estimators = 10000, verbose=1)\nmodel.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T14:50:42.552902Z","iopub.execute_input":"2022-07-11T14:50:42.553472Z","iopub.status.idle":"2022-07-11T14:55:32.297001Z","shell.execute_reply.started":"2022-07-11T14:50:42.553438Z","shell.execute_reply":"2022-07-11T14:55:32.295802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Prediction of Validatation Data**","metadata":{}},{"cell_type":"code","source":"y_pred = model.predict(X_valid)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T14:55:32.299117Z","iopub.execute_input":"2022-07-11T14:55:32.299901Z","iopub.status.idle":"2022-07-11T14:55:40.135655Z","shell.execute_reply.started":"2022-07-11T14:55:32.299849Z","shell.execute_reply":"2022-07-11T14:55:40.134385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Accuracy of Model**","metadata":{}},{"cell_type":"code","source":"print(\"\\nACCURACY OF THE MODEL: \", metrics.accuracy_score(y_valid, y_pred))","metadata":{"execution":{"iopub.status.busy":"2022-07-11T14:55:40.137349Z","iopub.execute_input":"2022-07-11T14:55:40.137827Z","iopub.status.idle":"2022-07-11T14:55:40.146601Z","shell.execute_reply.started":"2022-07-11T14:55:40.137746Z","shell.execute_reply":"2022-07-11T14:55:40.145356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Submission Preparation**","metadata":{}},{"cell_type":"code","source":"o_pred = model.predict_proba(test)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T14:55:40.149059Z","iopub.execute_input":"2022-07-11T14:55:40.149386Z","iopub.status.idle":"2022-07-11T14:55:47.091683Z","shell.execute_reply.started":"2022-07-11T14:55:40.149357Z","shell.execute_reply":"2022-07-11T14:55:47.090391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv(\"../input/railofy-challenge/Railofy_testing_data_for_model.csv\")\nsubmission_file = pd.DataFrame()\n\n\nsubmission_file['pk'] = test['pk']\nsubmission_file['target'] = o_pred[:,1]\nsubmission_file.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T14:57:55.349530Z","iopub.execute_input":"2022-07-11T14:57:55.350451Z","iopub.status.idle":"2022-07-11T14:57:55.393750Z","shell.execute_reply.started":"2022-07-11T14:57:55.350402Z","shell.execute_reply":"2022-07-11T14:57:55.392823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_file","metadata":{"execution":{"iopub.status.busy":"2022-07-11T14:57:55.890034Z","iopub.execute_input":"2022-07-11T14:57:55.890618Z","iopub.status.idle":"2022-07-11T14:57:55.903728Z","shell.execute_reply.started":"2022-07-11T14:57:55.890583Z","shell.execute_reply":"2022-07-11T14:57:55.902702Z"},"trusted":true},"execution_count":null,"outputs":[]}]}