{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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 in \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 \"../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# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"testData  = pd.read_csv(\"/kaggle/input/cat-in-the-dat-ii/test.csv\" , index_col = 'id')\ntrainData  = pd.read_csv(\"/kaggle/input/cat-in-the-dat-ii/train.csv\", index_col = 'id')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"combinedData = pd.concat([ testData, trainData] , sort = False, ignore_index = True )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#creating new index\nts = list(range(0, 1000000))\ndf = pd.DataFrame( ts , columns = [\"id\"])\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"comData = pd.concat([df, combinedData], axis = 1) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"comData.set_index('id', inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Slicing Data into different Dataframes\ncomBinary1Data = comData.iloc[ : , 0 : 3]\ncomBinary2Data = comData.iloc[ : , 3 : 5]\ncomNominalData = comData.iloc[ : , 5 : 15]\ncomOrdinalData = comData.iloc[ : , 15 : 21]\ncomCyclicData = comData.iloc[ : , 21 : 23]\ntargetData = comData.iloc[ : ,  23]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n#handling missing data \ncomBinary1Data.fillna(0 , inplace =True)\ncomBinary2Data.replace({ 'bin_3' : 'F' , 'bin_4' : 'N'} , 0, inplace = True)\ncomBinary2Data.replace({ 'bin_3' : 'T' , 'bin_4' : 'Y'} , 1, inplace = True)\ncomBinary2Data.fillna(method = 'ffill' , inplace =True)\n\ncomBinary2Data.head(50)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"comNominalData.nom_0.fillna(method = \"ffill\", inplace= True)\ncomNominalData.nom_2.fillna(\"Lion\", inplace= True)\ncomNominalData.nom_3.fillna(\"India\", inplace= True)\ncomNominalData.nom_4.fillna(\"Piano\", inplace= True)\ncomNominalData.iloc[ : , 5 : 10].fillna( '0', inplace= True)\ncomNominalData.nom_1.fillna( \"Polygon\" , inplace =True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"comOrdinalData.ord_0.fillna(method = \"ffill\", inplace= True)\ncomOrdinalData.ord_1.fillna(method = \"ffill\", inplace= True)\ncomOrdinalData.ord_2.fillna(method = \"ffill\", inplace= True)\ncomOrdinalData.ord_3.fillna(method = \"ffill\", inplace= True)\ncomOrdinalData.ord_4.fillna(method = \"ffill\", inplace= True)\ncomOrdinalData.ord_5.fillna(method = \"ffill\", inplace= True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"comCyclicData.day.fillna(method = \"ffill\", inplace= True)\ncomCyclicData.month.fillna(method = \"ffill\", inplace= True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"finalComData = pd.concat([comBinary1Data,\ncomBinary2Data,\ncomNominalData,\ncomOrdinalData,\ncomCyclicData,\ntargetData] , axis = 1)\nfinalComData.head(50)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"finalTrainData = finalComData[400000 : ]\nfinalTrainData.tail()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"finalTestData = finalComData[0:400000].drop('target' , axis = 1)\nfinalTestData.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.linear_model import LogisticRegression\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X=finalTrainData.drop(['target'],axis=1)\ny=finalTrainData['target']\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x=y.value_counts()\nplt.bar(x.index,x)\nplt.gca().set_xticks([0,1])\nplt.title('distribution of target variable')\nplt.show()\nx","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"le=pd.DataFrame()\nlabel=LabelEncoder()\nfor c in  X.columns:\n    if(X[c].dtype=='object'):\n        le[c]=label.fit_transform(X[c])\n    else:\n        le[c]=X[c]\n        \nle.head(3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"le_test = pd.DataFrame()\nfor c in  finalTestData.columns:\n    if(finalTestData[c].dtype=='object'):\n        le_test[c]=label.fit_transform(finalTestData[c])\n    else:\n        le_test[c]=finalTestData[c]\n        \nle_test.head(3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nlr=LogisticRegression()\nlr.fit(le,y)\ntarget_pre=lr.predict(le_test)\nlen(target_pre)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y.head(20)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('Accuracy : ',lr.score(le_test,target_pre))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"le_test[100:200]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\ncm = confusion_matrix(y[: 400000],target_pre )\n\nimport seaborn as sn\nplt.figure(figsize = (10 , 7))\nsn.heatmap(cm, annot =True)\nplt.xlabel('Predicted')\nplt.ylabel('Truth')\n\n","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}