Open franziweindel opened 2 years ago
def objective(fac, reg, it, alp): model = implicit.als.AlternatingLeastSquares(factors=int(fac), regularization=reg, iterations=int(it)) alpha = int(alp)
data = (train.T * alpha)#.astype('double')
model.fit(data)
user_index = range(data.shape[0])
F1=[]
RMSE=[]
Precision=[]
#Recall=[]
NDCG=[]
percentageright=[]
for i in range(0, len(user_index)):
recommended=model.recommend(user_index[i], data[i],k)
pred=recommended[0] # vector of length k of predictions
pred_score=recommended[1] #score
true_score= sparse.find(val_user_item[i])[2] #should be the number of clicks in the test set for each item
act=val_user_item[i].indices #should be the item indicies that he clicked on
#check the number of observations in the test set and adjust them to recommendation list
while len(act)<k:
act=np.append(act, 0)
while len(true_score)<k:
true_score=np.append(true_score, 0)
f1=f1_score(act[0:k], pred[0:k], average='weighted', labels=np.unique(pred))
F1.append(f1)
rmse= mean_squared_error(true_score[0:k], pred_score[0:k])
RMSE.append(rmse)
precision= precision_score(act[0:k], pred[0:k], average='weighted', labels=np.unique(pred))
Precision.append(precision)
#recall= recall_score(act, pred, average='weighted', labels=np.unique(pred))
#Recall.append
p=np.asarray([pred_score[0:k]])
t=np.asarray([true_score[0:k]])
ndcg=ndcg_score(t,p)
NDCG.append(ndcg)
pr=sum(np.isin(act,pred))/len(test[1].indices) #P@k (precision at k): number of relevant docs in top k divided by k
percentageright.append(pr)
np.mean(F1), np.mean(RMSE), np.mean(Precision), np.mean(NDCG), np.mean(percentageright) #np.mean(NDCG),#np.mean(Recall)
s1= np.mean(F1)
s2= np.mean(RMSE)
s3= np.mean(Precision)
s4= np.mean(NDCG)
s5=np.mean(percentageright)
#want to maximize the average of all evaluation criteria
#note I can also weigh this differently, should read into Gini index and multi armed optimization as mentioned in lit review
#pymoo: Multi-objective Optimization in Python relevant article
return np.array([s1, s2, s3, s4, s5])
tuning=np.array([[2, 100], [0, 1], [0,200], [0,100]]) import mobopt as mo
Optimizer = mo.MOBayesianOpt(target=objective, NObj=5, pbounds=tuning) Init=np.array([10,0,1,5,10]) Optimizer.initialize(Init)
This is my code if it helps : )
Hi,
Can you post the full stack trace of the error?
Hi, I get the error message "TypeError: only integer scalar arrays can be converted to a scalar index" when I try to inizialize my optimizer. I belive it is because self.space.random_points(init_points) returns a list an not an array. Is there a way to fix this by changing my input arguments (Init is of array type right now as specified in the wiki)
Thanks for the help!