Skip to content

Instantly share code, notes, and snippets.

@rougier
rougier / fractal-dimension.py
Last active October 18, 2023 12:37
Fractal dimension computing
# -----------------------------------------------------------------------------
# From https://en.wikipedia.org/wiki/Minkowski–Bouligand_dimension:
#
# In fractal geometry, the Minkowski–Bouligand dimension, also known as
# Minkowski dimension or box-counting dimension, is a way of determining the
# fractal dimension of a set S in a Euclidean space Rn, or more generally in a
# metric space (X, d).
# -----------------------------------------------------------------------------
import scipy.misc
import numpy as np
@karpathy
karpathy / min-char-rnn.py
Last active May 6, 2024 16:42
Minimal character-level language model with a Vanilla Recurrent Neural Network, in Python/numpy
"""
Minimal character-level Vanilla RNN model. Written by Andrej Karpathy (@karpathy)
BSD License
"""
import numpy as np
# data I/O
data = open('input.txt', 'r').read() # should be simple plain text file
chars = list(set(data))
data_size, vocab_size = len(data), len(chars)
@karpathy
karpathy / gist:587454dc0146a6ae21fc
Last active March 19, 2024 05:50
An efficient, batched LSTM.
"""
This is a batched LSTM forward and backward pass
"""
import numpy as np
import code
class LSTM:
@staticmethod
def init(input_size, hidden_size, fancy_forget_bias_init = 3):
@wpm
wpm / spark_parallel_boost.py
Last active December 3, 2018 02:56
A simple example of how to integrate the Spark parallel computing framework and the scikit-learn machine learning toolkit. This script randomly generates test and train data sets, trains an ensemble of decision trees using boosting, and applies the ensemble to the test set. The ensemble training is done in parallel.
from pyspark import SparkContext
import numpy as np
from sklearn.cross_validation import train_test_split, Bootstrap
from sklearn.datasets import make_classification
from sklearn.metrics import accuracy_score
from sklearn.tree import DecisionTreeClassifier
def run(sc):
@johnb30
johnb30 / bootstrap.py
Created November 29, 2012 01:06
Bootstrapped two-sample t-test in Python
from __future__ import division
import numpy as np
import pandas as pd
import random
def sample(data):
sample = [random.choice(data) for _ in xrange(len(data))]
return sample
def bootstrap_t_test(treatment, control, nboot = 1000, direction = "less"):
@danhammer
danhammer / gist:1691694
Created January 28, 2012 00:05
trend analysis in python and r
def convert_ts(time_series, start_year=2000, start_pd=4, freq=23):
"""
Convert a numpy time-series into an rpy2 object, which, in turn,
is a 'ts' object in R. The 'ts' object is more wholly specified
if a start date is provided. For our applications at 16-day
intervals for NDVI, this start date is April 2000, with a
frequency of 23 observations each year.
input: numpy time-series
output: rpy2 ts object