Monday, 14 September 2015

Mahout: CityBlockSimilarity: Compute item similarity

CityBlockSimilarity is implementation of City Block distance (Also known as Manhattan distance.

if x=(a,b) and y=(c,d), the Manhatten distance between x and y is |a−c|+|b−d|.

x = (5, 3)
y = (6, 2)

Manhatten distance = |5-6| + |3-2| = 1 + 1 = 2.

x1:  1.0, 3.2, 4.8, 0.1, 3.2, 0.6, 2.2, 1.1
x2:  0.1, 5.2, 1.9, 4.2, 1.9, 0.1, 0.1, 6.0

Manhatten distance(MD) between x1 and x2 is

|1−0.1|+|3.2−5.2|+|4.8−1.9|+|0.1−4.2|+|3.2−1.9|+|0.6−0.1|+|2.2−0.1|+|1.1−6.0|

0.9+2+2.9+4.1+1.3+0.5+2.1+4.9

18.7

Following example explains how to compute ItemSimilarity between items using CityBlockSimilarity.

Let’s say I had following input data.


customer.csv
1,4,3
1,7,2
1,8,2
1,10,1
2,3,2
2,4,3
2,6,3
2,7,1
2,9,1
3,0,3
3,3,2
3,4,1
3,8,3
3,9,1
4,2,5
4,3,4
4,7,3
4,9,2
5,4,5
5,6,4
5,7,1
5,8,3


1,4,3 means customer 1 like item 4 and rated it 3
import java.io.File;
import java.io.IOException;

import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
import org.apache.mahout.cf.taste.impl.similarity.CityBlockSimilarity;
import org.apache.mahout.cf.taste.model.DataModel;

public class CityBlockSimilarityEx {
 public static String dataFile = "/Users/harikrishna_gurram/customer.csv";

 public static void main(String args[]) throws IOException, TasteException {

  DataModel model = new FileDataModel(new File(dataFile));

  CityBlockSimilarity similarity = new CityBlockSimilarity(model);

  long itemIds[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10 };

  double distance[] = similarity.itemSimilarities(1, itemIds);

  for (int i = 0; i < itemIds.length; i++) {
   System.out.println("distance between item 1 and " + itemIds[i]
     + " is " + distance[i]);
  }

 }
}

Output    
distance between item 1 and 2 is 0.5
distance between item 1 and 3 is 0.25
distance between item 1 and 4 is 0.2
distance between item 1 and 5 is 1.0
distance between item 1 and 6 is 0.3333333333333333
distance between item 1 and 7 is 0.2
distance between item 1 and 8 is 0.25
distance between item 1 and 9 is 0.25
distance between item 1 and 10 is 0.5



Prevoius                                                 Next                                                 Home

Setting MySql root password after installation

Generally if the password for MySql is not set during the installation, then these commands will help you set it externally.

$ mysql -u root
mysql> use mysql;
mysql> update user set password=PASSWORD("tiger") where User='root';
mysql> flush privileges;
mysql> quit

Now open MySQL prompt using following command.

$ mysql -u root -p
Enter password:


Enter 'tiger' as password.

Sunday, 13 September 2015

linux: tree command code

#!/bin/bash

SEDMAGIC='s;[^/]*/;|____;g;s;____|; |;g'

if [ "$#" -gt 0 ] ; then
   dirlist="$@"
else
   dirlist="."
fi

for x in $dirlist; do
     find "$x" -print | sed -e "$SEDMAGIC"
done

Mahout: Item similarity

Item-item models use rating distributions per item, not per user. Suppose there are more users than items, each item tends to have more ratings than each user, so an item's average rating usually doesn't change quickly.

Mahout provides following implementations for item-to-item collaborative filtering. Following classes implements ItemSimilarity interface. ItemSimilarity finds similarity between two items.

a.      CachingItemSimilarity
d.      FileItemSimilarity
e.      GenericItemSimilarity


Prevoius                                                 Next                                                 Home

Saturday, 12 September 2015

Mahout: TanimotoCoefficientSimilarity: Compute User similarity


TanimotoCoefficientSimilarity is based on Tanimoto coefficient, or extended Jaccard coefficient. Tanimoto coefficient is the ratio of the size of the intersection to the size of the union of their preferred items. Go through following article to know about Tanimoto coefficient.


This is used when user don’t provide preference values.

Let’s say I had following input data.

customer.csv
1,1
1,2
1,3
1,7
1,8
2,1
2,2
2,3
2,4
2,5
2,7
3,1
3,2
3,3
3,5
3,6
3,7
4,1
4,3
4,4
4,5
4,7
4,9
4,10
5,1
5,2
5,3
5,4
5,9


1,2 means customer 1 like item 1.
import java.io.File;
import java.io.IOException;

import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
import org.apache.mahout.cf.taste.impl.similarity.TanimotoCoefficientSimilarity;
import org.apache.mahout.cf.taste.model.DataModel;

public class TanimotoCoefficientSimilarityEx {
 public static String dataFile = "/Users/harikrishna_gurram/customer.csv";

 public static void main(String args[]) throws IOException, TasteException {

  DataModel model = new FileDataModel(new File(dataFile));

  TanimotoCoefficientSimilarity similarity = new TanimotoCoefficientSimilarity(
    model);

  System.out.println("Similarity between user1 and user2 is "
    + similarity.userSimilarity(1, 2));

 }
}


Output
Similarity between user1 and user2 is 0.5714285714285714
Prevoius                                                 Next                                                 Home