{"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","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\n# !pip install missingno\nimport missingno as msno\nfrom datetime import date\nfrom sklearn.neighbors import LocalOutlierFactor\nfrom sklearn.preprocessing import MinMaxScaler, LabelEncoder, StandardScaler, RobustScaler\n\nfrom sklearn.preprocessing import RobustScaler\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.metrics import accuracy_score, roc_auc_score, confusion_matrix, classification_report, plot_roc_curve\nfrom sklearn.model_selection import train_test_split, cross_validate\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.metrics import mean_squared_error, mean_absolute_error\nfrom sklearn.model_selection import train_test_split, cross_val_score\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.set_option('display.max_columns', None)\npd.set_option('display.max_rows', None)\npd.set_option('display.float_format', lambda x: '%.3f' % x)\npd.set_option('display.width', 500)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_identity = pd.read_csv('../input/ieee-fraud-detection/train_identity.csv')\ntrain_transaction = pd.read_csv('../input/ieee-fraud-detection/train_transaction.csv')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_identity.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_transaction.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_transaction.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_identity.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The data is broken into two files identity and transaction, which are joined by TransactionID. Not all transactions have corresponding identity information.","metadata":{}},{"cell_type":"code","source":"df_ = train_transaction.merge(train_identity, on = 'TransactionID')\ndf= df_.copy()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Quick glance at a dataset\ndef check_df(data, x=5):\n    print('################################# shape ##########################')\n    print(data.shape )\n    print('################################# type ##########################')\n    print(data.dtypes)\n    print('################################# head ##########################')\n    print(data.head(x))\n    print('################################# tail ##########################')\n    print(data.tail(x))\n    print('################################# null ##########################')\n    print(data.isnull().sum().sort_values(ascending=False))\n    print('################################# quantiles #####################')\n    print(data.describe([0, 0.05, 0.5, 0.95, 0.99, 1]).T)\ncheck_df(df)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def grab_col_names (dataframe, categorical = 10, cardinal =20):\n\n    # categoricals\n    cat_cols = [col for col in dataframe.columns if dataframe[col].dtype == 'O']\n    #  [col for col in df.columns if str(df[col].dtypes) in [\"category\",\"object\",\"bool\"]]\n    cat_but_car = [col for col in dataframe.columns if dataframe[col].dtype == 'O' and\n                   dataframe[col].nunique() > cardinal]\n    num_but_cat = [col for col in dataframe.columns if dataframe[col].dtype != 'O' and\n                   dataframe[col].nunique() < categorical]\n    cat_cols += num_but_cat\n    cat_cols = [col for col in cat_cols if col not in cat_but_car]\n\n    # numericals\n\n    num_cols = [col for col in dataframe.columns if dataframe[col].dtype != 'O' and\n                col not in num_but_cat]\n\n    print(f\"Observations: {dataframe.shape[0]}\")\n    print(f\"Variables: {dataframe.shape[1]}\")\n    print(f'cat_cols: {len(cat_cols)}')\n    print(f'num_cols: {len(num_cols)}')\n    print(f'cat_but_car: {len(cat_but_car)}')\n    print(f'num_but_cat: {len(num_but_cat)}')\n    return cat_cols, num_cols, cat_but_car\n\n\ncat_cols, num_cols, cat_but_car = grab_col_names (df, categorical = 10, cardinal =20)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_cols","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}