{"cells":[{"metadata":{"_uuid":"77fddbc013b48d7a92b470ee7c1fa2bdfcd32df6"},"cell_type":"markdown","source":"Objective\n- Objective of the analysis is to understand GA Customer Revenue Prediction Data\n- Build a rich visualization on various Geography parameters\n- Treat data in special form like JSON to wide tabular one\n- An introductory lesson for Plotly learners\n\nThis kernel is inspired by many of SRK's(an excellent teacher) work."},{"metadata":{"trusted":true,"_uuid":"9cf4400711774a06fa2899d0aafc0d4e3b099e14","_kg_hide-input":true,"_kg_hide-output":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\nDATA_DIR = \"../input\"\n\n\nimport os\nprint(os.listdir(DATA_DIR))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ac41d1a6f40c089dafb1d5c5700c73b622784b0c","_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"import json\nfrom pandas.io.json import json_normalize","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2f39275182478403a956b1e420740be7724db93c"},"cell_type":"markdown","source":"### Load and Explore\n- Load the data, explore the variables, number of observations etc\n- Identify data type of all variables\n- De norm it if necessary\n\n### Columns: Data Types\n- There is a date column that can be parsed while loading\n- Four of the columns looks like JSON Input, they are\n    - device\n    - geoNetwork\n    - totals\n    - trafficSource\n- Its better to make the JSON data into wider format\n- Except visitNumber, all other columns are categorical"},{"metadata":{"trusted":true,"_uuid":"c4741022df0518ce59781089ae708e0324ebda51","_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"%%time\nJSON_COLUMNS = ['device', 'geoNetwork', 'totals', 'trafficSource']\ntrain_df = pd.read_csv(DATA_DIR + \"/train_v2.csv\", dtype={\"fullVisitorId\": \"str\"}, \n    converters={column: json.loads for column in JSON_COLUMNS}, nrows=500000)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6d79dfaa07ff8a75a59d325fdb43e522834bb220"},"cell_type":"code","source":"import json\nfrom pandas.io.json import json_normalize\ndef json_to_dataframe(data, column_name):\n    json_as_df = json_normalize(data[column_name])\n    json_as_df.columns = [f\"{column_name}.{subcolumn}\" for subcolumn in json_as_df.columns]\n    data = data.drop(column_name, axis=1).merge(json_as_df, right_index=True, left_index=True)\n    return data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3f72adacd6070b49ac3f7cb443c3e38c5de8b88d"},"cell_type":"code","source":"%%time\ntrain_df = json_to_dataframe(train_df, \"device\")\ntrain_df = json_to_dataframe(train_df, \"geoNetwork\")\ntrain_df = json_to_dataframe(train_df, \"totals\")\ntrain_df = json_to_dataframe(train_df, \"trafficSource\")\ntrain_df.shape","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ef716ff968a77a0ecc631bd1a0c88a9af8422373"},"cell_type":"markdown","source":"### Get the Data Type again."},{"metadata":{"trusted":true,"_uuid":"99738ac5d05f4688ccd749be0d9a7f25fabceff0"},"cell_type":"code","source":"dtype_df = train_df.dtypes.reset_index()\ndtype_df.columns = [\"Count\", \"Column Type\"]\ndtype_df.groupby(\"Column Type\").aggregate(\"count\").reset_index()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c429458de7636ce4cdd04e2a07a2401b50a1cee2","_kg_hide-input":true},"cell_type":"code","source":"dtype_df.head(55)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"108f63f9416ed51545d7444bd2ab7b877725d567","_kg_hide-input":true},"cell_type":"code","source":"train_df.head(2)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0f776c654e57b5010d1cec102ed00750ac0c8ffc"},"cell_type":"markdown","source":"### Missing Values"},{"metadata":{"trusted":true,"_uuid":"8193c658812f39dd3375cb85b8690dfe6f5d3f4f","_kg_hide-input":true},"cell_type":"code","source":"missing_values_df = train_df.isnull().sum(axis=0).reset_index()\nmissing_values_df.columns = [\"Column_Name\", \"Missing_Count\"]\nmissing_values_df = missing_values_df.loc[missing_values_df[\"Missing_Count\"] > 0]\nprint(missing_values_df.shape)\nmissing_values_df","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d26892d2085ccf954f14ebebbb4ea7bf20c502b8"},"cell_type":"markdown","source":"## Geo Distribution"},{"metadata":{"trusted":true,"_uuid":"216b85dc63e17a2d407200aa7b72b244ef15b129","_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"import plotly.offline as py\npy.init_notebook_mode(connected=True)\nimport plotly.graph_objs as go\nimport plotly.tools as tls\n\npd.options.mode.chained_assignment = None\npd.options.display.max_columns = 9999\npd.options.display.float_format = '{:20,.2f}'.format","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a0bf3a9acc67ef51104d794ebf13fc465e3fccb2"},"cell_type":"markdown","source":"### Continent Distribution"},{"metadata":{"trusted":true,"_uuid":"f71eab529b2b979cb8ee2b1f601819c69916c3cc","_kg_hide-input":true},"cell_type":"code","source":"continent_series = train_df[\"geoNetwork.continent\"].value_counts()\nlabels = (np.array(continent_series.index))\nsizes = (np.array((continent_series / continent_series.sum())*100))\n\ntrace = go.Pie(labels=labels, values=sizes)\nlayout = go.Layout(title=\"Continent Distribution\")\ndata = [trace]\nfig = go.Figure(data=data, layout=layout)\npy.iplot(fig, filename=\"continent\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c6c1d4bbf86a1fae20fd2c5fdc30547afd73bb78"},"cell_type":"markdown","source":"### Country Distribution"},{"metadata":{"trusted":true,"_uuid":"ba9db96bc8e21f169850f90ae7cbbbf22d230909","_kg_hide-input":true},"cell_type":"code","source":"country_series = train_df[\"geoNetwork.country\"].value_counts().head(25)\ncountry_count = country_series.shape[0]\nprint(\"Total No. Of Countries: \", country_count)\ncountry_series = country_series.head(25)\n\ntrace = go.Bar(\n    x=country_series.index,\n    y=country_series.values,\n    marker=dict(\n        color=country_series.values,\n        showscale=True\n    ),\n)\nlayout = go.Layout(title=\"Countrywise Observation Count\")\ndata = [trace]\nfig = go.Figure(data=data, layout=layout)\npy.iplot(fig, filename=\"country\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"499e1ce326730343e450e549636b09449bc66aca"},"cell_type":"markdown","source":"- 50% of the observerations are registered from Americas\n- 360K Observations are from USA alone.\n- Note, China is not there. Google is baned in China\n- Following USA, its India, Is it because of the population\n\n### What is Americas?"},{"metadata":{"trusted":true,"_uuid":"e03d8d49d094f4b8de177a7c671282d5ead15a15","_kg_hide-input":true},"cell_type":"code","source":"americas_df = train_df[train_df[\"geoNetwork.continent\"] == \"Americas\"]\nprint(\"# of Observations in Americas: \", americas_df.shape[0])\namericas_series = americas_df[\"geoNetwork.country\"].value_counts()\ntrace = go.Bar(\n    x=americas_series.index,\n    y=americas_series.values,\n    marker=dict(\n        color=americas_series.values,\n        colorscale=\"Viridis\",\n        showscale=True\n    ),\n)\nlayout = go.Layout(title=\"Americas Observation Count\")\ndata = [trace]\nfig = go.Figure(data=data, layout=layout)\npy.iplot(fig, filename=\"americas\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2e6446938bcfb3ba6140833409b5c64e7279f941"},"cell_type":"markdown","source":"Its USA, Canada and the whole set of countries from South America.\nSince USA takes big chunk of observations, Look at rest of the world"},{"metadata":{"trusted":true,"_uuid":"e8f0318fc8c2cafbd3ed4726f292f027000b7b7e","_kg_hide-input":true},"cell_type":"code","source":"not_usa_df = train_df[train_df[\"geoNetwork.country\"] != \"United States\"]\nprint(\"# of Observations from Rest of the World: \", not_usa_df.shape[0])\nnot_usa_series = not_usa_df[\"geoNetwork.country\"].value_counts().head(50)\ntrace = go.Bar(\n    x=not_usa_series.index,\n    y=not_usa_series.values,\n    marker=dict(\n        color=not_usa_series.values,\n        colorscale=\"Viridis\",\n        showscale=True\n    ),\n)\nlayout = go.Layout(title=\"Not USA Observation Count\")\ndata = [trace]\nfig = go.Figure(data=data, layout=layout)\npy.iplot(fig, filename=\"not_usa\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6c8bd95418f28dbb663bc956ddbae9afbf84f085"},"cell_type":"markdown","source":"- Clearly USA in terms of countries is an outlier\n- Is it wise to treat USA and Rest of the World separately\n### Transaction Revenue by Country"},{"metadata":{"trusted":true,"_uuid":"9eb6f3d2939b63103334bab7d919798ef6383f86","_kg_hide-input":true},"cell_type":"code","source":"train_df[\"totals.transactionRevenue\"] = train_df[\"totals.transactionRevenue\"].astype('float')\nrevenue_by_country = train_df.groupby(\"geoNetwork.country\")[\"totals.transactionRevenue\"].sum().reset_index()\nprint(\"Total Transaction Revenue: %8.2f\" % revenue_by_country[\"totals.transactionRevenue\"].sum())\n#revenue_by_country = np.sort(revenue_by_country.transactionRevenue.values)\nrevenue_by_country.sort_values(by =\"totals.transactionRevenue\", ascending=False, inplace=True)\nrevenue_by_country = revenue_by_country[revenue_by_country[\"totals.transactionRevenue\"] > 0]\n\nactive_countries_count = revenue_by_country.shape[0]\nprint(\"% of Countries contributing: \", (active_countries_count/country_count)*100)\nrevenue_by_country.head(25)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1376b8d80dec4eff05f9558b2545d959adbad3d8","_kg_hide-input":true},"cell_type":"code","source":"LOCDATA=\"\"\"COUNTRY,GDP (BILLIONS),CODE\nAfghanistan,21.71,AFG\nAlbania,13.40,ALB\nAlgeria,227.80,DZA\nAmerican Samoa,0.75,ASM\nAndorra,4.80,AND\nAngola,131.40,AGO\nAnguilla,0.18,AIA\nAntigua and Barbuda,1.24,ATG\nArgentina,536.20,ARG\nArmenia,10.88,ARM\nAruba,2.52,ABW\nAustralia,1483.00,AUS\nAustria,436.10,AUT\nAzerbaijan,77.91,AZE\n\"Bahamas, The\",8.65,BHM\nBahrain,34.05,BHR\nBangladesh,186.60,BGD\nBarbados,4.28,BRB\nBelarus,75.25,BLR\nBelgium,527.80,BEL\nBelize,1.67,BLZ\nBenin,9.24,BEN\nBermuda,5.20,BMU\nBhutan,2.09,BTN\nBolivia,34.08,BOL\nBosnia and Herzegovina,19.55,BIH\nBotswana,16.30,BWA\nBrazil,2244.00,BRA\nBritish Virgin Islands,1.10,VGB\nBrunei,17.43,BRN\nBulgaria,55.08,BGR\nBurkina Faso,13.38,BFA\nBurma,65.29,MMR\nBurundi,3.04,BDI\nCabo Verde,1.98,CPV\nCambodia,16.90,KHM\nCameroon,32.16,CMR\nCanada,1794.00,CAN\nCayman Islands,2.25,CYM\nCentral African Republic,1.73,CAF\nChad,15.84,TCD\nChile,264.10,CHL\n\"People 's Republic of China\",10360.00,CHN\nColombia,400.10,COL\nComoros,0.72,COM\n\"Congo, Democratic Republic of the\",32.67,COD\n\"Congo, Republic of the\",14.11,COG\nCook Islands,0.18,COK\nCosta Rica,50.46,CRI\nCote d'Ivoire,33.96,CIV\nCroatia,57.18,HRV\nCuba,77.15,CUB\nCuracao,5.60,CUW\nCyprus,21.34,CYP\nCzech Republic,205.60,CZE\nDenmark,347.20,DNK\nDjibouti,1.58,DJI\nDominica,0.51,DMA\nDominican Republic,64.05,DOM\nEcuador,100.50,ECU\nEgypt,284.90,EGY\nEl Salvador,25.14,SLV\nEquatorial Guinea,15.40,GNQ\nEritrea,3.87,ERI\nEstonia,26.36,EST\nEthiopia,49.86,ETH\nFalkland Islands (Islas Malvinas),0.16,FLK\nFaroe Islands,2.32,FRO\nFiji,4.17,FJI\nFinland,276.30,FIN\nFrance,2902.00,FRA\nFrench Polynesia,7.15,PYF\nGabon,20.68,GAB\n\"Gambia, The\",0.92,GMB\nGeorgia,16.13,GEO\nGermany,3820.00,DEU\nGhana,35.48,GHA\nGibraltar,1.85,GIB\nGreece,246.40,GRC\nGreenland,2.16,GRL\nGrenada,0.84,GRD\nGuam,4.60,GUM\nGuatemala,58.30,GTM\nGuernsey,2.74,GGY\nGuinea-Bissau,1.04,GNB\nGuinea,6.77,GIN\nGuyana,3.14,GUY\nHaiti,8.92,HTI\nHonduras,19.37,HND\nHong Kong,292.70,HKG\nHungary,129.70,HUN\nIceland,16.20,ISL\nIndia,2048.00,IND\nIndonesia,856.10,IDN\nIran,402.70,IRN\nIraq,232.20,IRQ\nIreland,245.80,IRL\nIsle of Man,4.08,IMN\nIsrael,305.00,ISR\nItaly,2129.00,ITA\nJamaica,13.92,JAM\nJapan,4770.00,JPN\nJersey,5.77,JEY\nJordan,36.55,JOR\nKazakhstan,225.60,KAZ\nKenya,62.72,KEN\nKiribati,0.16,KIR\n\"Korea, North\",28.00,PRK\n\"Korea, South\",1410.00,KOR\nKosovo,5.99,KSV\nKuwait,179.30,KWT\nKyrgyzstan,7.65,KGZ\nLaos,11.71,LAO\nLatvia,32.82,LVA\nLebanon,47.50,LBN\nLesotho,2.46,LSO\nLiberia,2.07,LBR\nLibya,49.34,LBY\nLiechtenstein,5.11,LIE\nLithuania,48.72,LTU\nLuxembourg,63.93,LUX\nMacau,51.68,MAC\nMacedonia,10.92,MKD\nMadagascar,11.19,MDG\nMalawi,4.41,MWI\nMalaysia,336.90,MYS\nMaldives,2.41,MDV\nMali,12.04,MLI\nMalta,10.57,MLT\nMarshall Islands,0.18,MHL\nMauritania,4.29,MRT\nMauritius,12.72,MUS\nMexico,1296.00,MEX\n\"Micronesia, Federated States of\",0.34,FSM\nMoldova,7.74,MDA\nMonaco,6.06,MCO\nMongolia,11.73,MNG\nMontenegro,4.66,MNE\nMorocco,112.60,MAR\nMozambique,16.59,MOZ\nNamibia,13.11,NAM\nNepal,19.64,NPL\nNetherlands,880.40,NLD\nNew Caledonia,11.10,NCL\nNew Zealand,201.00,NZL\nNicaragua,11.85,NIC\nNigeria,594.30,NGA\nNiger,8.29,NER\nNiue,0.01,NIU\nNorthern Mariana Islands,1.23,MNP\nNorway,511.60,NOR\nOman,80.54,OMN\nPakistan,237.50,PAK\nPalau,0.65,PLW\nPanama,44.69,PAN\nPapua New Guinea,16.10,PNG\nParaguay,31.30,PRY\nPeru,208.20,PER\nPhilippines,284.60,PHL\nPoland,552.20,POL\nPortugal,228.20,PRT\nPuerto Rico,93.52,PRI\nQatar,212.00,QAT\nRomania,199.00,ROU\nRussia,2057.00,RUS\nRwanda,8.00,RWA\nSaint Kitts and Nevis,0.81,KNA\nSaint Lucia,1.35,LCA\nSaint Martin,0.56,MAF\nSaint Pierre and Miquelon,0.22,SPM\nSaint Vincent and the Grenadines,0.75,VCT\nSamoa,0.83,WSM\nSan Marino,1.86,SMR\nSao Tome and Principe,0.36,STP\nSaudi Arabia,777.90,SAU\nSenegal,15.88,SEN\nSerbia,42.65,SRB\nSeychelles,1.47,SYC\nSierra Leone,5.41,SLE\nSingapore,307.90,SGP\nSint Maarten,304.10,SXM\nSlovakia,99.75,SVK\nSlovenia,49.93,SVN\nSolomon Islands,1.16,SLB\nSomalia,2.37,SOM\nSouth Africa,341.20,ZAF\nSouth Sudan,11.89,SSD\nSpain,1400.00,ESP\nSri Lanka,71.57,LKA\nSudan,70.03,SDN\nSuriname,5.27,SUR\nSwaziland,3.84,SWZ\nSweden,559.10,SWE\nSwitzerland,679.00,CHE\nSyria,64.70,SYR\nTaiwan,529.50,TWN\nTajikistan,9.16,TJK\nTanzania,36.62,TZA\nThailand,373.80,THA\nTimor-Leste,4.51,TLS\nTogo,4.84,TGO\nTonga,0.49,TON\nTrinidad and Tobago,29.63,TTO\nTunisia,49.12,TUN\nTurkey,813.30,TUR\nTurkmenistan,43.50,TKM\nTuvalu,0.04,TUV\nUganda,26.09,UGA\nUkraine,134.90,UKR\nUnited Arab Emirates,416.40,ARE\nUnited Kingdom,2848.00,GBR\nUnited States,17420.00,USA\nUruguay,55.60,URY\nUzbekistan,63.08,UZB\nVanuatu,0.82,VUT\nVenezuela,209.20,VEN\nVietnam,187.80,VNM\nVirgin Islands,5.08,VGB\nWest Bank,6.64,WBG\nYemen,45.45,YEM\nZambia,25.61,ZMB\nZimbabwe,13.74,ZWE\n    \"\"\"\nwith open(\"location.csv\", \"w\") as ofile:\n    ofile.write(LOCDATA)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"41a798d67031b7b3cdc58499d82d0d002d943692","_kg_hide-input":true},"cell_type":"code","source":"location_df = pd.read_csv(\"location.csv\")\nrevenue_by_country_new = pd.merge(\n    revenue_by_country[[\"geoNetwork.country\", \"totals.transactionRevenue\"]], location_df, \n    left_on=\"geoNetwork.country\", right_on=\"COUNTRY\")\n\nrevenue_by_country_new.head(25)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"000da571f0c065ac99addd838b00f8b0ec16a1d4"},"cell_type":"markdown","source":"### Revenue on the Map(USA Excluded)"},{"metadata":{"trusted":true,"_uuid":"022b2f5e9e167a57ac58646100df08bea5b0612c","_kg_hide-input":true},"cell_type":"code","source":"revenue_by_country_new = revenue_by_country_new[revenue_by_country_new[\"geoNetwork.country\"] != \"United States\"]\n\ndata = [dict(\n    type=\"choropleth\",\n    locations=revenue_by_country_new[\"CODE\"],\n    z=revenue_by_country_new[\"totals.transactionRevenue\"],\n    text=revenue_by_country_new[\"geoNetwork.country\"],\n    colorscale=\"Viridis\",\n    autocolorscale=False,\n    reversescale=True,\n    marker=dict(\n        line=dict(\n            color=\"rgb(180, 180, 180)\",\n            width=0.5\n        )\n    ),\n    colorbar=dict(\n        autotick=False,\n        title=\"Transaction Revenue\"\n    )\n)]\nlayout = dict(\n    title=\"Countrywise Transaction Revenue\",\n    geo=dict(\n        showframe=False,\n        showcoastlines=True,\n        projection=dict(type=\"Mercator\")\n    )\n)\nfig = dict(data=data, layout=layout)\n# py.iplot(fig, validate=False, filename=\"d3-world-map\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"eeca1a90e276819b2d405fd59b60862560dce847"},"cell_type":"markdown","source":"## Device Characteristics\n### Active Customer: What browser they use?"},{"metadata":{"trusted":true,"_uuid":"1fce5a200b619819126b03352785da5d2df55960","_kg_hide-input":true},"cell_type":"code","source":"train_active_df = train_df[train_df[\"totals.transactionRevenue\"] > 0]\ncountry_series = train_active_df[\"device.browser\"].value_counts()\ncountry_count = country_series.shape[0]\nprint(\"Total No. Of Countries: \", country_count)\ncountry_series = country_series.head(25)\ntrace = go.Bar(\n    x=country_series.index,\n    y=country_series.values,\n    marker=dict(\n        color=country_series.values,\n        colorscale=\"Viridis\",\n        showscale=True\n    ),\n)\nlayout = go.Layout(title=\"Countrywise Observation Count\")\ndata = [trace]\nfig = go.Figure(data=data, layout=layout)\npy.iplot(fig, filename=\"country\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a89ddbcff221c7caa50dc3ed69a95c9f90912cc2"},"cell_type":"markdown","source":"### Which Device they use?"},{"metadata":{"trusted":true,"_uuid":"a56aa1ae6ffc51d0be6ab23b2e530f1c4541bd60","_kg_hide-input":true,"_kg_hide-output":false},"cell_type":"code","source":"device_category = train_active_df[\"device.deviceCategory\"].value_counts()\nlabels = (np.array(device_category.index))\nsizes = (np.array((device_category / device_category.sum())*100))\n\ntrace = go.Pie(labels=labels, values=sizes)\nlayout = go.Layout(title=\"Device Category Distribution\")\ndata = [trace]\nfig = go.Figure(data=data, layout=layout)\npy.iplot(fig, filename=\"device_category\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f6fc0625d1f7668d68605930bf52054e6e46e1ae"},"cell_type":"markdown","source":"### Does this behaviour indicates mobile devices are still not there when it comes business?\n## How to differentiate Device Category and IsMobile field, both should have similar pattern"},{"metadata":{"trusted":true,"_uuid":"121af2084be8e1d34f97e2c51d1aa28e26a92b52","_kg_hide-input":true},"cell_type":"code","source":"is_mobile = train_active_df[\"device.isMobile\"].value_counts()\nlabels = (np.array(is_mobile.index))\nsizes = (np.array((is_mobile / is_mobile.sum())*100))\n\ntrace = go.Pie(labels=labels, values=sizes)\nlayout = go.Layout(title=\"Mobile or Not\")\ndata = [trace]\nfig = go.Figure(data=data, layout=layout)\npy.iplot(fig, filename=\"device_category\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"5a01a87b817a057da2bcd5a0b55ecf5462362f25"},"cell_type":"markdown","source":"## Transaction Revenue: Visitor ID"},{"metadata":{"trusted":true,"_uuid":"2b13d8c0cf8a3401b192100fd1e52116e1a64842","_kg_hide-input":true},"cell_type":"code","source":"visitor_transactions = train_active_df.groupby(\"fullVisitorId\")[\"totals.transactionRevenue\"].sum().reset_index()\nvisitor_transactions.head(25)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c520718e17993610dcec6886107277e73d03c1a1"},"cell_type":"markdown","source":"Sort the transaction revenue and plot the observations."},{"metadata":{"trusted":true,"_uuid":"88de4c2d0218e2df857e3bb48b453dbc4f0dbf2c","_kg_hide-input":true},"cell_type":"code","source":"trace = go.Scatter(\n    x=visitor_transactions.index,\n    y=np.sort(visitor_transactions[\"totals.transactionRevenue\"].values.astype(float)),\n    mode='markers',\n    marker=dict(\n        sizemode=\"diameter\",\n        sizeref=2,\n        size=2,\n        color=visitor_transactions[\"totals.transactionRevenue\"].values.astype(float),\n        colorscale=\"Viridis\",\n        showscale=True\n    )\n    \n)\n\nlayout = go.Layout(\n    title='Transaction Revenue Distribution by Visitor'\n)\n\ndata = [trace]\nfig = go.Figure(data=data, layout=layout)\npy.iplot(fig, filename=\"transact_rev_by_vistor\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f32d069c6e975d0d456e297f21e11343d6755cf3"},"cell_type":"markdown","source":"** Plot log of transaction revenue to get an idea whats going on **"},{"metadata":{"trusted":true,"_uuid":"f9c47eeb36496d6048aea85908098f2281d7d5bd","_kg_hide-input":true},"cell_type":"code","source":"\ntrace = go.Scatter(\n    x=visitor_transactions.index,\n    y=np.sort(np.log1p(visitor_transactions[\"totals.transactionRevenue\"].values.astype(float))),\n    mode='markers',\n    marker=dict(\n        color=visitor_transactions[\"totals.transactionRevenue\"].values.astype(float),\n        colorscale=\"Viridis\",\n        showscale=True\n    )\n    \n)\n\nlayout = go.Layout(\n    title='Transaction Revenue Distribution by Visitor'\n)\n\ndata = [trace]\nfig = go.Figure(data=data, layout=layout)\npy.iplot(fig, filename=\"transact_rev_by_vistor\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"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}