lunedì 29 febbraio 2016

What is a model? A nice definition

Eric Evans, during this talk about Domain-Driven Design (DDD), defines nicely a model as

a system of abstractions that describes selected aspects of a domain and can be used to solve problems related to that domain

mercoledì 10 febbraio 2016

CPM tutorial

You can find a nice and practical guide to the Critical Path Method here.

JPA in Eclipse: drop database at each test

If you want to drop your database at each test launch, open the file persistence.xml (under JPA) and modify the following line:

 <property name="javax.persistence.schema-generation.database.action" value="create"/>

to become as follows:

 <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>

giovedì 10 dicembre 2015

Levenshtein distance, AKA "how to compute similarity between strings"

The Levenshtein distance is a useful distance metrics that you can use to state whether two strings are similar between each other. It computes the number of different characters of a pair of strings.
The Wikipedia page proposes two alternatives:

  1. the first one is recursive
  2. the second one is iterative
Although they are fully working, and the recursive one is elegant to be read, when I found myself in implementing it in JavaScript to compare pairs of tweet texts, the recursive one wouldn't come to a result. It took ages and did not finish the computation for the pair of provided strings!
Thus, I found this nice test case which compares the two versions (in JavaScript):

[Oh, well, why JavaScript? Tweets are collected in JSON format and stored in MongoDB, and when you are required to analyze in a quick-and-dirty manner your dataset, you can JavaScript functions directly via the MongoDB console]

martedì 1 dicembre 2015

Classification of imbalanced datasets

How to overcome these binary text classification problems?
1) Highly imbalanced dataset (less than 10% positive samples)
2) Positive class subjectivity

To overcome problem 1, one could use more refined classification systems (boosting, bagging, e.g., AdaBoost for starters), although a very large dataset (even if imbalanced) could help ease the problem. This complicates the situation "a little bit": to collect enough positive samples one has to annotate tons of instances.

To overcome problem 2, one could find a god to pray.

venerdì 20 novembre 2015

Sorting algorithms in C++ - A small introduction for newbies and wannabe programmers

Today I finished the preparation of my set of slides on sorting algorithms.
This presentation is meant to introduce sorting algorithms to the ones that:

  • do not know what a sorting algorithm is
  • want to know more details about bubble sort algorithm (less interesting...) and merge sort algorithm (...definitely interesting!)
  • want to know how to implement such algorithms in C++
  • are a little bit rusty on the sorting process
So, drink up!

domenica 8 novembre 2015

A crash course in Python - Lesson 7: Functional programming

In this post, I don't want to discuss functional programming at all, but I would like to list some of the functional tools I encountered while using Python.

Curry functions

From Wikipedia:
"Currying is the technique of translating the evaluation of a function that takes multiple arguments (or a tuple of arguments) into evaluating a sequence of functions, each with a single argument"

Suppose we have the following function:

def exp(base, power):
    return base ** power

If we want to create the function two_to_the with a single input (which is the power) and fixed base (which is 2), we may do it by simply doing the following:

def two_to_the(power):
    return exp(2, power)

The same thing can be done as follows:


from functools import partial

two_to_the = partial(exp,2)  # is a function of one variable
print two_to_the(3)  # 8

square_of = partial(exp, power=2)
print square_of(3)  # 9

Map, Filter and Reduce

We occasionally use map, reduce and filter, which provide functional alternatives to list comprehension.

Map

The map function can be used with multiple-argument function if you provide multiple lists:

def multiply(x, y):
    return x * y

products = map(multiply, [1,2], [4,5])
# [1*4, 2*5] = [4, 10]

Filter

The filter function does the work of a list-comprehension if:

def isEven(x):
    return x % 2 == 0

xs = [1,2,3,4]
x_evens = [x for x in xs if isEven(x)]  # [2,4]
x_evens = filter(isEven, xs)  # same as above
list_evener = partial(filter, isEven)  # function that filters a list
x_evens = list_evener(xs)  # again [2,4]

Reduce

The reduce function combines the first two elements of a list, then that result with the third, that result with the fourth, and so on.

xProduct = reduce(multiply, xs)  # = 1*2*3*4 = 24
listProduct = partial(reduce, multiply)  # function that reduces a list
xProduct = listProduct(xs)  # again = 24