{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Categorical Feature Encoding Challenge II"},{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","trusted":true},"cell_type":"code","source":"import numpy as np \nimport pandas as pd\nimport os\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.model_selection import cross_val_score\nfrom sklearn.preprocessing import MinMaxScaler","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Load the training data."},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"train_df = pd.read_csv(\"/kaggle/input/cat-in-the-dat-ii/train.csv\")\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Load the test data."},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df = pd.read_csv(\"/kaggle/input/cat-in-the-dat-ii/test.csv\")\ntest_df.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Concatenate training and test data.\n\nAfterwards convert the categorical data into one hot encoded columns.\n\n\nNote: The columns \"nom_5\",\"nom_6\",\"nom_7\",\"nom_8\",\"nom_9\" have a very high cardinality and are unpractical for one hot encoding.\nThat's why I left them out for now."},{"metadata":{"trusted":true},"cell_type":"code","source":"total_df = train_df.append(test_df,sort=False).drop(columns=[\"target\",\"id\"])\nX_cat = pd.get_dummies(total_df.drop(columns=[\"nom_5\",\"nom_6\",\"nom_7\",\"nom_8\",\"nom_9\"])).fillna(0).astype(\"uint8\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_cat.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Split the transformed train and test data again."},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train = X_cat[:600000]\nX_test = X_cat[600000:]\n\ny_train = train_df[\"target\"]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Scale the data."},{"metadata":{"trusted":true},"cell_type":"code","source":"scaler = MinMaxScaler()\nX_train = scaler.fit_transform(X_train)\nX_test = scaler.transform(X_test)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Get the crossvalidation score to test the model."},{"metadata":{"trusted":true},"cell_type":"code","source":"clf = LogisticRegression(solver=\"saga\",max_iter=200,random_state=0)\nprint(\"crossvalidation score:\",cross_val_score(clf,X_train,y_train,cv=3,scoring='roc_auc').mean())","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Train the classifier, predict the labels and create a submission."},{"metadata":{"trusted":true},"cell_type":"code","source":"clf.fit(X_train,y_train)\nsubmission = pd.DataFrame()\nsubmission[\"id\"] = test_df[\"id\"]\nsubmission[\"target\"] = clf.predict_proba(X_test)[:,1]\npd.DataFrame(submission).to_csv(\"submission.csv\",index=False)\nprint(pd.read_csv(\"submission.csv\"))","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.6.6"}},"nbformat":4,"nbformat_minor":1}