<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Let's look at a traditional logistic regression model for some mildly complicated data.
Step1:  Another pair of metrics
Step2:  F1 is the harmonic mean of precision and recall
<ASSISTANT_TASK:>
Python Code:
# synthetic data
X, y = make_classification(n_samples=10000, n_features=50, n_informative=12,
                           n_redundant=2, n_classes=2, random_state=0)
# statsmodels uses logit, not logistic
lm = sm.Logit(y, X).fit()
results = lm.summary()
print(results)
# hard problem
lm = sm.Logit(y, X).fit(maxiter=1000)
results = lm.summary()
print(results)
Xtr, Xte, ytr, yte = train_test_split(X, y, test_size=0.2, random_state=0)
# 'null' prediction
print(np.sum(yte) / len(yte))
null_preds = np.ones(len(yte))
print('{:.3f}'.format(accuracy_score(yte, null_preds)))
# linear model - logistic regression
lm = LogisticRegression().fit(Xtr, ytr)
lm.coef_
figsize(12, 6)
plt.scatter(range(len(lm.coef_[0])), lm.coef_[0])
plt.xlabel('predictor')
plt.ylabel('coefficient');
preds = lm.predict(Xte)
prob_preds = lm.predict_proba(Xte)
print(preds[:5])
print(prob_preds[:5])
accuracy_score(yte, preds)
import pandas as pd
pd.DataFrame(confusion_matrix(yte, preds)).apply(lambda x: x / sum(x), axis=1)
def plot_roc(actual, predicted):
    fpr, tpr, thr = roc_curve(actual, predicted)
    roc_auc = auc(fpr, tpr)
    # exercise: add code to color curve by threshold value
    figsize(12, 8)
    plt.plot(fpr, tpr, label='ROC curve (area = %0.2f)' % roc_auc)
    plt.plot([0, 1], [0, 1], 'k--')
    plt.xlim([0.0, 1.0])
    plt.ylim([0.0, 1.05])
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.title('ROC (Class 1)')
    plt.legend(loc="lower right");
    return
plot_roc(yte, null_preds)
plot_roc(yte, prob_preds[:,1])
# more synthetic data
Xub, yub = make_classification(n_samples=10000, n_features=50, n_informative=12,
                               n_redundant=2, n_classes=2, weights=(0.99, 0.01), random_state=0)
np.sum(yub)
Xtrub, Xteub, ytrub, yteub = train_test_split(Xub, yub, test_size=0.2, random_state=0)
lm = LogisticRegression().fit(Xtrub, ytrub)
accuracy_score(yteub, lm.predict(Xteub))
plot_roc(yteub, lm.predict_proba(Xteub)[:,1])
Explanation: Let's look at a traditional logistic regression model for some mildly complicated data.
End of explanation
# using data from balanced classes
prec, rec, thresh = precision_recall_curve(yte, prob_preds[:,1])
figsize(12, 6)
plt.plot(rec, prec, label='AUC={0:0.2f}'.format(average_precision_score(yte, prob_preds[:,1])))
plt.title('Precision-Recall')
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.legend(loc='best');
# classification_report
print(classification_report(yte, preds))
Explanation: Another pair of metrics: Precision and Recall:
These are sometimes also plotted against each other:
End of explanation
# if time - l1 vs l2 penalty
lm = LogisticRegression(penalty='l1').fit(Xtr, ytr)
plt.scatter(range(len(lm.coef_[0])), lm.coef_[0]);
Explanation: F1 is the harmonic mean of precision and recall: 
$$F1 = \frac{2\cdot precision\cdot recall}{precision + recall}.$$
End of explanation
<END_TASK>