Programming Collective Intelligence读书笔记二

这章主要讲了如何做推荐,现在推荐最常用的几种算法:Collaborative Filtering、Cluster Models、Search-Based Methods、Item-to-Item Collaborative Filtering.前两种是通过找相似的Customer,后两种通过找相似的Item.论文Amazon.com Recommendations Item-to-Item Collaborative Filtering 对这几种算法都有介绍。这章主要提了Collaborative Filtering和tem-to-Item Collaborative Filtering。 Collaborative Filtering:通过搜索大量的Customer数据集来找到那一小撮和你口味相似的。书中举了一个电影评论的例子,每个人都对一些电影进行评等级,通过这些数据来找到和你口味相似的人,以及对你没有看过的电影做推荐,并以这个例子演示了如何做推荐。

准备数据:(本笔记的代码使用ruby实现,python代码的实现见原书)

critics={
	'Lisa Rose' => {'Lady in the Water' => 2.5, 'Snakes on a Plane' => 3.5,
	'Just My Luck' => 3.0, 'Superman Returns' => 3.5, 'You, Me and Dupree' => 2.5,
	'The Night Listener' => 3.0},

	'Gene Seymour' => {'Lady in the Water' => 3.0, 'Snakes on a Plane' => 3.5,
	'Just My Luck' => 1.5, 'Superman Returns' => 5.0, 'The Night Listener'=> 3.0,
	'You, Me and Dupree' => 3.5},

	'Michael Phillips' => {'Lady in the Water' => 2.5, 'Snakes on a Plane' => 3.0,
	'Superman Returns' => 3.5, 'The Night Listener' => 4.0},

	'Claudia Puig' => {'Snakes on a Plane' => 3.5, 'Just My Luck' => 3.0,
	'The Night Listener' => 4.5, 'Superman Returns' => 4.0,
	'You, Me and Dupree' => 2.5},

	'Mick LaSalle'=> {'Lady in the Water' => 3.0, 'Snakes on a Plane' => 4.0,
	'Just My Luck' => 2.0, 'Superman Returns' => 3.0, 'The Night Listener' => 3.0,
	'You, Me and Dupree' => 2.0},

	'Jack Matthews'=> {'Lady in the Water' => 3.0, 'Snakes on a Plane' => 4.0,
	'The Night Listener'=> 3.0, 'Superman Returns'=> 5.0, 'You, Me and Dupree' => 3.5},

	'Toby' => {'Snakes on a Plane' =>4.5,'You, Me and Dupree' =>1.0,'Superman Returns' => 4.0}
}

 

定义相似度:

欧拉距离:

 

 

 

代码实现:

 

def sim_distance(prefs,person1,person2)
	si = {}
	prefs[person1].each_key do |item|
		si[item] = 1 if prefs[person2][item]
	end
	
	return 0 if si.empty?
	
	sum_of_squares = si.keys.inject(0) do |sum,item|
		sum + (prefs[person1][item] - prefs[person2][item]) ** 2
	end
  
	return 1 / (1 + sum_of_squares)
end

 

 

 Pearson Correlation Score:

 

代码实现:

 

def sim_pearson(prefs,person1,person2)
	si = {}
	prefs[person1].each_key do |item|
		si[item] = 1 if prefs[person2][item]
	end
  
	return 0 if si.empty?
  
	sum1 = si.keys.inject(0){|sum,item| sum + prefs[person1][item]}
	sum2 = si.keys.inject(0){|sum,item| sum + prefs[person2][item]}
	
	sum1Sq = si.keys.inject(0){|sum,item| sum + prefs[person1][item] ** 2}
	sum2Sq = si.keys.inject(0){|sum,item| sum + prefs[person2][item] ** 2}
	
	pSum = si.keys.inject(0){|sum,item| sum + prefs[person1][item] * prefs[person2][item]}
	num = pSum - (sum1 * sum2 / si.size)
	den = Math.sqrt((sum1Sq - sum1 ** 2 / si.size) * (sum2Sq - sum2 ** 2 / si.size))
	return (if den == 0 then 0 else num/den end)
end

 

待续

你可能感兴趣的:(算法,python,读书,Ruby)