您的位置:首页 > 编程语言 > Python开发

ubuntu14.04下python2.7推荐系统Crab搭建

2015-05-16 08:35 706 查看
本文欢迎转载,但转载请标注作者及出处。

      一直在寻找开源的python推荐系统源码,偶然机会接触到crab,Crab是基于Python开发的开源推荐软件,它提供了一些常用的推荐算法,例如协同过滤(CF)、Slope
One等,并且自带了几个数据集,非常方便。


   首先介绍一下我的环境,我的Linux系统是ububtu14.04安装的python2.7.6,若想成功运行Crab比较繁琐的部分是需要安装一些依赖的库,下面是我的整个安装流程,也包括我在安装过程中出现的问题和解决方法,希望对有同样需要的人有所帮助。

系统的Tutorial可以看这里:

http://muricoca.github.io/crab/

1.库安装

在安装crab前需要安装numpy,SciPy,setuptools,scikits.learn,python
development headers和C++编译器。Unbuntu系统下用root权限执行:

sudo apt-get install python-dev python-numpy python-numpy-dev python-setuptools python-numpy-dev python-scipy libatlas-dev g++

为了运行代码,还需要安装matplotlib:

sudo apt-get install python-matplotlib


在这里为了检测以上几个库是否成功安装,我们先进行测试,为后续做保障。(此处参考博客http://blog.csdn.net/i_with_u/article/details/45460661)

import numpy as np
import matplotlib.pyplot as plt

X = np.arange(-5.0, 5.0, 0.1)
Y = np.arange(-5.0, 5.0, 0.1)
x, y = np.meshgrid(X, Y)
f = 17 * x ** 2 - 16 * np.abs(x) * y + 17 * y ** 2 - 225
fig = plt.figure()
cs = plt.contour(x, y, f, 0, colors = 'r')
plt.show()



如果出现以上心形,意味着以上库已经安装正确。否者,根据错误提示,重新安装库。

为了安装scikits.learn最新的稳定版,可以通过使用PIP或easy_install的安装:

sudo pip install -U scikits.learn


or:

sudo easy_install -U scikits.learn


进入python交互环境输入:

import sklearn.svm
如果没有红色错误,表示安装成功。

Nose安装

sudo easy_install nose

检查

import nose
result = nose.run
print result ()
如果返回True或False,意味着安装完成。以后,在Python开发可用使用nose 做单元测试。

Crab

接下来就是最重要的crab的安装了

这里建议采用去官网下载安装包进行下载安装,官网下载下载:https://github.com/muricoca/crab

解压后,cmd到该文件夹下,输入命令:

python setup.py install  

来安装该库。接着出入命令:将该源码升级到最新版本。

上面的库安装部分就完成了,其实也没那么繁琐对吧~接下来就对crab进行测试吧

在python交互环境下输入一下代码

from scikits.crab import datasets
movies = datasets.load_sample_movies()
songs = datasets.load_sample_songs()


如果没有错误,意味着安装已经基本完成!
接下来进行进一步检测

(注:此处代码来自深蓝苹果的博客:http://my.oschina.net/kakablue/blog/260749)


#!/usr/bin/env python
#coding=utf-8

def base_demo():
# 基础数据-测试数据
from scikits.crab import datasets
movies = datasets.load_sample_movies()
#print movies.data
#print movies.user_ids
#print movies.item_ids

#Build the model
from scikits.crab.models import MatrixPreferenceDataModel
model = MatrixPreferenceDataModel(movies.data)

#Build the similarity
# 选用算法 pearson_correlation
from scikits.crab.metrics import pearson_correlation
from scikits.crab.similarities import UserSimilarity
similarity = UserSimilarity(model, pearson_correlation)

# 选择 基于User的推荐
from scikits.crab.recommenders.knn import UserBasedRecommender
recommender = UserBasedRecommender(model, similarity, with_preference=True)
print recommender.recommend(5) # 输出个结果看看效果 Recommend items for the user 5 (Toby)

# 选择 基于Item 的推荐(同样的基础数据,选择角度不同)
from scikits.crab.recommenders.knn import ItemBasedRecommender
recommender = ItemBasedRecommender(model, similarity, with_preference=True)
print recommender.recommend(5) # 输出个结果看看效果 Recommend items for the user 5 (Toby)

def itembase_demo():
from scikits.crab.models.classes import MatrixPreferenceDataModel
from scikits.crab.recommenders.knn.classes import ItemBasedRecommender
from scikits.crab.similarities.basic_similarities import ItemSimilarity
from scikits.crab.recommenders.knn.item_strategies import ItemsNeighborhoodStrategy
from scikits.crab.metrics.pairwise import euclidean_distances
movies = {
'Marcel Caraciolo': \
{'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}, \
'Paola Pow': \
{'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}, \
'Leopoldo Pires': \
{'Lady in the Water': 2.5, 'Snakes on a Plane': 3.0, 'Superman Returns': 3.5, 'The Night Listener': 4.0},
'Lorena Abreu': \
{'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}, \
'Steve Gates': \
{'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}, \
'Sheldom':\
{'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}, \
'Penny Frewman': \
{'Snakes on a Plane':4.5,'You, Me and Dupree':1.0, 'Superman Returns':4.0}, 'Maria Gabriela': {}
}
model = MatrixPreferenceDataModel(movies)
items_strategy = ItemsNeighborhoodStrategy()
similarity = ItemSimilarity(model, euclidean_distances)
recsys = ItemBasedRecommender(model, similarity, items_strategy)

print recsys.most_similar_items('Lady in the Water')
#Return the recommendations for the given user.
print recsys.recommend('Leopoldo Pires')
#Return the 2 explanations for the given recommendation.
print recsys.recommended_because('Leopoldo Pires', 'Just My Luck', 2)
#Return the similar recommends
print recsys.most_similar_items('Lady in the Water')
#估算评分
print recsys.estimate_preference('Leopoldo Pires','Lady in the Water')

base_demo()
itembase_demo()<pre name="code" class="python" style="color: rgb(51, 51, 51); line-height: 25.2000007629395px;">


在运行这段代码是出现了一个问题

No Attribute named _set_params  

这个错误的解决方法是打开错误中的提到的scikits\crab\recommenders\knn\class.py,将第138和600行的“self._set_params(**params)”替换成“self.set_params(**params)”。

再运行代码进行测试,没有错误就表示成功了!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: