Skip to content

Instantly share code, notes, and snippets.

View dansondergaard's full-sized avatar

Dan Søndergaard dansondergaard

View GitHub Profile
@dansondergaard
dansondergaard / decisiontree.py
Created October 30, 2013 00:11
A simple decision tree for classification.
"""Decision Tree"""
__all__ = ['grow', 'classify']
class Node(object):
def __init__(self, error, dimension, value, left, right):
self.error = error
self.dimension = dimension
self.value = value
@dansondergaard
dansondergaard / naivebayes.r
Created October 19, 2013 23:43
R implementation of the Naive Bayes classifier example from [Wikipedia](http://en.wikipedia.org/wiki/Naive_Bayes_classifier#Examples).
# naivebayes.txt
#
# sex height weight footsize
# male 6 180 12
# male 5.92 190 11
# male 5.58 170 12
# male 5.92 165 10
# female 5 100 6
# female 5.5 150 8
# female 5.42 130 7
@dansondergaard
dansondergaard / print_stream_redirect.py
Created August 26, 2013 17:35
I recently discovered this awesome redirection syntax for the print statement in Python. Also, it reveals quite nicely that print writes the newline separately to the stream.
class Logger(object):
def __init__(self):
self.records = []
def write(self, record):
self.records.append(record)
logger = Logger()
print >> logger, "hello"
print >> logger, "world"
class Node(object):
def __init__(self, name, checkpoint=False):
self.name = name
self.explored = False
self.checkpoint = checkpoint
self.edges = []
def add_edge(self, other):
self.edges.append(other)
int *translate_observations_to_indexes(struct hmm_t *hmm, const char *observations) {
int *observations_idx = malloc(strlen(observations) * sizeof(int));
for (int i = 0; i < strlen(observations); i++) {
for (int j = 0; j < hmm->observables_size; j++) {
if (observations[i] == hmm->observables[j]) {
observations_idx[i] = j;
}
}
}
return observations_idx;
@dansondergaard
dansondergaard / README.md
Created January 11, 2013 08:55
Easily modify your scripts to run any command in parallel.

= q

Many of the command-line tools we use and write daily have no clue about multiple cores or threads. Wrote a script to convert a directory of music files to some other format? You probably did it serially! Did you write a script for downloading a bunch of big files? You probably did that serially too!

These problems are inherently parallisable. Don't waste idle CPU cycles on your machine.

q is here to help. It allows you to conveniently build a queue of commands that you wish to run. When you're done adding commands to the queue, you execute the queue. q will automatically figure out how many cores you've got and run your commands in parallel. Here's a generic example:

@dansondergaard
dansondergaard / CMakeLists.txt
Last active December 10, 2015 15:39
Generic CMake C++ template. Supports GCC and clang.
cmake_minimum_required (VERSION 2.6)
project(myproject)
file(GLOB "${PROJECT_NAME}_SOURCES" *.cpp)
set(CMAKE_CXX_FLAGS "-Wall -pedantic -std=c++11")
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g")
set(CMAKE_CXX_FLAGS_MINSIZEREL, "-Os -DNDEBUG")
set(CMAKE_CXX_FLAGS_RELEASE, "-O4 -DNDEBUG")
@dansondergaard
dansondergaard / jvm-core-dump.txt
Created December 8, 2012 15:19
jvm core dump
#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0x000000010a0e83e3, pid=15815, tid=19459
#
# JRE version: 7.0_07-b10
# Java VM: Java HotSpot(TM) 64-Bit Server VM (23.3-b01 mixed mode bsd-amd64 compressed oops)
# Problematic frame:
# V [libjvm.dylib+0x3cd3e3]
#
@dansondergaard
dansondergaard / Viterbi.scala
Created November 25, 2012 18:46
Viterbi in Scala
package dk.linnk.hmm
object Viterbi {
type State = String
type Observation = String
type Probability = Double
type ProbabilityMap = (State, State) => Probability
type EmissionMap = (State, Observation) => Probability
type ProbabilityPath = (Probability, List[State])
@dansondergaard
dansondergaard / Newick.scala
Created November 6, 2012 20:44
Newick Parser, much simplified, with printer
package dk.linnk
import scala.util.parsing.combinator._
sealed abstract class Tree
case class Node(edges: List[Edge], name: Option[String]) extends Tree
case class Leaf(name: Option[String]) extends Tree
case class Edge(to: Tree, weight: Option[Double]) extends Tree
trait Newick {