Nice that now python has this built in method for creating virtual environments per docs # like this python3 -m venv /path/to/new/virtual/environment python -m venv ~/.python_venvs/skpy39 source ~/.python_venvs/skpy39/bin/activate pip install scikit-learn scikit-learn pandas ipdb ipython matplotlib tqdm colormap easydev
Handies
histogram overlays # Nice technique from https://srome.github.io/Covariate-Shift,-i.e.-Why-Prediction-Quality-Can-Degrade-In-Production-and-How-To-Fix-It/ # ... put two histograms on same plot ... def produce_overlayed_hists_for_col_dfs(col, dfs): fig = plt.figure(figsize=(12,12)) ax = fig.add_subplot(121) ax.hist(dfs[0][1][col], color='r', alpha=0.2, bins=50) ax.hist(dfs[1][1][col], color='b', alpha=0.2, bins=50) ax.set(title=f'{dfs[0][0]} (red) vs {dfs[1][0]} (blue)', ylabel=col) Basic goal looks like the below. sparse diagonal x axis ticks import matplotlib.pyplot as plt import pandas as pd import datetime def make_xtick_labels(x, step=5): '''Given x, step the labels every <step> Aka, take every <step>th x label ''' x_ticks = [i for i in range(len(x)) if i % step == 0] x_labels = [x[i] for i in x_ticks] return x_ticks, x_labels Did not add an example x , y yet, but showing an example where x contains dates and y is numeric....
concurrent.futures Recently at work I needed to add retry logic to some code that was using the concurrent python library. I had done some research and I ended up also answering this stack overflow question too in the process. I am finding concurrent.futures to be pretty nice! Of course joblib is nice too. Anyway, re-posting my answer below as well. import concurrent.futures import time import urllib from random import randint from collections import defaultdict URLS = ['http://www....
environmental variable local injection using https://pypi.org/project/python-dotenv/ pip install -U python-dotenv Given a file like .env.test … FOO=hi from dotenv import load_dotenv, find_dotenv load_dotenv(find_dotenv(".env.test", raise_error_if_not_found=True)) import os os.getenv('FOO') # => 'hi'
import numpy as np from bokeh.plotting import figure, show, output_file def doplot(x, y, **figure_kwargs): N = x.shape[0] radii = np.array([0.1,]*N) # print 'DEBUG, ', radii[:4], ', ', N colors = [ "#%02x%02x%02x" % (int(r), int(g), 150) for r, g in zip(50+2*x, 30+2*y) ] TOOLS="hover,crosshair,pan,wheel_zoom,zoom_in,zoom_out,box_zoom,undo,redo,reset,tap,save,box_select,poly_select,lasso_select," p = figure(tools=TOOLS, **figure_kwargs) p.scatter(x, y, radius=radii, fill_color=colors, fill_alpha=0.6, line_color=None) output_file("color_scatter.html") show(p) # open a browser def make_data(N=100, trials=1000, minmax=(0, 1)): a, b = minmax data = [[sum(vec), fano(vec)] for vec in [a + (b - a)*np....