Skip to content

Instantly share code, notes, and snippets.

View urigoren's full-sized avatar

Uri Goren urigoren

View GitHub Profile
from itertools import combinations
from functools import reduce
from pprint import pprint as pr
def all_arrangements(k):
m=k*(k+1)/2
m_bits_on=set([tuple(reduce(lambda x,y:x[:y]+[1]+x[y+1:],c,[0]*(2*m+1))) for c in combinations(range(2*m+1),m)])
return set([tuple(sorted(filter(lambda i:i>0,reduce(lambda x,y: x+[y] if y==0 else x[:-1]+[x[-1]+1,],p,[0])))) for p in m_bits_on])
def move(t,n=1):
<?php
define('SLACK_WEBHOOK', 'https://hooks.slack.com/services/xxx/yyy/zzz');
define('TELEGRAM_BOT_TOKEN', '...');
define('TELEGRAM_CHAT_ID', '12345');
function slack($txt) {
$msg = array('text' => $txt);
$c = curl_init(SLACK_WEBHOOK);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
import numpy as np
from scipy import sparse
import collections
"""
#SPARK Co-Occurence Matrix
#Format: (vertex, vertex) : count
import json, operator, itertools
def cooccur_matrix(srcHdfs, product2vertex):
ret = sc.textFile(srcHdfs)\
@urigoren
urigoren / LSTM_Binary.py
Last active June 22, 2023 19:37
LSTM Binary classification with Keras
from keras.layers import Dense, Dropout, LSTM, Embedding
from keras.preprocessing.sequence import pad_sequences
from keras.models import Sequential
import pandas as pd
import numpy as np
input_file = 'input.csv'
def load_data(test_split = 0.2):
print ('Loading data...')
@urigoren
urigoren / rshift.py
Last active April 19, 2021 20:50
Use arrow notation (>>) like Haskell to make filter, map and reduce operations more readable.
from itertools import chain
from functools import reduce
import operator
"""
Usage of this module:
<iterable> >> function3 * function2 * function1 >> aggregation
for example:
@urigoren
urigoren / hvim.sh
Created July 4, 2017 10:58
Run vim on hadoop files
hadoop fs -text $1>hvim.txt
vim hvim.txt
hadoop fs -rm -skipTrash $1
hadoop fs -copyFromLocal hvim.txt $1
rm hvim.txt
hadoop fs -chmod 777 $1
@urigoren
urigoren / mcl.py
Last active April 17, 2024 23:52
Markov clustering algorithm
import numpy as np
from scipy.sparse import linalg, eye, csr_matrix
from sklearn.preprocessing import normalize
from sklearn.metrics.pairwise import pairwise_distances
from collections import defaultdict
class MarkovClustering:
def __init__(self, matrix, metric="cosine", bias=1):
@urigoren
urigoren / purity.py
Last active August 9, 2017 20:03
This module tests whether a function is pure
import ast, inspect, textwrap
whitelist = {'math', 'itertools', 'collections', 'functools', 'operator',
'json', 'pickle', 'string', 'types', 'statistics', 'fractions', 'decimal'}
def pure(f):
"""pure decorator"""
f.pure = True
return f
import numpy as np
from matplotlib import pyplot as plt
def polyfit_plot(x, y, p=1):
plt.scatter(x, y)
axes = plt.gca()
coeff = np.polyfit(x, y, p)
X_plot = np.linspace(axes.get_xlim()[0],axes.get_xlim()[1],100)
print (np.corrcoef(x,y)[0,1])
Y_plot = 0
@urigoren
urigoren / nre.md
Last active September 14, 2019 18:20
Nice regular expressions