Skip to content

Instantly share code, notes, and snippets.

View mamonu's full-sized avatar
🎯
Focusing

Theodore M mamonu

🎯
Focusing
View GitHub Profile
@mamonu
mamonu / logloss.py
Last active August 29, 2015 14:22
log-loss evaluation metric for prediction funciton
import scipy as sp
def loglossf(act, pred):
epsilon = 1e-15
pred = sp.maximum(epsilon, pred)
pred = sp.minimum(1-epsilon, pred)
ll = sum(act*sp.log(pred) + sp.subtract(1,act)*sp.log(sp.subtract(1,pred)))
ll = ll * -1.0/len(act)
return ll
@mamonu
mamonu / pyplotdemo.py
Created June 11, 2015 10:27
minimal matplotlib demo
import matplotlib.pyplot
import numpy as np
x = np.random.randn(10)
y = np.random.randn(10)
print x,y
matplotlib.pyplot.scatter(x, y)
matplotlib.pyplot.show()
matplotlib.pyplot.savefig('PLTDEMO.png')
@mamonu
mamonu / multtestplot.py
Created June 11, 2015 10:40
calculate probability of false positive after x tests
import matplotlib.pyplot
import numpy as np
x = np.arange(1,150,1)
y = 1.0-((1.0-0.05)**x)
print x,y
matplotlib.pyplot.scatter(x, y)
#matplotlib.pyplot.show()
matplotlib.pyplot.savefig('PLTDEMO.png')
@mamonu
mamonu / euclidgcd.py
Created June 11, 2015 16:15
find greatest common denominator with Euclidean algorithm
def gcd(u, v):
return gcd(v, u % v) if v else abs(u)
@mamonu
mamonu / getStackO.r
Last active August 29, 2015 14:22
get Stack Overflow programming language use data
# Get StackOverflow data
get.stack<-function(tok) {
# Must check for XML install, thanks onertipaday!
if (!require(XML)) install.packages('XML')
library(XML)
# Enter a SO tag as character string, and number of tags are returned
tok<-gsub("(/| )","-",tok)
tok<-gsub("#","%23",tok,fixed=TRUE)
base.stack<-"http://stackoverflow.com/questions/tagged/"
stack.tree<-htmlTreeParse(paste(base.stack,tok,sep=""),useInternalNodes=TRUE)
@mamonu
mamonu / printfile.clj
Created June 13, 2015 13:29
open and print a file in clojure
(defn print-file [filename]
(with-open [rdr (clojure.java.io/reader filename)]
(println (line-seq rdr))))
@mamonu
mamonu / markovtext.clj
Created June 13, 2015 13:38
markovchain based text obfuscation :)
(defn markov-data [text]
(let [maps
(for [line (clojure.string/split text #"\.")
m (let [l (str line ".")
words
(cons :start (clojure.string/split l #"\s+"))]
(for [p (partition 2 1 (remove #(= "" %) words))]
{(first p) [(second p)]}))]
m)]
@mamonu
mamonu / someclojure.clj
Created June 14, 2015 00:21
from (Learn X in Y minutes Where X=clojure) blog post
; Comments start with semicolons.
; Clojure is written in "forms", which are just
; lists of things inside parentheses, separated by whitespace.
;
; The clojure reader assumes that the first thing is a
; function or macro to call, and the rest are arguments.
; The first call in a file should be ns, to set the namespace
(ns learnclojure)
@mamonu
mamonu / higherorderfunctions.clj
Created June 14, 2015 10:26
clojure higher order functions (map / filter / reduce)
(meditations
"The map function relates a sequence to another"
(= [4 8 12] (map (fn [x] (* 4 x)) [1 2 3]))
"You may create that mapping"
(= [1 4 9 16 25] (map (fn [x] (* x x)) [1 2 3 4 5]))
"Or use the names of existing functions"
(= [false false true false false] (map nil? [:a :b nil :c :d]))
@mamonu
mamonu / generateMAC.clj
Last active August 29, 2015 14:23
generate a MAC using the ISO9797 Algorithm 3 in Clojure
(defn mac2 [key message]
(let [engine (org.bouncycastle.crypto.engines.DESEngine.)
mac (org.bouncycastle.crypto.macs.ISO9797Alg3Mac. engine)
bytes (byte-array (.getMacSize mac))
key (->bytes key)
msg (->bytes E-IFD)]
(prn key (count key))
(.init mac (org.bouncycastle.crypto.params.DESedeParameters. key))
(.update mac msg 0 (count msg))
(.doFinal mac bytes 0)