本文整理匯總了Python中recsys.datamodel.data.Data類的典型用法代碼示例。如果您正苦於以下問題:Python Data類的具體用法?Python Data怎麽用?Python Data使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Data類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _convert_hash
def _convert_hash(self, dataset):
data = Data()
for key in dataset:
record = dataset[key]
batch = [(record[k], key, k) for k in record]
data.set(batch, extend=True)
return data
示例2: train_and_save
def train_and_save(filename):
step = filename.split('.')[-1]
data = Data()
format = {'col': 1, 'row': 0, 'value': 2, 'ids': 'str'}
data.load(filename, sep='::', format=format)
train, test = data.split_train_test(percent=80)
try:
svd = SVD('svdn_model_{step}.zip'.format(step=step))
print('Already exists: svdn_model_{step}.zip'.format(step=step))
except:
svd = SVD()
svd.set_data(train)
svd.compute(
k=100,
min_values=2,
pre_normalize=False,
mean_center=True,
post_normalize=True,
savefile='svdn_model_{step}'.format(step=step)
)
print('Saved svdn_model_{step}.zip'.format(step=step))
示例3: ex1
def ex1(dat_file='./ml-1m/ratings.dat',
pct_train=0.5):
data = Data()
data.load(dat_file, sep='::', format={'col':0, 'row':1, 'value':2,'ids':int})
# create train/test split
train, test = data.split_train_test(percent=pct_train)
# create svd
K=100
svd = SVD()
svd.set_data(train)
svd.compute(k=K, min_values=5, pre_normalize=None, mean_center=True, post_normalize=True)
# evaluate performance
rmse = RMSE()
mae = MAE()
for rating, item_id, user_id in test.get():
try:
pred_rating = svd.predict(item_id, user_id)
rmse.add(rating, pred_rating)
mae.add(rating, pred_rating)
except KeyError:
continue
print 'RMSE=%s' % rmse.compute()
print 'MAE=%s' % mae.compute()
示例4: Algorithm
class Algorithm(object):
def __init__(self):
self._data = Data()
def __repr__(self):
s = '%d rows.' % len(self.get_data())
if len(self.get_data()):
s += '\nE.g: %s' % str(self.get_data()[0])
return s
def __len__(self):
return len(self.get_data())
def get_data(self):
return self._data
def set_data(self, data):
self._data = data
def add_tuple(self, tuple):
self.get_data().add_tuple(tuple)
def load_data(self, filename, sep='\t', format={'value':0, 'row':1, 'col':2}):
self._data.load_file(filename, sep, format)
def compute(self):
if not self._data.get():
raise ValueError('No data set. Matrix is empty!')
示例5: get_preference
def get_preference(user_List):
#generate list of users
preference_dict={}
user_map={}
data = Data() #saving rating data
i=1
for user in user_List:
user_id=(str(user))
url = "http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?\
key=147CBF377C6B648EC3DC73499CE73D32&steamid="+user+"&format=json"
response = urllib2.urlopen(url)
owned_gameData = json.loads(response.read().decode('utf-8-sig'))
user_Pref={}
#print (user)
try:
if owned_gameData['response']['game_count']!=0:
user_Pref={}
for games in owned_gameData['response']['games']:
if games['playtime_forever']>0:
user_Pref[games['appid']]= math.log(games['playtime_forever'])
data.add_tuple((math.log(games['playtime_forever'], 10), games['appid'], i))
user_map[i]=user
except:
continue
i=i+1
preference_dict[user]=user_Pref
data.save('rating.dat')
示例6: test_data_extend
def test_data_extend():
dataset = [(1,2,3), (4,5,6)]
dataset2 = [(7,8,9), (10,11,12)]
data = Data()
data.set(dataset)
assert_equal(len(data), 2)
data.set(dataset2, extend=True)
assert_equal(len(data), 4)
示例7: load_ratings
def load_ratings(filename):
""" Load ratings
"""
data = Data()
format = {'col':0, 'row':1, 'value':2, 'ids': 'int'}
data.load(filename, sep=',', format=format)
return data
示例8: build_model
def build_model(self,uids,kn):
data = Data()
for uid,songs in uids.items():
for song in songs:
data.add_tuple((1,song,uid))
svd = SVD()
svd.set_data(data)
svd.compute(k=kn,min_values=1)
self.model = svd
示例9: getAverageRating
def getAverageRating(ITEMID):
averageRating = 0
totalUsers = 0
data = Data()
data.load('./data/movielens/ratings.dat', sep='::', format={'col':0, 'row':1, 'value':2, 'ids':int})
for rating, item_id, user_id in data.get():
if(item_id == ITEMID):
totalUsers += 1
averageRating += rating
print averageRating/totalUsers
示例10: calculate_stats_features
def calculate_stats_features(pct_train):
dat_file='feature_matrix.csv'
data = Data()
data.load(dat_file, sep=',', format={'col':0, 'row':1, 'value':2,'ids':int})
train, test = data.split_train_test(percent=pct_train)
K=100
svd = SVD()
svd.set_data(train)
svd.compute(k=K, min_values=0, pre_normalize=None, mean_center=False,
post_normalize=False)
return svd,train,test
示例11: get_data_model_matrix
def get_data_model_matrix(data):
"""
This method process raw data and store rating/users/movies in a matrix <value/row/column> respectively
using recsys library
:return: data object (recsys.datamodel.Data()) )
"""
processed_data = Data()
for user, review in data.items():
for mov, rat in review.items():
processed_data.add_tuple((rat, user, mov))
return processed_data
示例12: get_friend_matrix
def get_friend_matrix(u_ids, raw_data):
idata = Data()
u_idx = 0
for u_id in u_ids:
u_idx += 1
i_idx = 0
i_ids = raw_data[u_id].keys()
for i_id in i_ids:
i_idx += 1
rate, ts = raw_data[u_id][i_id]
idata.add_tuple((float(rate),u_idx,i_idx))
return idata
示例13: prepare_data
def prepare_data(raw_data):
idata = Data()
u_idx = 0
for u_id in raw_data.keys():
i_idx = 0
u_idx += 1
pre_u_raw_data = raw_data[u_id]
for i_id in pre_u_raw_data.keys():
i_idx += 1
rate, _ = pre_u_raw_data[i_id]
idata.add_tuple((float(rate),u_idx,i_idx))
return idata
示例14: setup_svd
def setup_svd(self, vote_list):
if self.svd is None:
self.cache['svd'] = SVD()
data = Data()
for vote in vote_list:
user_id = vote[0].id
item_id = vote[1]
value = float(vote[2])
data.add_tuple((value, item_id, user_id)) # Tuple format is: <value, row, column>
self.cache['svd'].set_data(data)
self.cache['svd'].compute(k=self.k, min_values=1)
return self.svd
示例15: setUp
def setUp(self):
data = Data()
for stars, item_id, user_id in ratings:
data.add_tuple((stars, item_id, user_id))
movies = dict()
for mid, name, genres in movie_genres:
movie = Item(mid)
movie.add_data({'name': name, 'genres': genres})
movies[mid] = movie
self.ratings = data
self.movies = movies