Skip to content

Instantly share code, notes, and snippets.

View gr33ndata's full-sized avatar

Tarek Amr gr33ndata

View GitHub Profile
@gr33ndata
gr33ndata / gist:4440339
Created January 3, 2013 02:50
Solution for Kaggle Digit Recognizer using Nearest Centroid, https://www.kaggle.com/c/digit-recognizer
# Solution for Kaggle Digit Recognizer
# <https://www.kaggle.com/c/digit-recognizer>
#
# You need to install sklearn
# Solution done using Rocchio (Nearest Centroid)
#
# Get train and test data files from here::
# <https://www.kaggle.com/c/digit-recognizer/data>
#
# Author: Tarek Amr (@gr33ndata)
@gr33ndata
gr33ndata / gist:5657934
Created May 27, 2013 16:28
Code used to generate a heat map for the Egyptian textile industry. Google Geochart was used here, https://developers.google.com/chart/interactive/docs/gallery/geochart
<html>
<head>
<script type='text/javascript' src='https://www.google.com/jsapi'></script>
<script type='text/javascript'>
google.load('visualization', '1', {'packages': ['geochart']});
google.setOnLoadCallback(drawMarkersMap);
function drawMarkersMap() {
var data = google.visualization.arrayToDataTable([
['City', 'Factories', 'Factories'],
@gr33ndata
gr33ndata / gist:6096781
Created July 27, 2013 23:56
Quick Sort
def qsort(x):
if x == []:
return x
less = []
more = []
for item in x[1:]:
if item < x[0]:
less.append(item)
else:
more.append(item)
@gr33ndata
gr33ndata / gist:8141155
Created December 27, 2013 01:25
This script adds a latlng() function to your Google Spreadsheets so you can easily convert addresses into latitudes and longitudes.
function latlng(address){
var response = Maps.newGeocoder().geocode(address);
if (response.status == "OK"){
var result = response.results[0];
return '' + result.geometry.location.lat + ',' + result.geometry.location.lat + '';
}
else{
return '0,0';
}
};
@gr33ndata
gr33ndata / multiprocessing.py
Created January 18, 2014 07:58
Python Multiprocessing using Pool.map
from time import sleep
from multiprocessing import Pool
def fun(i):
if i % 2:
sleep(2)
print i
pool = Pool(4)
pool.map(fun,range(40))
@gr33ndata
gr33ndata / gist:8727727
Created January 31, 2014 07:08
Even Fibonacci numbers
# Solving this problem:
# http://projecteuler.net/problem=2
# Sum: 4,613,732
def fab(a,b, maxlimit):
while b < maxlimit:
yield b
a, b = b, a+b
@gr33ndata
gr33ndata / gist:8848854
Created February 6, 2014 17:33
Prime numbers generator!
# Using Sieve of Eratosthenes
# https://en.wikipedia.org/wiki/Generating_primes
import sys
def main():
num = int(sys.argv[1])
num_list = [[i,True] for i in range(num)]
num_list[0][1] = False
num_list[1][1] = False
@gr33ndata
gr33ndata / Flatten.py
Created September 15, 2014 19:39
Flatten a List
# Let's have a list x = [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]
# How to convert it into x = [0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4]
x = [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]
# We thuse use reduce.
# When given a list, it goes throgu each pair of items from left to right,
# and applied some function on them.
# Remember, [1,3] + [2,4] => [1, 3, 2, 4]
@gr33ndata
gr33ndata / gist:a098da514144398dd32d
Created March 24, 2015 22:43
Solving Anagram check problem
def dict_load(s):
d = {}
for c in s:
if c == " ":
pass
else:
d[c] = d.get(c, 0) + 1
return d
def dict_unload(s, d):
# We are gonna use Scikit's LinearRegression model
from sklearn.linear_model import LinearRegression
# Your input data, X and Y are lists (or Numpy Arrays)
x = [[2,4],[3,6],[4,5],[6,7],[3,3],[2,5],[5,2]]
y = [14,21,22,32,15,16,19]
# Initialize the model then train it on the data
genius_regression_model = LinearRegression()
genius_regression_model.fit(x,y)