\renewcommand\arraystretch{1.5}
\newcommand{\myitem}[2] { \textbf{#1} & #2 \\}
\begin{table}[htbp]
\begin{tabular}{ll}
\myitem{SVM}{Support Vector Machine}
\end{tabular}%
\label{tab:Glossary}%
\end{table}%
Read more!
\renewcommand\arraystretch{1.5}
\newcommand{\myitem}[2] { \textbf{#1} & #2 \\}
\begin{table}[htbp]
\begin{tabular}{ll}
\myitem{SVM}{Support Vector Machine}
\end{tabular}%
\label{tab:Glossary}%
\end{table}%
#!/usr/bin/env python
from random import random
from math import log, exp
def normalize(P):
return [p / sum(P) for p in P]
# test data
D = [[2,10,0],[1,5,0],[0,0,8],[0,1,9]]
N = len(D)
W = len(D[0])
print 'D = ', D
#initialize
K = 5
Q = [normalize([random() for n in range(N)]) for k in range(K)]
R = [normalize([random() for w in range(W)]) for k in range(K)]
P = normalize([random() for k in range(K)])
print 'Q = ', Q
print 'R = ', R
#iteration
for loop in range(10):
# e-step
C = [[normalize([Q[k][w] * R[k][w] * P[k] for k in range(K)]) for w in range(W)] for n in range(N)]
# m-step
for k in range(K):
Q[k] = normalize([sum([C[n][w][k] * D[n][w] for w in range(W)]) for n in range(N)])
R[k] = normalize([sum([C[n][w][k] * D[n][w] for n in range(N)]) for w in range(W)])
P = normalize([sum(D[n][w] * C[n][w][k] for w in range(W) for n in range(N)) for k in range(K)])
# log likelihood
L = sum(D[n][w] * log(sum(Q[k][n] * R[k][w] for k in range(K))) for n in range(N) for w in range(W))
print 'L = ', L
'''
Loosely based on Mike Verdone's excellent twitter library:
http://github.com/sixohsix/twitter/
'''
from exceptions import Exception
from urllib import urlencode
import urllib2
try:
import json
except ImportError:
import simplejson as json
class GoogleError(Exception):
"""
Exception thrown by the search object when there is an
error interacting with google.com.
"""
pass
class GoogleImageSearch(object):
def __init__(self, endpoint, user_agent= None, cache=None):
self.endpoint = endpoint
self.cache = cache
self.user_agent = user_agent
def __call__(self, **params):
params['v']='1.0'
kwargs = dict(params)
kwargs = urlencode(kwargs)
# HTTP GET
req = urllib2.Request('%s?%s' % (self.endpoint, kwargs))
print req.get_full_url()
cache_response = True
if self.user_agent:
req.add_header('User-Agent', self.user_agent)
try:
handle = urllib2.urlopen(req)
response = handle.read()
if cache_response and self.cache != None:
self.cache.set('googleimagesearch-' + kwargs, response, time=int(time() + 60))
response = json.loads(response)
return response
except urllib2.HTTPError, e:
raise GoogleError('google imagesearch sent status %i for method: %s\ndetails: %s' % (e.code, e.fp.read()))
class GoogleImage(GoogleImageSearch):
def __init__(self, endpoint='https://ajax.googleapis.com/ajax/services/search/images', user_agent='python-googleimage/0.1', cache=None):
GoogleImageSearch.__init__(self, endpoint=endpoint, user_agent=user_agent, cache=cache)
if __name__ == '__main__':
googleimage = GoogleImage()
query = {'q':'EURECOM'}
print googleimage(**query)