Skip to content

Instantly share code, notes, and snippets.

View fnielsen's full-sized avatar

Finn Årup Nielsen fnielsen

View GitHub Profile
@fnielsen
fnielsen / Nielsen2014Data_femalehurricanes.ipynb
Last active August 29, 2015 14:02
Modeling of Female Hurricanes dataset.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@fnielsen
fnielsen / typetesting.py
Created January 3, 2014 14:54
Small example demonstrating the need for testing functions with different types of input arguments. Here an integer division problem (in Python 2).
from numpy import max, min
def mean_diff(a):
"""
Parameters
----------
a : array_like
"""
return float((max(a) - min(a)) / (len(a) - 1))
@fnielsen
fnielsen / gist:7988477
Last active December 31, 2015 12:49
YouTube comment reading
import gdata.youtube.service
from time import sleep
class YtsClassic(object):
"""
http://finnaarupnielsen.wordpress.com/2009/09/21/getting-comments-from-youtube-via-pythons-gda/
"""
def __init__(self):
self.yts = gdata.youtube.service.YouTubeService()
@fnielsen
fnielsen / gist:7873982
Created December 9, 2013 15:29
Simplest EEG reader
# This example is close to the example provide by the emokit module.
from emokit import emotiv
import gevent
headset = emotiv.Emotiv()
gevent.spawn(headset.setup)
gevent.sleep(1.5)
try:
@fnielsen
fnielsen / instance_class_variable.py
Last active December 27, 2015 08:59
Instance variables vs. class (static) variables
class MyClass:
my_static_variable = "Static" # not self.
def __init__(self):
self.my_instance_variable = "Instance"
def change_static(self):
MyClass.my_static_variable = "Changed" # not self.
def change_instance(self):
self.my_instance_variable = "Also changed"
def accuracy(truth, predicted):
if len(truth) != len(predicted):
raise Exception("Wrong sizes ...")
total = len(truth)
if total == 0:
return 0
hits = len(filter(lambda (x, y): x == y, zip(truth, predicted)))
return float(hits)/total
@fnielsen
fnielsen / brownzipf.py
Last active November 6, 2022 12:32
Zipf plot from word counts in the Brown corpus
from __future__ import division
from itertools import *
from pylab import *
from nltk.corpus import brown
from string import lower
from collections import Counter
# The data: token counts from the Brown corpus
tokens_with_count = Counter(imap(lower, brown.words()))
@fnielsen
fnielsen / gist:6761078
Created September 30, 2013 08:55
Tkinter canvas: Drawing in the Tkinter interface with canvas. Example from (Langtangen, 2005, pages 527+)
from Tkinter import *
root = Tk()
canvas = Canvas(root, width=400, height=200)
canvas.pack()
canvas.create_oval(10, 10, 110, 60, fill="grey")
canvas.create_text(60, 35, text="Oval")
canvas.create_rectangle(10, 100, 110, 150, outline="blue")
canvas.create_text(60, 125, text="Rectangle")
@fnielsen
fnielsen / gist:6761017
Created September 30, 2013 08:48
Tkinter Hello world in Python
from Tkinter import *
def hello():
"""Callback function for button press."""
print "Hello World"
# Main root window
root = Tk()
# A button with a callback function
@fnielsen
fnielsen / gist:6599783
Created September 17, 2013 20:01
Example of iteration over YouTube comments with Python.
import gdata.youtube.service
yts = gdata.youtube.service.YouTubeService()
urlpattern = "http://gdata.youtube.com/feeds/api/videos/" + \
"9ar0TF7J5f0/comments?start-index=%d&max-results=25"
index = 1
url = urlpattern % index
comments = []
while True:
ytfeed = yts.GetYouTubeVideoCommentFeed(uri=url)
comments.extend([comment.content.text for comment in ytfeed.entry])