Skip to content

Instantly share code, notes, and snippets.

View marcelcaraciolo's full-sized avatar
💭
Coding !

Marcel Caraciolo marcelcaraciolo

💭
Coding !
View GitHub Profile
#-*-coding:utf-8 -*-
"""
Neste primeiro dojo, vamos tentar resolver um problema bem simples com o
objetivo de ter um primeiro contato com as bibliotecas Numpy e Scipy.
Problema: Dada uma matriz de dimensão qualquer, encontrar o elemento de
valor máximo e o elemento de valor mínimo.
@marcelcaraciolo
marcelcaraciolo / dojo.py
Created August 5, 2011 02:20
Final Dojo
#-*-coding:utf-8 -*-
"""
Neste primeiro dojo, vamos tentar resolver um problema bem simples com o
objetivo de ter um primeiro contato com as bibliotecas Numpy e Scipy.
Problema: Dada uma matriz de dimensão qualquer, encontrar o elemento de
valor máximo e o elemento de valor mínimo.
@marcelcaraciolo
marcelcaraciolo / dojo.py
Created August 5, 2011 02:25
Coding Dojo Python
#-*-coding:utf-8 -*-
"""
Neste primeiro dojo, vamos tentar resolver um problema bem simples com o
objetivo de ter um primeiro contato com as bibliotecas Numpy e Scipy.
Problema: Dada uma matriz de dimensão qualquer, encontrar o elemento de
valor máximo e o elemento de valor mínimo.
@marcelcaraciolo
marcelcaraciolo / ip_location.py
Created August 21, 2011 20:06
ip_location API Example
import json
import urllib
API_KEY = "7084a6cb5d680c54df55fb3401ba042bfe9e5d55f61b65f0d8f027e0a0f486b2"
IP = '187.41.231.167'
url = "http://api.ipinfodb.com/v3/ip-city/?key=%s&ip=%s&format=json" % (API_KEY, IP)
from scipy import linalg
import numpy as np
from scipy.spatial.distance import cosine
#Let's define the matrix
user_ids = np.array(['Amanda', 'Anna', 'Bruno', 'Ricardo'])
item_ids = np.array(['Back to The Future', 'Conan',
'Lord of the Rings', 'Star Wars'])
matrix = np.matrix([
@marcelcaraciolo
marcelcaraciolo / spearman.py
Created September 12, 2011 01:48
spearman coefficient
def spearman_coefficient(X, Y):
"""
Considering the rows of X (and Y=X) as vectors, compute the
distance matrix between each pair of vectors.
Like Pearson Coefficient , but compares relative ranking of preference
values instead of preference values themselves. That is, each user's
preferences are sorted and then assign a rank as their preference value,
with 1 being assigned to the least preferred item.
@marcelcaraciolo
marcelcaraciolo / spearman.py
Created September 12, 2011 02:21
spearman coefficient
import datetime
import sys
import random
def _rank_dists(ranks1, ranks2):
"""Finds the difference between the values in ranks1 and ranks2 for keys
present in both dicts. If the arguments are not dicts, they are converted
from (key, rank) sequences.
"""
# BM25F Model
def bm25(idf, tf, fl, avgfl, B, K1):
# idf - inverse document frequency
# tf - term frequency in the current document
# fl - field length in the current document
# avgfl - average field length across documents in collection
# B, K1 - free paramters
return idf * ((tf * (K1 + 1)) / (tf + K1 * (1 - B + B * (fl / avgfl))))
@marcelcaraciolo
marcelcaraciolo / setup.py
Created September 17, 2011 04:03
Pyx Cython Setup.py example
# setup.py
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
# for notes on compiler flags see:
# http://docs.python.org/install/index.html
setup(
cmdclass = {’build_ext’: build_ext},
ext_modules = [Extension("calculate_z", ["calculate_z.pyx"])]
)
@marcelcaraciolo
marcelcaraciolo / fatorial.c
Created September 17, 2011 12:44
fatorial
#include <stdio.h>
int fatorial(int n){
if (n == 1) {
return 1;
}