{"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\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nimport os\n# using python csv library for writing results\nimport csv\n# loading data in windows environment\nimport pathlib\nimport re\nprint (tf.__version__)\nfrom kaggle_datasets import KaggleDatasets\nAUTO = tf.data.experimental.AUTOTUNE\n# Input data files are available in the read-only \"../input/\" directory\n\n# Input Data Set\nDATA = [{\n'owner_name': 'john',\n'year': 2018,\n'sale_price': 200000,\n'business_type': 'clothing',\n'average_yearly_revenue': 60000\n}, {\n'owner_name': 'kelly',\n'year': 2017,\n'sale_price': 300000,\n'business_type': 'clothing',\n'average_yearly_revenue': 80000\n}, {\n'owner_name': 'jason',\n'year': 2017,\n'sale_price': 250000,\n'business_type': 'food',\n'average_yearly_revenue': 80000\n}, {\n'owner_name': 'ryan',\n'year': 2018,\n'sale_price': 225000,\n'business_type': 'clothing',\n'average_yearly_revenue': 90000\n}, {\n'owner_name': 'mariah',\n'year': 2019,\n'sale_price': 400000,\n'business_type': 'food',\n'average_yearly_revenue': 120000\n}, {\n'owner_name': 'simon',\n'year': 2019,\n'sale_price': 1000000,\n'business_type': 'clothing',\n'average_yearly_revenue': 200000\n}, {\n'owner_name': 'harry',\n'year': 2018,\n'sale_price': 100000,\n'business_type': 'clothing',\n'average_yearly_revenue': 40000\n}, {\n'owner_name': 'ella',\n'year': 2018,\n'sale_price': 75000,\n'business_type': 'food',\n'average_yearly_revenue': 32000\n}, {\n'owner_name': 'kate',\n'year': 2017,\n'sale_price': 125000,\n'business_type': 'clothing',\n'average_yearly_revenue': 40000\n}, {\n'owner_name': 'peter',\n'year': 2017,\n'sale_price': 175000,\n'business_type': 'food',\n'average_yearly_revenue': 55000\n}]\n\n# Function take two parameters\n# Parameter 1 is a list of Dictionary\n# Parameter 2 is one of the key Value from the Dictionary\n\n# Function No.1 \ndef Transaction_Median(L, B_TYPE):\n    # Taking the average_yearly_revenue Value\n    # Storing in the Value list V\n    V=[]\n\n    # Scaning through the list.\n    # Storing the value with Matching Key\n    for item in L:\n        if (item['business_type']==B_TYPE):\n           V.append(item['average_yearly_revenue'])\n\n    # Calculating the Median Value\n    Median = int(np.median(V))\n    # Displaying the output\n    print (\"Busines_Type : {}, Median Value : {}\".format(B_TYPE, Median))\n    return Median\n\nTransaction_Median(DATA, 'food')\n\n# Function No.2\n# Calculating Mean Difference from the Data List\ndef MeanDiff(L):\n    # Storing the different years from Data\n    YEAR=[]\n    \n    # The calculated difference value associate with the year\n    VEC=[]\n    \n    # Scanning through the Data list \n    for item in L:\n        # Finding the different years\n        # Storing the different years\n        if item['year'] not in YEAR:\n            YEAR.append(item['year'])\n    # Sorting the years\n    YEAR = np.sort(YEAR, axis=0)\n    \n    # For each years\n    for Y in YEAR:\n        # Counting the number of the years\n        COUNTER = 0\n        # Storing the difference for each year\n        DIFF = 0\n        # Scanning through the Original Data\n        for item in L:\n            # For each of the years, calculating the difference\n            # Counter counts number of the years\n            # For each of year, the difference will be summed\n            if (item['year'] == Y):\n                COUNTER = COUNTER + 1\n                DIFF =DIFF+item['sale_price']-item['average_yearly_revenue']\n        # Calcuating the mean of the difference for that year\n        AVG = DIFF/COUNTER\n        # Making a dictionary of the year and the mean\n        DICT = {'year':Y, 'Mean_Difference':AVG}\n        # Storing the year and mean tuple in VEC\n        VEC.append(DICT)\n        \n    # Updating the original data list as the mean for each year\n    for item in VEC:\n        for T in L:\n            if item['year'] == T['year']:\n                T['Mean_Difference'] = item['Mean_Difference']\n                \n    # Display the updated data \n    print (L)        \n    # Function Return\n    return YEAR\n\nY = MeanDiff(DATA)\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}