Skip to content

Instantly share code, notes, and snippets.

View pavlin-policar's full-sized avatar

Pavlin Poličar pavlin-policar

View GitHub Profile
Moodle prepend lang tags where any translations are needed.
Long syntax:
Find:
(<span lang="es")
Replace:
<span lang="en" class="multilang">&nbsp;English version<\/span><span lang="sl" class="multilang">&nbsp;Slovenska verzija<\/span>$1
Short syntax:
Find:
@pavlin-policar
pavlin-policar / keras_load_model_error.py
Last active February 23, 2017 17:26
Can't load_model with error “Optimizer weight shape (...) not compatible with provided weight shape (...)”
# Solution found here: https://github.com/fchollet/keras/issues/4044
import h5py
f = h5py.File('model_file.h5', 'r+')
del f['optimizer_weights']
f.close()
import numpy as np
def entropy(p):
"""Compute the Shannon entropy of a distribution.
The Shannon entropy is defined as follows
:math:`\sum_x p(x_i) * \log p(x_i)`.
Parameters
class SomeClass:
class single_execution:
"""Compute property only once and cache the result for further access.
When the property is first accessed, the result is computed, and the
attribute on the instance is replaced with the result. This is
essentially a single execution lazy property.
"""
def __init__(self, method):
self.__method = method
self.__attribute_name = method.__name__
from functools import reduce
from typing import Iterable, Dict, List, Union, Optional
from collections import Counter
import numpy as np
from data_provider import get_talks
class TokenizedDocument(Counter):
@pavlin-policar
pavlin-policar / ridge_circulant.py
Created April 30, 2018 18:45
Ridge regression on circulant matrices
import numpy as np
# Initialize the generating vector
x = np.array([1, 2, 3, 4, 5])
y = np.array([5, 4, 3, 2, 1])
# Initialize the permutation matrix
P = np.array([
[0, 0, 0, 0, 1],
[1, 0, 0, 0, 0],
[0, 1, 0, 0, 0],
@pavlin-policar
pavlin-policar / lagrange_interpolation.py
Created May 1, 2018 19:36
Lagrange interpolating polynomial for both arbitrary and equidistant points
from typing import Callable
import numpy as np
import matplotlib.pyplot as plt
def lagrangian_interpolation(xs: np.ndarray, ys: np.ndarray) -> Callable:
"""Make a Lagrangian interpolating polynomial function."""
def _interpolate(x: float) -> float:
result = 0
@pavlin-policar
pavlin-policar / max_heap.py
Created November 22, 2018 11:11
Python implementation of max heap/priority queue
class MaxHeap:
def __init__(self, collection=None):
self._heap = []
if collection is not None:
for el in collection:
self.push(el)
def push(self, value):
self._heap.append(value)
@pavlin-policar
pavlin-policar / h5ad.r
Last active September 16, 2019 13:26
Utility functions to read/write H5AD files from R
read.h5ad = function (fname) {
#' Read an H5AD file
#'
#' @param fname str: The path to the H5AD file
library(reticulate)
library(Matrix)
ad = import("anndata", convert = FALSE)
sp = import("scipy.sparse", convert = FALSE)
"""Adapted from Orange 2"""
import numpy as np
import matplotlib.pyplot as plt
def compute_critical_difference(avg_ranks, N, alpha="0.05", type="nemenyi"):
""" Returns critical difference for Nemenyi or Bonferroni-Dunn test
according to given alpha (either alpha="0.05" or alpha="0.1") for average
ranks and number of tested data sets N. Type can be either "nemenyi" for