{"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":"markdown","source":"# Introduction","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-19T04:13:44.120476Z","iopub.execute_input":"2022-03-19T04:13:44.121263Z","iopub.status.idle":"2022-03-19T04:13:44.146371Z","shell.execute_reply.started":"2022-03-19T04:13:44.121157Z","shell.execute_reply":"2022-03-19T04:13:44.145461Z"}}},{"cell_type":"markdown","source":"**Marketing** connects **best products** to **right customers**. \n\n\nIn todays digital world, **personalization** leads to **increased** customer **satisfaction** and likelihood of **repeat purchases**.\n\n\n**Recommendation System algorithms** are a set of algorithms which recommend most relevant items to users based on their preferences predicted using algorithms. It acts on **behavioural data**, such as cutomer’s previous purchases, ratings or reviews to predict their likelihood of buying a new product or service.\n**Examples** are Amazon’s  “Customers who buy this item also bought”, Netflix “shows or movies you may want to watch”.\n\n\nRecommender systems are very popular for **recommending products** such as movies, music, groceries and act as backbone for cross-selling across inductries.\n\n\n**Three** widely used algorithms used for building Recommendation System are:-\n\n1) Association Rules\n2) Collaborative Filtering\n3) Matrix Factorization\n","metadata":{}},{"cell_type":"markdown","source":"# Import Packages","metadata":{}},{"cell_type":"code","source":"\n\nimport numpy as np\nimport pandas as pd\nimport os\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\n\nfrom IPython.core.interactiveshell import InteractiveShell\nInteractiveShell.ast_node_interactivity='all'","metadata":{"execution":{"iopub.status.busy":"2022-05-06T07:10:53.534346Z","iopub.execute_input":"2022-05-06T07:10:53.534722Z","iopub.status.idle":"2022-05-06T07:10:54.620545Z","shell.execute_reply.started":"2022-05-06T07:10:53.534627Z","shell.execute_reply":"2022-05-06T07:10:54.619804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Read files\n\nThere are 4 files namely,\n1. articles.csv\n2. customers.csv\n3. transactions_train.csv\n4. sample_submission.csv","metadata":{}},{"cell_type":"code","source":"# Input data files are available in the \"../input/\" directory.\n# List all files under the input directory\n\ninput_path = '../input/h-and-m-personalized-fashion-recommendations'\n\n       \n# read files\n\nfname = 'articles.csv'\narticles_df = pd.read_csv(os.path.join(input_path , fname))\n\nfname = 'customers.csv'\ncustomers_df = pd.read_csv(os.path.join(input_path , fname))\n\n# Make sure article_id is being loading in as a string\ntransactions_train_path = '../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv'\ntransactions_train_df = pd.read_csv(transactions_train_path,  index_col=\"t_dat\", \n                                    parse_dates=True,dtype={'article_id': str})\n\n\nfname = 'sample_submission.csv'\nsample_submission_df = pd.read_csv(os.path.join(input_path , fname))","metadata":{"execution":{"iopub.status.busy":"2022-05-06T07:10:54.622724Z","iopub.execute_input":"2022-05-06T07:10:54.623058Z","iopub.status.idle":"2022-05-06T07:12:22.664047Z","shell.execute_reply.started":"2022-05-06T07:10:54.623017Z","shell.execute_reply":"2022-05-06T07:12:22.663410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"../input/h-and-m-personalized-fashion-recommendations/sample_submission.csv\n../input/h-and-m-personalized-fashion-recommendations/articles.csv\n../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv\n../input/h-and-m-personalized-fashion-recommendations/customers.csv\n../input/h-and-m-personalized-fashion-recommendations/images/057/0570177001.jpg","metadata":{}},{"cell_type":"markdown","source":"## Exploring articles","metadata":{}},{"cell_type":"code","source":"articles_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-05-06T07:12:22.665421Z","iopub.execute_input":"2022-05-06T07:12:22.666112Z","iopub.status.idle":"2022-05-06T07:12:22.674579Z","shell.execute_reply.started":"2022-05-06T07:12:22.666072Z","shell.execute_reply":"2022-05-06T07:12:22.673770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"So, there are 1,05,542 rows or observations and 25 columns or features.","metadata":{}},{"cell_type":"code","source":"articles_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-05-06T07:12:22.676280Z","iopub.execute_input":"2022-05-06T07:12:22.676560Z","iopub.status.idle":"2022-05-06T07:12:22.862827Z","shell.execute_reply.started":"2022-05-06T07:12:22.676531Z","shell.execute_reply":"2022-05-06T07:12:22.861369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This method prints information about a **DataFrame** including the **index** dtype and **column** dtypes, **non-null** values and **memory usage**.\n\nAll values are **non-null**\n\nSo now we know names of **25 features** (on left) and \n\nOf which **11** are **ints**(whole numbers) - \n\narticle_id , product_code, product_type_no, graphical_appearance_no, colour_group_code, perceived_colour_value_id, perceived_colour_master_id, department_no, index_code, index_group_no, section_no , garment_group_no                                                                                            \n\nand **14** are objects(text or string) - \n\nprod_name , product_type_name, product_group_name, graphical_appearance_name, colour_group_name, perceived_colour_value_name, perceived_colour_master_name, department_name , index_name ,index_group_name, section_name, garment_group_name, detail_desc .\n\nLet us check out their values and whether they match the types.","metadata":{}},{"cell_type":"code","source":"#pandas.DataFrame.head(n=5) - Return the first `n` rows.\narticles_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-06T07:12:22.863872Z","iopub.execute_input":"2022-05-06T07:12:22.864553Z","iopub.status.idle":"2022-05-06T07:12:22.895024Z","shell.execute_reply.started":"2022-05-06T07:12:22.864516Z","shell.execute_reply":"2022-05-06T07:12:22.894459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Whole data in article.csv is **Qualitative** in nature.  Qualitative data is categorical in nature as it assigns our observations to certain group.\n\nEach article has prod_name , product_type_name, product_group_name, graphical_appearance_name, colour_group_name, perceived_colour_value_name, perceived_colour_master_name, department_name , index_name ,index_group_name, section_name, garment_group_name,    (**12 different categories**)\n\n","metadata":{}},{"cell_type":"code","source":"product_group_name_counts = articles_df['product_group_name'].value_counts()\nproduct_group_name_counts # Series (index, value)\n\nproduct_group_name_counts.index.values # array of index of Series\nproduct_group_name_counts.values # array of value of Series","metadata":{"execution":{"iopub.status.busy":"2022-05-06T07:12:22.895915Z","iopub.execute_input":"2022-05-06T07:12:22.896544Z","iopub.status.idle":"2022-05-06T07:12:22.925554Z","shell.execute_reply.started":"2022-05-06T07:12:22.896514Z","shell.execute_reply":"2022-05-06T07:12:22.924723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index_group_name_counts = articles_df['index_group_name'].value_counts()\nindex_group_name_counts # Series (index, value)\n\nindex_group_name_counts.index.values # array of index of Series\nindex_group_name_counts.values # array of value of Series","metadata":{"execution":{"iopub.status.busy":"2022-05-06T07:12:22.926837Z","iopub.execute_input":"2022-05-06T07:12:22.927041Z","iopub.status.idle":"2022-05-06T07:12:22.956553Z","shell.execute_reply.started":"2022-05-06T07:12:22.927017Z","shell.execute_reply":"2022-05-06T07:12:22.955634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"garment_group_name_counts = articles_df['garment_group_name'].value_counts()\ngarment_group_name_counts # Series (index, value)\n\ngarment_group_name_counts.index.values # array of index of Series\ngarment_group_name_counts.values # array of value of Series","metadata":{"execution":{"iopub.status.busy":"2022-05-06T07:12:22.957720Z","iopub.execute_input":"2022-05-06T07:12:22.957960Z","iopub.status.idle":"2022-05-06T07:12:22.988801Z","shell.execute_reply.started":"2022-05-06T07:12:22.957931Z","shell.execute_reply":"2022-05-06T07:12:22.988210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"graphical_appearance_name_counts = articles_df['graphical_appearance_name'].value_counts()\ngraphical_appearance_name_counts # Series (index, value)\n\ngraphical_appearance_name_counts.index.values # array of index of Series\ngraphical_appearance_name_counts.values # array of value of Series","metadata":{"execution":{"iopub.status.busy":"2022-05-06T07:12:22.989834Z","iopub.execute_input":"2022-05-06T07:12:22.990556Z","iopub.status.idle":"2022-05-06T07:12:23.022520Z","shell.execute_reply.started":"2022-05-06T07:12:22.990521Z","shell.execute_reply":"2022-05-06T07:12:23.021774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Bar plots for single categorical variable\n\n","metadata":{}},{"cell_type":"markdown","source":"### Frequency distribution of Single categorical variable\n\nPlot the index of Series (different value that column/ feature can take) on X-axis and number of times that value occurs in dataset (frequency count) on Y-axis","metadata":{"execution":{"iopub.status.busy":"2022-02-17T08:51:32.520807Z","iopub.execute_input":"2022-02-17T08:51:32.521079Z","iopub.status.idle":"2022-02-17T08:51:32.526959Z","shell.execute_reply.started":"2022-02-17T08:51:32.521051Z","shell.execute_reply":"2022-02-17T08:51:32.52569Z"}}},{"cell_type":"code","source":"plt.style.use('seaborn-whitegrid')\n\n\n# Get the figure and the axes (or subplots)\n\nfig, (ax0, ax1, ax2) = plt.subplots(nrows=1, ncols=3, figsize=(15, 4))\n\n# Thus we have to give more margin:\nplt.subplots_adjust(top=0.7)\nax0.xaxis.set_tick_params(rotation=90)\nax1.xaxis.set_tick_params(rotation=90)\nax2.xaxis.set_tick_params(rotation=90)\n\nax0.bar(product_group_name_counts.index.values, product_group_name_counts.values, width=0.5, align='center')\nax0.set(title = 'product_group_name_counts', xlabel='product_group_name' , ylabel = 'Frequency')\n\nax1.bar(index_group_name_counts.index.values, index_group_name_counts.values, width=0.5, align='center')\nax1.set(title = 'index_group_name_counts', xlabel='index_group_name' , ylabel = 'Frequency')\n\nax2.bar(garment_group_name_counts.index.values, garment_group_name_counts.values, width=0.5, align='center')\nax2.set(title = 'garment_group_name_counts', xlabel='garment_group_name' , ylabel = 'Frequency')\n\n# Title the figure\nfig.suptitle('Frequency Distribution', fontsize=14, fontweight='bold');","metadata":{"execution":{"iopub.status.busy":"2022-05-06T07:12:23.025345Z","iopub.execute_input":"2022-05-06T07:12:23.025813Z","iopub.status.idle":"2022-05-06T07:12:23.901668Z","shell.execute_reply.started":"2022-05-06T07:12:23.025782Z","shell.execute_reply":"2022-05-06T07:12:23.890447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles_df.nunique()","metadata":{"execution":{"iopub.status.busy":"2022-05-06T07:12:23.903350Z","iopub.execute_input":"2022-05-06T07:12:23.903959Z","iopub.status.idle":"2022-05-06T07:12:24.081424Z","shell.execute_reply.started":"2022-05-06T07:12:23.903906Z","shell.execute_reply":"2022-05-06T07:12:24.080481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"So we have as many unique article_ids as we have total no of rows in articles_df (105,542)","metadata":{}},{"cell_type":"markdown","source":"## Exploring customers","metadata":{}},{"cell_type":"code","source":"customers_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-05-06T07:12:24.082482Z","iopub.execute_input":"2022-05-06T07:12:24.082691Z","iopub.status.idle":"2022-05-06T07:12:24.088819Z","shell.execute_reply.started":"2022-05-06T07:12:24.082667Z","shell.execute_reply":"2022-05-06T07:12:24.088021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"So, there are **13,71,980** rows or observations and **7** columns or features.\n","metadata":{}},{"cell_type":"code","source":"customers_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-05-06T07:12:24.090044Z","iopub.execute_input":"2022-05-06T07:12:24.090373Z","iopub.status.idle":"2022-05-06T07:12:24.711829Z","shell.execute_reply.started":"2022-05-06T07:12:24.090343Z","shell.execute_reply":"2022-05-06T07:12:24.710940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This method prints information about a** DataFrame** including the **index dtype** and **column dtypes**, **non-null values** and **memory usage**.\n\nAll values are **non-null**\n\nSo now we know names of **7** features (on left) and\n\ncustomer_id, FN, Active, club_member_status, fashion_news_frequency, age and postal_code   \n\nof which **3 are float64** (FN, Active, Age)\n\nand rest are object( text)","metadata":{}},{"cell_type":"code","source":"customers_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-06T07:12:24.714115Z","iopub.execute_input":"2022-05-06T07:12:24.714448Z","iopub.status.idle":"2022-05-06T07:12:24.729670Z","shell.execute_reply.started":"2022-05-06T07:12:24.714404Z","shell.execute_reply":"2022-05-06T07:12:24.728766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers_df.nunique()","metadata":{"execution":{"iopub.status.busy":"2022-05-06T07:12:24.730631Z","iopub.execute_input":"2022-05-06T07:12:24.730829Z","iopub.status.idle":"2022-05-06T07:12:26.393560Z","shell.execute_reply.started":"2022-05-06T07:12:24.730804Z","shell.execute_reply":"2022-05-06T07:12:26.392610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"There are 1371,980 unique customers.\n\n\nFN and Active take 1 unique value.(qualitative data)\n\nclub_member_status  has 3 unique values and fashion_news_frequency  has 4 unique values.(qualitative data)\n\nage has 84 unique values. (quanitative data)\n\npostal_code has different unique 352899.\n\nLet us check out each column's values.","metadata":{}},{"cell_type":"code","source":"customers_df['FN'].unique()\n\ncustomers_df['FN'].unique().size","metadata":{"execution":{"iopub.status.busy":"2022-05-06T07:12:26.394672Z","iopub.execute_input":"2022-05-06T07:12:26.394948Z","iopub.status.idle":"2022-05-06T07:12:26.433716Z","shell.execute_reply.started":"2022-05-06T07:12:26.394919Z","shell.execute_reply":"2022-05-06T07:12:26.433029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers_df['Active'].unique()\n\ncustomers_df['Active'].unique().size","metadata":{"execution":{"iopub.status.busy":"2022-05-06T07:12:26.434833Z","iopub.execute_input":"2022-05-06T07:12:26.435516Z","iopub.status.idle":"2022-05-06T07:12:26.473048Z","shell.execute_reply.started":"2022-05-06T07:12:26.435478Z","shell.execute_reply":"2022-05-06T07:12:26.472180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers_df['club_member_status'].unique()\n\ncustomers_df['club_member_status'].unique().size","metadata":{"execution":{"iopub.status.busy":"2022-05-06T07:12:26.474203Z","iopub.execute_input":"2022-05-06T07:12:26.474936Z","iopub.status.idle":"2022-05-06T07:12:26.774186Z","shell.execute_reply.started":"2022-05-06T07:12:26.474901Z","shell.execute_reply":"2022-05-06T07:12:26.773364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers_df['fashion_news_frequency'].unique()\n\ncustomers_df['fashion_news_frequency'].unique().size","metadata":{"execution":{"iopub.status.busy":"2022-05-06T07:12:26.775322Z","iopub.execute_input":"2022-05-06T07:12:26.775525Z","iopub.status.idle":"2022-05-06T07:12:27.080468Z","shell.execute_reply.started":"2022-05-06T07:12:26.775500Z","shell.execute_reply":"2022-05-06T07:12:27.079565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers_df['age'].unique()\n\ncustomers_df['age'].unique().size","metadata":{"execution":{"iopub.status.busy":"2022-05-06T07:12:27.082192Z","iopub.execute_input":"2022-05-06T07:12:27.082566Z","iopub.status.idle":"2022-05-06T07:12:27.123473Z","shell.execute_reply.started":"2022-05-06T07:12:27.082523Z","shell.execute_reply":"2022-05-06T07:12:27.122559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers_df['postal_code'].unique()\n\ncustomers_df['postal_code'].unique().size","metadata":{"execution":{"iopub.status.busy":"2022-05-06T07:12:27.125060Z","iopub.execute_input":"2022-05-06T07:12:27.126305Z","iopub.status.idle":"2022-05-06T07:12:28.226212Z","shell.execute_reply.started":"2022-05-06T07:12:27.126255Z","shell.execute_reply":"2022-05-06T07:12:28.225272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers_active_counts = customers_df['Active'].value_counts()\ncustomers_active_counts # Series (index, value)\n\ncustomers_active_counts.index.values # array of index of Series\ncustomers_active_counts.values # array of value of Series","metadata":{"execution":{"iopub.status.busy":"2022-05-06T07:12:28.227779Z","iopub.execute_input":"2022-05-06T07:12:28.228005Z","iopub.status.idle":"2022-05-06T07:12:28.262161Z","shell.execute_reply.started":"2022-05-06T07:12:28.227977Z","shell.execute_reply":"2022-05-06T07:12:28.261066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Out of total 1371,980 customers , \n\n464,404 are active(around 1/3rd)\n\nrest are nan.","metadata":{}},{"cell_type":"code","source":"club_member_status_counts = customers_df['club_member_status'].value_counts()\nclub_member_status_counts # Series (index, value)\n\nclub_member_status_counts.index.values # array of index of Series\nclub_member_status_counts.values # array of value of Series","metadata":{"execution":{"iopub.status.busy":"2022-05-06T07:12:28.263600Z","iopub.execute_input":"2022-05-06T07:12:28.263820Z","iopub.status.idle":"2022-05-06T07:12:28.485948Z","shell.execute_reply.started":"2022-05-06T07:12:28.263794Z","shell.execute_reply":"2022-05-06T07:12:28.485019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Out of total 1371,980 customers ,\n\n1272,491 are ACTIVE club members,( a majority are Active club members) \n\n92, 960 have pre-create status , \n\n467 have left club and \n\nrest are nan.","metadata":{}},{"cell_type":"code","source":"fashion_news_frequency_counts = customers_df['fashion_news_frequency'].value_counts()\nfashion_news_frequency_counts # Series (index, value)\n\nfashion_news_frequency_counts.index.values # array of index of Series\nfashion_news_frequency_counts.values # array of value of Series","metadata":{"execution":{"iopub.status.busy":"2022-05-06T07:12:28.489134Z","iopub.execute_input":"2022-05-06T07:12:28.489368Z","iopub.status.idle":"2022-05-06T07:12:28.711442Z","shell.execute_reply.started":"2022-05-06T07:12:28.489339Z","shell.execute_reply":"2022-05-06T07:12:28.710599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Out of total 1371,980 customers ,\n\n477,416 (Regularly) and 842(Monthly) have subscribed to Fashion News Agency \n\n877,711 (NONE), None (2)\n\n1/3 rd have subscribed and 2/3 rd have not subscribed.\n\nUnsubscribed (877,711) is double(477,416) the no of Subscribed.\n","metadata":{}},{"cell_type":"code","source":"plt.style.use('seaborn-whitegrid')\n\n\n# Get the figure and the axes (or subplots)\n\nfig, (ax0, ax1, ax2) = plt.subplots(nrows=1, ncols=3, figsize=(15, 4))\n\n\nax0.bar(customers_active_counts.index.values, customers_active_counts.values, width=0.5, align='center')\nax0.set(title = 'customers_active_counts', xlabel='customers_active' , ylabel = 'Frequency')\n\nax1.bar(club_member_status_counts.index.values, club_member_status_counts.values, width=0.5, align='center')\nax1.set(title = 'club_member_status_counts', xlabel='club_member_status' , ylabel = 'Frequency')\n\nax2.bar(fashion_news_frequency_counts.index.values, fashion_news_frequency_counts.values, width=0.5, align='center')\nax2.set(title = 'fashion_news_frequency_counts', xlabel='fashion_news_frequency' , ylabel = 'Frequency')\n\n# Title the figure\nfig.suptitle('Frequency Distribution', fontsize=14, fontweight='bold');","metadata":{"execution":{"iopub.status.busy":"2022-05-06T07:12:28.713004Z","iopub.execute_input":"2022-05-06T07:12:28.713356Z","iopub.status.idle":"2022-05-06T07:12:29.120344Z","shell.execute_reply.started":"2022-05-06T07:12:28.713314Z","shell.execute_reply":"2022-05-06T07:12:29.119530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Exploring age\n\nIt is the only quantitative data.\n","metadata":{}},{"cell_type":"code","source":"#customers_df[\"age\"].describe()","metadata":{"execution":{"iopub.status.busy":"2022-05-06T07:12:29.121681Z","iopub.execute_input":"2022-05-06T07:12:29.122231Z","iopub.status.idle":"2022-05-06T07:12:29.125771Z","shell.execute_reply.started":"2022-05-06T07:12:29.122195Z","shell.execute_reply":"2022-05-06T07:12:29.124823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Histogram \nsns.distplot(customers_df['age'], kde=False)","metadata":{"execution":{"iopub.status.busy":"2022-05-06T07:12:29.126909Z","iopub.execute_input":"2022-05-06T07:12:29.127149Z","iopub.status.idle":"2022-05-06T07:12:29.453652Z","shell.execute_reply.started":"2022-05-06T07:12:29.127102Z","shell.execute_reply":"2022-05-06T07:12:29.452578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# KDE plot (Smooth Histogram )\nsns.kdeplot(customers_df['age'], shade=True)","metadata":{"execution":{"iopub.status.busy":"2022-05-06T07:12:29.459254Z","iopub.execute_input":"2022-05-06T07:12:29.459536Z","iopub.status.idle":"2022-05-06T07:12:34.799163Z","shell.execute_reply.started":"2022-05-06T07:12:29.459504Z","shell.execute_reply":"2022-05-06T07:12:34.798314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make boxplot for one group only\nsns.violinplot(y=customers_df[\"age\"])","metadata":{"execution":{"iopub.status.busy":"2022-05-06T07:12:34.800526Z","iopub.execute_input":"2022-05-06T07:12:34.800862Z","iopub.status.idle":"2022-05-06T07:12:37.688323Z","shell.execute_reply.started":"2022-05-06T07:12:34.800820Z","shell.execute_reply":"2022-05-06T07:12:37.687476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Maximum purchases are for customers aged 20-30 and another peak for those aged 50.","metadata":{}},{"cell_type":"markdown","source":"### Exploring transactions_train","metadata":{}},{"cell_type":"code","source":"transactions_train_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-05-06T07:12:37.689497Z","iopub.execute_input":"2022-05-06T07:12:37.690238Z","iopub.status.idle":"2022-05-06T07:12:37.697334Z","shell.execute_reply.started":"2022-05-06T07:12:37.690189Z","shell.execute_reply":"2022-05-06T07:12:37.696341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"There are 31 788 324 transactions.","metadata":{}},{"cell_type":"code","source":"transactions_train_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-05-06T07:12:37.698536Z","iopub.execute_input":"2022-05-06T07:12:37.698772Z","iopub.status.idle":"2022-05-06T07:12:38.238517Z","shell.execute_reply.started":"2022-05-06T07:12:37.698735Z","shell.execute_reply":"2022-05-06T07:12:38.237588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-06T07:12:38.239719Z","iopub.execute_input":"2022-05-06T07:12:38.240045Z","iopub.status.idle":"2022-05-06T07:12:38.252204Z","shell.execute_reply.started":"2022-05-06T07:12:38.240010Z","shell.execute_reply":"2022-05-06T07:12:38.251308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_train_df.tail()","metadata":{"execution":{"iopub.status.busy":"2022-05-06T07:12:38.253956Z","iopub.execute_input":"2022-05-06T07:12:38.254318Z","iopub.status.idle":"2022-05-06T07:12:38.277455Z","shell.execute_reply.started":"2022-05-06T07:12:38.254273Z","shell.execute_reply":"2022-05-06T07:12:38.276557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sales_channel_id_counts = transactions_train_df['sales_channel_id'].value_counts()\nsales_channel_id_counts # Series (index, value)\n\nsales_channel_id_counts.index.values # array of index of Series\nsales_channel_id_counts.values # array of value of Series","metadata":{"execution":{"iopub.status.busy":"2022-05-06T07:12:38.278637Z","iopub.execute_input":"2022-05-06T07:12:38.279159Z","iopub.status.idle":"2022-05-06T07:12:38.457171Z","shell.execute_reply.started":"2022-05-06T07:12:38.279102Z","shell.execute_reply":"2022-05-06T07:12:38.456177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.style.use('seaborn-whitegrid')\n\n# Get the figure and the axes (or subplots)\n\nfig, (ax0) = plt.subplots(nrows=1, ncols=1, figsize=(8, 4))\n\nax0.bar(sales_channel_id_counts.index.values, sales_channel_id_counts.values, width=0.5, align='center')\nax0.set(title = 'sales_channel_id_counts', xlabel='sales_channel_id' , ylabel = 'Frequency')\n\n\n# Title the figure\nfig.suptitle('Frequency Distribution', fontsize=14, fontweight='bold');","metadata":{"execution":{"iopub.status.busy":"2022-05-06T07:12:38.458559Z","iopub.execute_input":"2022-05-06T07:12:38.458971Z","iopub.status.idle":"2022-05-06T07:12:38.687282Z","shell.execute_reply.started":"2022-05-06T07:12:38.458925Z","shell.execute_reply":"2022-05-06T07:12:38.686221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list(transactions_train_df.columns)","metadata":{"execution":{"iopub.status.busy":"2022-05-06T07:12:38.688441Z","iopub.execute_input":"2022-05-06T07:12:38.688687Z","iopub.status.idle":"2022-05-06T07:12:38.696978Z","shell.execute_reply.started":"2022-05-06T07:12:38.688656Z","shell.execute_reply":"2022-05-06T07:12:38.696042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_train_df_from26Aug2020=transactions_train_df.tail(1000000) #Last 1 million records as data is huge\ntransactions_train_df_from26Aug2020.head()\n\n","metadata":{"execution":{"iopub.status.busy":"2022-05-06T07:12:38.698379Z","iopub.execute_input":"2022-05-06T07:12:38.698598Z","iopub.status.idle":"2022-05-06T07:12:38.718614Z","shell.execute_reply.started":"2022-05-06T07:12:38.698563Z","shell.execute_reply":"2022-05-06T07:12:38.717522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_train_df_from26Aug2020.tail()","metadata":{"execution":{"iopub.status.busy":"2022-05-06T07:12:38.720219Z","iopub.execute_input":"2022-05-06T07:12:38.720447Z","iopub.status.idle":"2022-05-06T07:12:38.733833Z","shell.execute_reply.started":"2022-05-06T07:12:38.720420Z","shell.execute_reply":"2022-05-06T07:12:38.732711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_train_df_from26Aug2020.shape\ntransactions_train_df_from26Aug2020.nunique()","metadata":{"execution":{"iopub.status.busy":"2022-05-06T07:12:38.735099Z","iopub.execute_input":"2022-05-06T07:12:38.735361Z","iopub.status.idle":"2022-05-06T07:12:39.126530Z","shell.execute_reply.started":"2022-05-06T07:12:38.735331Z","shell.execute_reply":"2022-05-06T07:12:39.125546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"There are 226 120 unique customers and 28001 unique articles starting from 26 Aug 2020 to 22 Sep 2020","metadata":{}},{"cell_type":"code","source":"# Set the width and height of the figure\nplt.figure(figsize=(14,6))\n\n# Add title\nplt.title(\"Price\")\n\n# Line chart \nsns.lineplot(data=transactions_train_df_from26Aug2020['price'], label=\"Price\")\n\n# Add label for horizontal axis\nplt.xlabel(\"Date\")","metadata":{"execution":{"iopub.status.busy":"2022-05-06T07:13:33.586261Z","iopub.execute_input":"2022-05-06T07:13:33.587659Z","iopub.status.idle":"2022-05-06T07:13:46.237053Z","shell.execute_reply.started":"2022-05-06T07:13:33.587589Z","shell.execute_reply":"2022-05-06T07:13:46.236095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# References\n\n\nMachine Learning Using Python","metadata":{}}]}