Kaggle competition: Recommender Systems
<SOS> metric: categorizationaccuracy Kaggle data source: recommender-systems<train_model>
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)
rfr = RandomForestRegressor(max_depth=5, n_estimators=10)
 rfr.fit(X_train_tfidf, y_train_le )<categorify>
total.sort_values('user_id' )
pca = PCA(n_components=1000)
 X_train_pca = pca.fit_transform(X_train_tfidf_pd)
 X_test_pca = pca.transform(X_test_tfidf_pd)
 X_train_pca_pd = pd.DataFrame(X_train_pca )<compute_train_metric>
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
<save_to_csv>
ratings=datacoo
TFIDFRecommender, bm25_weight)
 log = logging.getLogger("implicit")
SVC_tuned_clf = SVC(kernel='rbf', **best_params)
 SVC_tuned_clf.fit(X_train_pca, y_train_le)
 y_true_pred_le = SVC_tuned_clf.predict(X_test_pca ).astype('int')
 y_true_pred = le.inverse_transform(y_true_pred_le)
 X_true_test_withpred = pd.DataFrame({'id': df_test_0.id, 'cuisine': y_true_pred})
 X_true_test_withpred.to_csv('svc_gscv_prediction.csv', mode='w', index=False )<split>
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()
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)
X_train_0, X_test_0, y_train_0, y_test_0 = model_selection.train_test_split(X_train_tfidf, y_train_mlb, test_size=0.2)
 num_cuisines = y_train_mlb.shape[1]
num_feats = X_train_tfidf.shape[1]<choose_model_class>
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)
inputs = keras.Input(shape=(num_feats,))
X = keras.layers.Dense(2000, activation=None )(inputs)
 X = keras.layers.BatchNormalization()(X)
 X = keras.layers.Activation('relu' )(X)
 X = keras.layers.Dropout(0.8 )(X)
 X = keras.layers.Dense(2000, activation=None )(inputs)
 X = keras.layers.BatchNormalization()(X)
 X = keras.layers.Activation('relu' )(X)
 X = keras.layers.Dropout(0.8 )(X)
 X = keras.layers.Dense(num_cuisines, activation=None )(X)
 X = keras.layers.BatchNormalization()(X)
 Y = keras.layers.Activation('softmax' )(X)
 model = keras.models.Model(inputs=inputs, outputs=Y)
 model.compile(optimizer=keras.optimizers.Adam(lr=0.001), loss='categorical_crossentropy',
metrics=['accuracy'])
 cb = keras.callbacks.ModelCheckpoint('cooking.weights.best.hdf5', monitor='val_acc', verbose=1, save_best_only=True, mode='max')
 es = keras.callbacks.EarlyStopping(monitor='val_loss', patience=4)
 model.fit(X_train_0, y_train_0, epochs=20, batch_size=64, validation_data=(X_test_0, y_test_0), callbacks=[cb, es] )<load_from_disk>
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)
<string_transform><EOS> 
test[['user_id','predictions']].to_csv('submit.csv',index=False )