Categories
Analytics Programming

How to fill missing dates in Pandas

Create a pandas dataframe with a date column: import pandas as pd import datetime   TODAY = datetime.date.today() ONE_WEEK = datetime.timedelta(days=7) ONE_DAY = datetime.timedelta(days=1)   df = pd.DataFrame({’dt’: [TODAY-ONE_WEEK, TODAY-3*ONE_DAY, TODAY], ‘x’: [42, 45,127]})import pandas as pd import datetime TODAY = datetime.date.today() ONE_WEEK = datetime.timedelta(days=7) ONE_DAY = datetime.timedelta(days=1) df = pd.DataFrame({‘dt’: [TODAY-ONE_WEEK, TODAY-3*ONE_DAY, TODAY], ‘x’: […]

Categories
Algorithms Analytics Machine Learning Programming

Cosine similarity in Python

Cosine similarity is the normalised dot product between two vectors. I guess it is called “cosine” similarity because the dot product is the product of Euclidean magnitudes of the two vectors and the cosine of the angle between them. If you want, read more about cosine similarity and dot products on Wikipedia. Here is how […]

Categories
Analytics

How to explore two-dimensional data with a heatmap

https://seaborn.pydata.org/generated/seaborn.heatmap.html

Categories
Analytics Programming Spatial stuff

How to display a Choropleth map in Jupyter Notebook

Here is the code: %matplotlib inline import geopandas as gpd import matplotlib as mpl # make rcParams available (optional) mpl.rcParams[’figure.dpi’]= 144 # increase dpi (optional)   world = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres")) world = world[world.name != ‘Antarctica’] # remove Antarctica (optional) world[’gdp_per_person’] = world.gdp_md_est / world.pop_est g = world.plot(column=’gdp_per_person’, cmap=’OrRd’, scheme=’quantiles’) g.set_facecolor(’#A8C5DD’) # make the ocean blue (optional)%matplotlib […]

Categories
Algorithms Analytics Optimization Programming

(Integer) Linear Programming in Python

Step one: brew install glpk pip install pulpbrew install glpk pip install pulp Step two: from pulp import *   prob = LpProblem("test1", LpMinimize)   # Variables x = LpVariable("x", 0, 4, cat="Integer") y = LpVariable("y", -1, 1, cat="Integer") z = LpVariable("z", 0, cat="Integer")   # Objective prob += x + 4*y + 9*z   […]

Categories
Analytics

How to select top-k items from each group in SQL

Here is an analytical query that you (and I) will often need to do if you work in e-commerce, marketing or similar domain. It answers the question, within each group of items (e.g. partitioned by territory, age groups or something else) what are the top-k items for some utility function over the items (e.g. the […]