Skip to content

Instantly share code, notes, and snippets.

@benman1
benman1 / create_poster.py
Created October 26, 2020 16:23
Create a poster based on a video. This can be useful for educational videos for your children.
# # Recipe for creating a poster based on a video
#
# 1. download a video (for example, youtube-dl)
# 2. extract images from the video, say every second one frame (ffmpeg)
# 3. choose the images you want and delete the images you don't want
# 4. (this notebook) align the images horizontally and vertically based on the numbering
# got this part from https://note.nkmk.me/en/python-pillow-concat-images/
from PIL import Image
@benman1
benman1 / keras-yolo3.py
Last active July 5, 2020 10:32
keras yolo3 object detection
"""
This is based on keras-yolo3 (https://github.com/experiencor/keras-yolo3)
and licensed under MIT
"""
import os
import numpy as np
from tensorflow.keras.layers import Conv2D, Input, BatchNormalization, LeakyReLU, ZeroPadding2D, UpSampling2D
from tensorflow.keras.layers import concatenate, add
from tensorflow.keras.models import Model
import struct
@benman1
benman1 / monte_carlo_tree_search.py
Created May 25, 2020 09:33 — forked from qpwo/monte_carlo_tree_search.py
Monte Carlo tree search (MCTS) minimal implementation in Python 3, with a tic-tac-toe example gameplay
"""
A minimal implementation of Monte Carlo tree search (MCTS) in Python 3
Luke Harold Miles, July 2019, Public Domain Dedication
See also https://en.wikipedia.org/wiki/Monte_Carlo_tree_search
https://gist.github.com/qpwo/c538c6f73727e254fdc7fab81024f6e1
"""
from abc import ABC, abstractmethod
from collections import defaultdict
import math
@benman1
benman1 / create_package_doc.py
Last active December 19, 2021 22:04
Create a documentation for a complete python package using pdoc
"""
Adapter from
[stackoverflow](https://stackoverflow.com/questions/39453948/create-a-html-page-with-a-list-of-packages-using-pdoc-module).
Please note that this script is currently limited to one level of submodules. Also it's a bit ugly. Help fix it, if you like.
This looks atrocious for markdown (my impresssion)
"""
from os import path, makedirs
import argparse
@benman1
benman1 / create_package_doc.py
Created May 14, 2020 11:50
Create a documentation for a complete python package
"""
Adapter from
[stackoverflow](https://stackoverflow.com/questions/39453948/create-a-html-page-with-a-list-of-packages-using-pdoc-module).
It's a bit ugly. Help fix it, if you like.
"""
from os import path, makedirs
import pdoc
@benman1
benman1 / wordnet_example.cpp
Last active March 27, 2020 14:46
a simple example with wordnet
#include <wn.h>
#include <stdlib.h>
#include <iostream>
// g++ wordnet_example.cpp /usr/lib/libwordnet.a -o wordnet_example
int main(int argc, char **argv) {
if (wninit()) {
printf("Fatal! Could not open the WordNet corpus!\n");
exit(-1);
@benman1
benman1 / calculate_adjacency_matrix.py
Created January 28, 2020 21:26
sparse adjacency matrix from distance metric and thresholding in numba
from numba import njit, jit, prange
import numpy as np
from numba.pycc import CC
from scipy.sparse import lil_matrix
cc = CC('adjacency_utils')
@cc.export('calc_dist', 'f8(f8[:], f8[:])')
@jit("f8(f8[:], f8[:])")
def calc_dist(u, v):
@benman1
benman1 / plot_decision_boundary.py
Last active January 19, 2020 18:10
plot decision boundaries of clustering or classification methods
import matplotlib.pyplot as plt
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import FastICA
from sklearn.pipeline import make_pipeline
from sklearn.svm import SVC
from sklearn.base import TransformerMixin
class Reduce(TransformerMixin):
def fit(self, X, y=None):
@benman1
benman1 / plot_feature_wordcloud.py
Last active January 16, 2020 09:39
Plot word cloud illustrating feature importance
from wordcloud import (WordCloud, get_single_color_func)
import matplotlib.pyplot as plt
from colour import Color
class GroupedColorFunc(object):
"""Create a color function object which assigns DIFFERENT SHADES of
specified colors to certain words based on the color to words mapping.
Uses wordcloud.get_single_color_func
@benman1
benman1 / lift_chart.py
Last active March 1, 2020 16:11
lift chart for predictions - should work for matching vectors, similar to a residuals plots. This can also be used for plotting variables against each other, similar to a scatter graph.
import numpy as np
import pandas as pd
import seaborn as sns
import scipy
def lift_chart(y_true, y_pred, bins=10, ax=None, normalize=False, labels=None):
'''Given matched vectors of true versus predicted
targets, plot them against each other.