{"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 5GB 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","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Categorical Feature Encoding Challenge 2","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Download data","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('../input/cat-in-the-dat-ii/train.csv')\ntest = pd.read_csv('../input/cat-in-the-dat-ii/test.csv')","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Defer test_id and target in advance","metadata":{}},{"cell_type":"code","source":"test_id = test.id\ntrain_target = train.target","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Let's look at the data","metadata":{}},{"cell_type":"code","source":"train.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Description","metadata":{}},{"cell_type":"code","source":"def description(df):\n    print(f'Dataset Shape:{df.shape}')\n    summary = pd.DataFrame(df.dtypes, columns=['dtypes'])\n    summary = summary.reset_index()\n    summary['Name'] = summary['index']\n    summary = summary[['Name','dtypes']]\n    summary['Missing'] = df.isnull().sum().values\n    summary['Uniques'] = df.nunique().values\n    summary['First Value'] = df.iloc[0].values\n    summary['Second Value'] = df.iloc[1].values\n    summary['Third Value'] = df.iloc[2].values\n    return summary\nprint('Variable Description of  train Data:')\ndescription(train)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"What data do we have?\n1. Binary data (bin_0 - bin_5) is a sign of an item, which indicates that the item consists of two parts or two elements (1/0; Yes / No)\n2. Nominal feature (nom_0 - nom_9) - a feature with a \"disordered state\", which has several different states that can be numbered.\n3. Ordinal attribute (ord_0 - ord_5) - attribute responsible for quality, can be ordered and arranged in ascending or descending order. Example - quality of living conditions (poor, fair, good, very good)\n4. Sign DateTime (day / month)","metadata":{}},{"cell_type":"markdown","source":"# Preprocessing","metadata":{}},{"cell_type":"code","source":"# Removing the unneeded \"id\" column\ntrain = train.drop('id', axis=1)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def replace_nan(data):\n    for column in data.columns:\n        if data[column].isna().sum() > 0:\n            data[column] = data[column].fillna(data[column].mode()[0])\n\n\n\nreplace_nan(train)\nreplace_nan(test)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"description(train)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualization data ","metadata":{}},{"cell_type":"markdown","source":"## 1.Target features ","metadata":{}},{"cell_type":"code","source":"import matplotlib.ticker as ticker\n\nplt.figure(figsize=(10,7))\nncount = len(train)\n\nax = sns.countplot(train.target, palette='coolwarm')\nplt.title(\"TARGET DISTRIBUTION\", fontsize = 20)\nplt.xlabel('Number of Axles', fontsize = 20)\n\n# Make twin axis\nax2=ax.twinx()\n\n# Switch so count axis is on right, frequency on left\nax2.yaxis.tick_left()\nax.yaxis.tick_right()\n\n# Also switch the labels over\nax.yaxis.set_label_position('right')\nax2.yaxis.set_label_position('left')\n\nax2.set_ylabel('Frequency [%]', fontsize = 15)\nax.set_ylabel('Count', fontsize = 15)\n\nfor p in ax.patches:\n    x=p.get_bbox().get_points()[:,0]\n    y=p.get_bbox().get_points()[1,1]\n    ax.annotate('{:.1f}%'.format(100.*y/ncount), (x.mean(), y), \n            ha='center', va='bottom') # set the alignment of the text\n\n# Use a LinearLocator to ensure the correct number of ticks\nax.yaxis.set_major_locator(ticker.LinearLocator(11))\n\n# Fix the frequency range to 0-100\nax2.set_ylim(0,100)\nax.set_ylim(0,ncount)\n\n# And use a MultipleLocator to ensure a tick spacing of 10\nax2.yaxis.set_major_locator(ticker.MultipleLocator(10))\n\n# Need to turn the grid on ax2 off, otherwise the gridlines end up on top of the bars\nax2.grid(True)","metadata":{"_kg_hide-output":false,"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Binary Features","metadata":{}},{"cell_type":"code","source":"import matplotlib.gridspec as gridspec # to do the grid of plots","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bin_list = ['bin_0','bin_1', 'bin_2', 'bin_3', 'bin_4']\n\n\ngrid = gridspec.GridSpec(3, 2) # The grid of chart\nplt.figure(figsize=(16,20))\n\nfor n, col in enumerate(train[bin_list]): \n    ax = plt.subplot(grid[n]) # feeding the figure of grid\n    sns.countplot(x=col, data=train, hue = 'target',palette='Set2') \n    ax.set_ylabel('Count', fontsize=15) # y axis label\n    ax.set_title(f'{col} Distribution by Target', fontsize=18) # title label\n    ax.set_xlabel(f'{col} values', fontsize=15)\n    ","metadata":{"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Nominal Features","metadata":{}},{"cell_type":"code","source":"num_list_one = ['nom_0', 'nom_1', 'nom_2', 'nom_3', 'nom_4']\nnum_list_two = ['nom_5', 'nom_6', 'nom_7', 'nom_8', 'nom_9']","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def ploting_cat_fet(df, cols, vis_row, vis_col, palette = 'Set1'):\n    grid = gridspec.GridSpec(vis_row, vis_col) # The grid of chart\n    plt.figure(figsize=(15, 25)) # size of figure\n\n    for i, col in enumerate(train[cols]):\n        ax = plt.subplot(grid[i])\n        sns.countplot(x = col, data = train, palette= palette)\n        ax.set_ylabel('Count', fontsize=15) # y axis label\n        ax.set_title(f'{col} Distribution by Target', fontsize=10) # title label\n        ax.set_xlabel(f'{col} values', fontsize=10)","metadata":{"_kg_hide-output":false,"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ploting_cat_fet(train, num_list_one, 3,2, palette = 'ch:2.5,-.2,dark=.3')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ploting_cat_fet(train, num_list_two, 5,1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Ordibal Features","metadata":{}},{"cell_type":"code","source":"ord_list = ['ord_1', 'ord_2', 'ord_3','ord_4', 'ord_5']","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ploting_cat_fet(train, ord_list,5,1,palette='muted')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data preparation","metadata":{}},{"cell_type":"code","source":"test","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.drop(['target'], axis=1)\ntest = test.drop(['id'],axis=1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.concat([train, test])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"columns = [i for i in data.columns]\ndummies = pd.get_dummies(data,columns=columns, drop_first=True,sparse=True)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = dummies.iloc[:train.shape[0], :]\ntest = dummies.iloc[train.shape[0]:, :]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.sparse.to_coo().tocsr()\ntest = test.sparse.to_coo().tocsr()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nlr = LogisticRegression(C=0.1,solver='lbfgs')\nlr.fit(train, train_target)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = lr.predict_proba(test)[:,1]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'id':test_id,'target':prediction})\nsubmission.to_csv('submission.csv', index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}