{"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 the files in the input directory\nimport pickle\nimport os\nprint(os.listdir(\"../input/feature-engineering/\"))\nprint(os.listdir(\"../input/elo-merchant-category-recommendation/\"))\nimport datetime\nimport time\nimport sys\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"with open('../input/feature-engineering/new_sum.pickle', 'rb') as f:\n    new_sum = pickle.load(f)\nwith open('../input/feature-engineering/his_sum.pickle', 'rb') as f:\n    his_sum = pickle.load(f)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5553cb79dddea61a614c73a9ac280dcad6f70536"},"cell_type":"code","source":"train = pd.read_csv( '../input/elo-merchant-category-recommendation/train.csv',parse_dates =[\"first_active_month\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0ed96594be4a22581264708528043ef4c9ef139f"},"cell_type":"code","source":"new_sum.head()\nnew_sum.columns.values[1] = 'new_card_id_size'\nnew_sum.columns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1a332c4077f502e9192602feaba833dd78d91d8d"},"cell_type":"code","source":"his_sum.head()\nhis_sum.columns.values[1] = 'his_card_id_size'\nhis_sum.columns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"703802c9087b54567aeaeaac903ac4d5ca6e0495"},"cell_type":"code","source":"train_his=train.merge(his_sum, how='right', on=\"card_id\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1f61a166c00db9676b1afcfe1280a64cf175889e"},"cell_type":"code","source":"train_his.head()\ntrain_his.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c41945b70ca85883180e9db26198d6270aa7e377"},"cell_type":"code","source":"train_his_new=train_his.merge(new_sum, how='right', on=\"card_id\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4cb255ed8ef295eaad091933450101824b770f66"},"cell_type":"code","source":"train_his_new.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"606e13a3fffdd44c57cbf893eae127bfda7fb451"},"cell_type":"code","source":"train_his_new.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1e5dc7a3ba8e22ed480c4beab2a70403f963c2d1"},"cell_type":"code","source":"train_his_new.columns.values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7618769e920fbd6e9ad7570c9fc328976b61b832"},"cell_type":"code","source":"train_his_new['first_active_month'] = pd.to_datetime(train_his_new['first_active_month'])\ntrain_his_new['first_active_years'] = train_his_new['first_active_month'].dt.year\ntrain_his_new['first_active_months'] = train_his_new['first_active_month'].dt.month\ntrain_his_new['howlong'] = (datetime.date(2018,2,1) - train_his_new['first_active_month'].dt.date).dt.days\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fa1a4acb5b34de3a83a6f79af9a92b5b0c1c5776"},"cell_type":"code","source":"train_his_new.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"168c2436fcfae0ed645bed7d51711c9d74526ecd"},"cell_type":"code","source":"train_matrix = train_his_new.drop(['card_id','first_active_month','target'], axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cc52ffa0004ebca348d68f368a3dd0a0819836a1"},"cell_type":"code","source":"train_matrix.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"36f511994babafe8148a124d646060a11246911a"},"cell_type":"code","source":"target = train_his_new['target']","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"879e9f14b35bf2b1c4fcc3b5e39c3ccf92018338"},"cell_type":"markdown","source":"# (?)Feature Selection Using Random forest\nhttps://towardsdatascience.com/feature-selection-using-random-forest-26d7b747597f"},{"metadata":{"trusted":true,"_uuid":"e4c92fe33abdbecc0be8cc00d9ba3d3e5eee6159"},"cell_type":"code","source":"# from sklearn.ensemble import RandomForestRegressor\n# from sklearn.feature_selection import SelectFromModel","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4c442cdb8d8e67a46e4b02a6b34ab37bd55394ab"},"cell_type":"code","source":"# sel = SelectFromModel(RandomForestRegressor(n_estimators = 100))\n# sel.fit(train_matrix, target)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cb1cd22b1c794b149a681de37e1e61a2f01bfba9"},"cell_type":"code","source":"# train_matrix[train_matrix.isnull().any(axis=1)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0750338246662094cf852e1e3b3a975bbaf96d17"},"cell_type":"markdown","source":"# A Feature Selection Tool for Machine Learning in Python\nhttps://towardsdatascience.com/a-feature-selection-tool-for-machine-learning-in-python-b64dd23710f0"},{"metadata":{"trusted":true,"_uuid":"da21520e0d44383ba845c44c9ac5e010f327ac34"},"cell_type":"code","source":"import sys\nsys.path.append('../input/feature-selector/')\nfrom feature_selector import FeatureSelector\n# Features are in train and labels are in train_labels\nfs = FeatureSelector(data = train_matrix, labels = target)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d4c24ca9592f64fd818ca2c0fbf641c86c5123ea"},"cell_type":"code","source":"fs.identify_missing(missing_threshold = 0.6)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"623d7505a8204aa6653b71e14231da4c69306083"},"cell_type":"code","source":"fs.missing_stats.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fd602181b93f9984b8c3988194cd6e00edd1ec08"},"cell_type":"code","source":"fs.plot_missing()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ec528e982b241ef5701600e02fa5d4c6b81aa02e"},"cell_type":"code","source":"fs.identify_collinear(correlation_threshold = 0.98)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c155562c04df1a8d6e7def405a961c32d1bc3f7c"},"cell_type":"code","source":"fs.plot_collinear()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1dde1887284a170d9af1f1dbe7fc59f545b5b52b"},"cell_type":"code","source":"# list of collinear features to remove\ncollinear_features = fs.ops['collinear']\n# dataframe of collinear features\nfs.record_collinear.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e8b2cf6cf15b085a4d82fe22bd1b18ed243c3c99"},"cell_type":"code","source":"# Pass in the appropriate parameters\nfs.identify_zero_importance(task = 'regression', \n                            eval_metric = 'auc', \n                            n_iterations = 10, \n                             early_stopping = True)\n# list of zero importance features\nzero_importance_features = fs.ops['zero_importance']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"066d00f82b65f9baa370b1c1cfa832b007256b6f"},"cell_type":"code","source":"# plot the feature importances\nfs.plot_feature_importances(threshold = 0.99, plot_n = 12)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0574efc0e8ea2200d5bd762727323b257159dc6c"},"cell_type":"code","source":"fs.identify_low_importance(cumulative_importance = 0.99)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e32087dbcd18bd389e74f558a870fa0f9c7c6f1c"},"cell_type":"code","source":"fs.identify_single_unique()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e10ea78faa89a2d285eaa1efaa99faa3eb9007a2"},"cell_type":"code","source":"# Remove the features from all methods (returns a df)\ntrain_removed = fs.remove(methods = 'all')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d7843c7cf07c5db6bf65a01eee3491648bddd8b8"},"cell_type":"code","source":"train_removed.shape\nwith open('train_removed.pickle', 'wb') as f:\n    pickle.dump(train_removed, f)\nwith open('target.pickle', 'wb') as f:\n    pickle.dump(target, f)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5dc71705ed74639e583480c007a21f3e9a657dda"},"cell_type":"code","source":"#train_removed[train_removed.isnull().any(axis=1)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bbbaae3d49ab2167bc4bb82df6172f8803d2ab51"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}