<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Finalize model
Step1:  Predicting top 3 categories
Top 3 categories are the top 3 categories with the highest probability.
Step2:  The function below brings all the steps above so it can be applied to the X_test set.
Step3:  The function below marks the result as positive if the correct category is within topk.
Step4:  By selecting the top 3 categories, the <u>accuracy score</u> using Logistic Regression goes from %64 to 86%.
Display example of predicted top 3 categories
Step5:  Implementation of Top 3 category recomendations
The implementation of Logistic Regression with top 3 categories would look like the mockup below
<ASSISTANT_TASK:>
Python Code:
def multiplek(y_test, topk):
    multik = []
    for i, score in enumerate(y_test):
        if y_test[i] in topk[i]:
            multik.append(y_test[i])
        else:
            multik.append(topk[i][0])
    return multik
Explanation: Finalize model: Use Logistic Regression to recommend top 3 categories
After fine tuning the parameters of each of the models it became clear that the performance of SVM is superior (F1=0.64) as compared to LR (F1=0.62) and MNB(F1=0.62). Nonetheless, the improvement of performance comes at the cost of interpretability of feature importance and a high complexity of parameters.
I will use Logistic Regression and use top k predictions to boost the accuracy of the model.
Update prediction list
If correct category is within the top k categories pick coorect category, else pick first item in top k categories.
End of explanation
pred = grid.predict(X_test.ravel())  # predicts a category
p_prob = grid.predict_proba(X_test.ravel()) # predicts the probabilities of each category
idxs = np.argsort(p_prob[0])[::-1] # sorts probabilities in descending order
print(grid.best_estimator_.named_steps["lr"].classes_[idxs][:3])
Explanation: Predicting top 3 categories
Top 3 categories are the top 3 categories with the highest probability.
End of explanation
def topk_scored(model,step_name X_test, k):
    results = []
    p_prob = model.predict_proba(X_test)
    for pred in p_prob:
        idxs = np.argsort(pred)[::-1]
        results.append(model.best_estimator_.named_steps[step_name].classes_[idxs][:k])
    return results
pps = topk_scored(grid,"lr", X_test.ravel(), 3)
Explanation: The function below brings all the steps above so it can be applied to the X_test set.
End of explanation
def scoring_multiplek(y_test, topk):
    result = 0
    l = len(y_test)
    for i, score in enumerate(y_test):
        if y_test[i] in topk[i]:
            result += 1
    return result/l
adjusted_score = scoring_multiplek(y_test.values, pps)
adjusted_score
Explanation: The function below marks the result as positive if the correct category is within topk.
End of explanation
X_test[:3]
y_test[:3]
pps[:3]
# Dataframe to review 3-category predictions
cont = list(zip(X_test,y_test.values, pps))
cols = ["Input", "Input Category", "Predicted Categories"]
results_df = pd.DataFrame(cont ,columns=cols)
results_df.head()
results_df.tail()
sc = scoring_multiplek(y_test.values, pps)
sc
Explanation: By selecting the top 3 categories, the <u>accuracy score</u> using Logistic Regression goes from %64 to 86%.
Display example of predicted top 3 categories
End of explanation
x = "new on twitter - shots of the keynote speakers, the panel members and the audiences reactions, as well as while people network. posed are fine but candid photos are preferred. the photographer will need to check in on the first floor with building security and say s/he is visiting twitter. security will send s/he to the 9th floor where they will check in with reception."
example = grid.predict_proba(x)
ex_idxs = np.argsort(example[0])[::-1]
print(grid.best_estimator_.named_steps["lr"].classes_[ex_idxs][:3])
Explanation: Implementation of Top 3 category recomendations
The implementation of Logistic Regression with top 3 categories would look like the mockup below:
<img src='../images/result.png'>
End of explanation
<END_TASK>