Kaggle competition: Recommender Systems
<SOS> metric: categorizationaccuracy Kaggle data source: recommender-systems<categorify>
train=pd.read_csv('.. /input/utkml/train_final.csv')
 test=pd.read_csv('.. /input/utkml/test_final.csv')
 total=train.append(test,ignore_index=True)
vectorizer=make_pipeline(
 TfidfVectorizer() ,
FunctionTransformer(lambda x: x.astype('float16'), validate=False)
)
 X_train=vectorizer.fit_transform(train['ingredients_text'].values)
 X_test=vectorizer.transform(test['ingredients_text'].values)
 X_train.shape<categorify>
total.sort_values('user_id' )
LabelEncoder=LabelEncoder()
y_train=LabelEncoder.fit_transform(train['cuisine'].values)
 label_dict=dict(zip(LabelEncoder.classes_,LabelEncoder.transform(LabelEncoder.classes_)))
 print(label_dict )<choose_model_class>
datacol=total[['user_id','JOKE:5']]
datacol.columns=['user_id','rating']
datacol['item_id']=0
data=datacol.dropna()
for ci in range(2,141):
colnm=train.columns[ci]
datacol=total[['user_id',colnm]]
datacol.columns=['user_id','rating']
datacol['item_id']=ci-1
data=data.append(datacol.dropna())
 data
datacoo=coo_matrix(( data.rating,(data.item_id,data.user_id)))
 datacoo
def model_explore(X, y):
models = [
RandomForestClassifier(n_estimators=200, max_depth=3, random_state=0),
SVC() ,
LinearSVC() ,
MultinomialNB() ,
LogisticRegression(random_state=0),
SGDClassifier(max_iter=200),
]
CV = 5
cv_df = pd.DataFrame(index=range(CV * len(models)))
 entries = []
for model in models:
model_name = model.__class__.__name__
accuracies = cross_val_score(model, X, y, scoring='accuracy', cv=CV)
 for fold_idx, accuracy in enumerate(accuracies):
entries.append(( model_name, fold_idx, accuracy))
cv_df = pd.DataFrame(entries, columns=['model_name', 'fold_idx', 'accuracy'])
 sns.boxplot(x='model_name', y='accuracy', data=cv_df)
 sns.stripplot(x='model_name', y='accuracy', data=cv_df,
size=5, jitter=True, edgecolor="gray", linewidth=2)
 plt.show()
return cv_df<compute_train_metric>
ratings=datacoo
TFIDFRecommender, bm25_weight)
 log = logging.getLogger("implicit")
cv_baseline=model_explore(X_train, y_train)
 cv_baseline.groupby("model_name" ).accuracy.mean()<choose_model_class>
start = time.time()
output_filename='output.txt'
model_name='bpr'
min_rating=-10.0,
titles=train.columns[1:]
ratings.data[ratings.data < min_rating] = 0
ratings.eliminate_zeros()
ratings.data = np.ones(len(ratings.data))
log.info("read data file in %s", time.time() - start)
 if model_name == "als":
model = AlternatingLeastSquares()
print("weighting matrix by bm25_weight")
 ratings =(bm25_weight(ratings, B=0.9)* 5 ).tocsr()
elif model_name == "bpr":
model = BayesianPersonalizedRanking()
print("weighting matrix by bm25_weight")
 ratings =(bm25_weight(ratings, B=0.9)* 5 ).tocsr()
elif model_name == "lmf":
model = LogisticMatrixFactorization()
elif model_name == "tfidf":
model = TFIDFRecommender()
elif model_name == "cosine":
model = CosineRecommender()
elif model_name == "bm25":
model = BM25Recommender(B=0.2)
 else:
raise NotImplementedError("TODO: model %s" % model_name)
 print("training model %s", model_name)
 start = time.time()
model.fit(ratings)
 print("trained model '%s' in %s", model_name, time.time() - start)
 log.debug("calculating top movies")
 user_count = np.ediff1d(ratings.indptr)
 to_generate = sorted(np.arange(len(titles)) , key=lambda x: -user_count[x])
 print("calculating similar movies")
 with tqdm.tqdm(total=len(to_generate)) as progress:
with codecs.open(output_filename, "w", "utf8")as o:
for movieid in to_generate:
print(movieid,model.similar_items(movieid))
if ratings.indptr[movieid] != ratings.indptr[movieid + 1]:
title = titles[movieid]
for other, score in model.similar_items(movieid):
" %(title, titles[other], score))
try:
print(title,titles[other],score)
 print()
except:
print(title,other,score)
 progress.update(1)
classifier_SVC = SVC(C=200,
kernel='rbf',
degree=3,
gamma=1,
coef0=1,
shrinking=True,
tol=0.001,
probability=False,
cache_size=1000,
class_weight=None,
verbose=False,
max_iter=-1,
decision_function_shape=None,
random_state=None)
 model_SVC_OVR = OneVsRestClassifier(classifier_SVC)
 scores_SVC = cross_validate(model_SVC_OVR, X_train, y_train, cv=3)
 print("The test accuracy of SVC is {}".format(scores_SVC['test_score'].mean()))<compute_train_metric>
for xi in range(10):
recommendations = model.recommend(xi, user_items)
 print('USER',xi,train.iloc[xi].sort_values(ascending=False)[:3])
 for ri,prob in recommendations:
print('recommended ',ri,titles[ri],prob)
SGD=SGDClassifier(max_iter=1000)
 model_SGD_OVR = OneVsRestClassifier(SGD)
 scores_SGD = cross_validate(model_SGD_OVR, X_train, y_train, cv=3)
 print("The test accuracy of SGD is {}".format(scores_SGD['test_score'].mean()))<choose_model_class>
test['predictions']='np.nan'
for xi in range(len(train),len(total)) :
testxi=xi-len(train)
 testuserid=test.iloc[testxi]['user_id']
recommendations = model.recommend(testuserid, user_items)
 test.iat[testxi,141]=titles[recommendations[0][0]]
if xi/1000==int(xi/1000):
print('USER',testxi,total.iloc[xi].sort_values(ascending=False)[:3])
 for ri,prob in recommendations:
print('recommended ',ri,titles[ri],prob)
Linear_SVC=LinearSVC()
model_LinearSVC_OVR = OneVsRestClassifier(Linear_SVC)
 scores_LinearSVC = cross_validate(model_LinearSVC_OVR, X_train, y_train, cv=3)
 print("The test accuracy of LinearSVC is {}".format(scores_LinearSVC['test_score'].mean()))<compute_train_metric>
test[['user_id','predictions']].to_csv('submit.csv',index=False )