Skip to content

Instantly share code, notes, and snippets.

@suzaku
suzaku / R1Page.md
Created September 25, 2012 06:07
Model Thinking Note
  • In confronting complex situations, no single model can capture everything. Thus, we need to bring several of models to bear, not just one

  • as powerful as models may be, they’re only guides to thinking. We have to maintain contact with reality.

@suzaku
suzaku / gist:3786423
Created September 26, 2012 06:25
About Graph
  • Simple Graph

  • Undirected

  • has no loops

  • no more than one edge between any two different vertices

  • Regular Graph

    • each vertex has the same number of neighbors
  • Complete Graph

  • each pair of vertices has an edge connecting them

  • different functions with the same growth rate may be represented using the same O notation.
  • If f(x) is a sum of several terms, the one with the largest growth rate is kept, and all others omitted.
  • If f(x) is a product of several factors, any constants (terms in the product that do not depend on x) are omitted.
  • stable sorting algorithms maintain the relative order of records with equal keys
  • A comparison sort examines the data only by comparing two elements with a comparison operator
  • Many integer sorting algorithms are based on the assumption that the key size "n" is large enough that all entries have unique key values, and hence that n << 2k, where << means "much less than."
@suzaku
suzaku / x.py
Created October 11, 2012 03:56
"""This would outputs
x
True
__main__
False
"""
import x
import random
import math
import csv
def hypothesis(params, features):
z = sum(p * f for p, f in zip(params, features))
return 1 / (1 + math.e ** -z)
def logistic_regression(learning_rate, samples):
@suzaku
suzaku / try_plac.py
Created December 16, 2012 12:50
try plac
import plac
import random
@plac.annotations(name="your name")
def hello(name):
"Greet you half of the time, and otherwise * you"
if random.randint(0, 10) > 5:
print "Hello %s" % name
else:
print "Screw you. I'm ... going ... home~"
@suzaku
suzaku / normalize.py
Created December 21, 2012 02:23
normalize data with dpark
"""Compute mean and variance to normalize data"""
import dpark
import plac
import math
def prepare(s):
return [float(i.strip()) for i in s.split(",")]
def add_squared_list(vals):
squared_vals = [v * v for v in vals]
"""Compute mean and variance to normalize data"""
import dpark
import plac
import numpy as np
def prepare(s):
return np.array([float(i.strip()) for i in s.split(",")])
def main(filename):
lines = dpark.textFile(filename)

Pony Object-Relational Mapper

Pony is an object-relational mapper. The most interesting feature of Pony is its ability to write queries to the database using generator expressions. Pony works with entities which are mapped to a SQL database. Using generator syntax for writing queries allows the user to formulate very eloquent queries. It increases the level of abstraction and allows a programmer to concentrate on the business logic of the application. For this purpose Pony analyzes the abstract syntax tree of a generator and translates it to its SQL equivalent.

Following is an example of a query in Pony:

    select(p for p in Product if p.name.startswith('A') and p.cost <= 1000)