推荐系统中的相似性度量
1、 皮尔逊相关系数 皮尔逊系数度量两个一一对应数列之间的线性相关程度,上述四种公式都是计算该系数方法,下面python 代码使用就是公式4。 def sim_pearson(prefs,p1,p2):
# Get the list of mutually rated items
si={}
for item in prefs[p1]:
if item in prefs[p2]: si[item]=1
# if they are no ratings in common,return 0
if len(si)==0:
return 0
# Sum calculations
n=len(si)
# Sums of all the preferences
sum1=sum([prefs[p1][it] for it in si])
sum2=sum([prefs[p2][it] for it in si])
# Sums of the squares
sum1Sq=sum([pow(prefs[p1][it],2) for it in si])
sum2Sq=sum([pow(prefs[p2][it],2) for it in si])
# Sum of the products
pSum=sum([prefs[p1][it]*prefs[p2][it] for it in si])
# Calculate r (Pearson score)
num=pSum-(sum1*sum2/n)
den=sqrt((sum1Sq-pow(sum1,2)/n)*(sum2Sq-pow(sum2,2)/n))
if den==0:
return 0
r=num/den
return r
说完皮尔逊系数的计算后我们来分析该系数的优势和不足。 2、 欧式距离定义相似度 public static double distance(double[] p1,double[] p2) {
double dotProduct = 0.0;
double lengthSquaredp1 = 0.0;
double lengthSquaredp2 = 0.0;
for (int i = 0; i < p1.length; i++) {
lengthSquaredp1 += p1[i] * p1[i];
lengthSquaredp2 += p2[i] * p2[i];
dotProduct += p1[i] * p2[i];
}
double denominator = Math.sqrt(lengthSquaredp1) * Math.sqrt(lengthSquaredp2);
// correct for floating-point rounding errors
if (denominator < dotProduct) {
denominator = dotProduct;
}
// correct for zero-vector corner case
if (denominator == 0 && dotProduct == 0) {
return 0;
}
return 1.0 - dotProduct / denominator;
}
4、 斯皮尔曼相关系数 @Override
public double userSimilarity(long userID1,long userID2) throws TasteException {
PreferenceArray xPrefs = dataModel.getPreferencesFromUser(userID1);
PreferenceArray yPrefs = dataModel.getPreferencesFromUser(userID2);
int xLength = xPrefs.length();
int yLength = yPrefs.length();
if (xLength <= 1 || yLength <= 1) {
return Double.NaN;
}
// Copy prefs since we need to modify pref values to ranks
xPrefs = xPrefs.clone();
yPrefs = yPrefs.clone();
// First sort by values from low to high
xPrefs.sortByValue();
yPrefs.sortByValue();
// Assign ranks from low to high
float nextRank = 1.0f;
for (int i = 0; i < xLength; i++) {
// ... but only for items that are common to both pref arrays
if (yPrefs.hasPrefWithItemID(xPrefs.getItemID(i))) {
xPrefs.setValue(i,nextRank);
nextRank += 1.0f;
}
// Other values are bogus but don't matter
}
nextRank = 1.0f;
for (int i = 0; i < yLength; i++) {
if (xPrefs.hasPrefWithItemID(yPrefs.getItemID(i))) {
yPrefs.setValue(i,nextRank);
nextRank += 1.0f;
}
}
xPrefs.sortByItem();
yPrefs.sortByItem();
long xIndex = xPrefs.getItemID(0);
long yIndex = yPrefs.getItemID(0);
int xPrefIndex = 0;
int yPrefIndex = 0;
double sumXYRankDiff2 = 0.0;
int count = 0;
while (true) {
int compare = xIndex < yIndex ? -1 : xIndex > yIndex ? 1 : 0;
if (compare == 0) {
double diff = xPrefs.getValue(xPrefIndex) - yPrefs.getValue(yPrefIndex);
sumXYRankDiff2 += diff * diff;
count++;
}
if (compare <= 0) {
if (++xPrefIndex >= xLength) {
break;
}
xIndex = xPrefs.getItemID(xPrefIndex);
}
if (compare >= 0) {
if (++yPrefIndex >= yLength) {
break;
}
yIndex = yPrefs.getItemID(yPrefIndex);
}
}
if (count <= 1) {
return Double.NaN;
}
// When ranks are unique,this formula actually gives the Pearson correlation
return 1.0 - 6.0 * sumXYRankDiff2 / (count * (count * count - 1));
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |