This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print("Hello") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# set ads | |
num_ads = 3 | |
ads = np.asarray(["ad_{}".format(i) for i in range(num_ads)]) | |
# assign random priors to contexts | |
ad_interaction_priors = np.asarray([0.1, 0.3, 0.6]) | |
user_context_priors = {context:np.random.permutation(ad_interaction_priors) for context in user_contexts} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# calculate precision-recall AUC | |
auc_prc = auc(recall, precision) | |
print(auc_prc) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
precision, recall, thresholds = precision_recall_curve(y_test, y_pred_proba) | |
plt.figure(figsize=(10,8)) | |
plt.plot([0, 1], [0.5, 0.5],'k--') | |
plt.plot(recall, precision, label='Knn') | |
plt.xlabel('recall') | |
plt.ylabel('precision') | |
plt.title('Knn(n_neighbors=5) PRC curve') | |
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
roc_auc_score(y_test, y_pred_proba) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
plt.figure(figsize=(10,8)) | |
plt.plot([0,1],[0,1],'k--') | |
plt.plot(fpr,tpr, label='Knn') | |
plt.xlabel('FPR') | |
plt.ylabel('TPR') | |
plt.title('Knn(n_neighbors=5) ROC curve') | |
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
y_pred_proba = knn.predict_proba(X_test)[:,1] | |
fpr, tpr, thresholds = roc_curve(y_test, y_pred_proba) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print(classification_report(y_test,y_pred)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
y_pred = knn.predict(X_test) | |
confusion_matrix(y_test,y_pred) | |
pd.crosstab(y_test, y_pred, rownames=['Actual'], colnames=['Predicted'], margins=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Setup a knn classifier with k neighbors | |
knn = KNeighborsClassifier(8) | |
knn.fit(X_train,y_train) | |
knn.score(X_test,y_test) |