<jupyter_start><jupyter_text>L2 Regularized Logistic Regression Model This demo file provides an example of using my L2 Regularized Logistic Regression model built with numpy to classify tumors in the Wisconsin Breast Cancer Dataset as benign or cancerous.  This demo also includes how to implement my from-scratch K fold cross-validation method to find the optimal lambda penalty value for the regression model.- **Dataset:** https://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_breast_cancer.html- **Author:** <NAME> (<EMAIL>)- **Credits:** University of Washington DATA 558 with Zaid Harchaoui and Corinne Jones  Import Standard Scikit-Learn Functionality<jupyter_code>from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import precision_recall_fscore_support as score<jupyter_output><empty_output><jupyter_text>Import Dataset<jupyter_code>from sklearn.datasets import load_breast_cancer<jupyter_output><empty_output><jupyter_text>Import Methods for Modeling Training and Analysis<jupyter_code>from l2_regularized_logistic_regression import *<jupyter_output><empty_output><jupyter_text>Load Data and Separate Features and Targets<jupyter_code>data = load_breast_cancer()
X = data.data
y = data.target
features = data.target_names<jupyter_output><empty_output><jupyter_text>Train Test Split<jupyter_code>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0)<jupyter_output><empty_output><jupyter_text>Scale Input Data and Transform Binary Targets to [-1, 1]<jupyter_code>X_scaler = StandardScaler()
X_train = X_scaler.fit_transform(X_train)
X_test = X_scaler.transform(X_test)
y_train = transform_target(y_train)
y_test = transform_target(y_test)<jupyter_output><empty_output><jupyter_text>Train L2 Regularized Logistic Regression<jupyter_code>beta_vals = l2_log_reg(X_train, y_train, lambda_penalty=1.0, eps=0.001, v=0)<jupyter_output><empty_output><jupyter_text>Visualize Value of Object from Each Iteration of Gradient Descent<jupyter_code>plot_objective_values(X_train, y_train, beta_vals, lambda_penalty=1.0)<jupyter_output><empty_output><jupyter_text>Predict with Each Coefficient Vector on Train and Test<jupyter_code>train_pred_vals = predict_all(beta_vals, X_train)
test_pred_vals = predict_all(beta_vals, X_test)<jupyter_output><empty_output><jupyter_text>Visualize Misclassification Error at Each Step of Gradient Descent<jupyter_code>plot_errors(train_pred_vals, test_pred_vals, y_train, y_test, labels=['Train Set', 'Test Set'])<jupyter_output><empty_output><jupyter_text>Use K Fold Cross Validation to Find the Optimal Lambda Value<jupyter_code>lambdas = [0.001, 0.01, 0.1, 1, 10, 100, 1000]
mean_test_errors = k_fold_crossval(X_train, y_train, lambdas, k=5, eps=0.001)
best_lambda = lambdas[np.argmin(mean_test_errors)]
print('Best lambda: {}'.format(best_lambda))<jupyter_output>Best lambda: 0.001<jupyter_text>Retrain L2 Regularized Logistic Regression with Optimal Lambda<jupyter_code>beta_vals = l2_log_reg(X_train, y_train, lambda_penalty=best_lambda, eps=0.001, v=0)<jupyter_output><empty_output><jupyter_text>Predict with Each Coefficient Vector on Train and Test<jupyter_code>train_pred_vals = predict_all(beta_vals, X_train)
test_pred_vals = predict_all(beta_vals, X_test)<jupyter_output><empty_output><jupyter_text>Visualize Misclassification Error at Each Step of Gradient Descent<jupyter_code>plot_errors(train_pred_vals, test_pred_vals, y_train, y_test, labels=['Train Set', 'Test Set'])<jupyter_output><empty_output><jupyter_text>Calculate Precision and Recall with the Final Beta from Training<jupyter_code>train_precision, train_recall, train_fscore, train_support = score(y_train, train_pred_vals[-1])
val_precision, val_recall, val_fscore, val_support = score(y_test, test_pred_vals[-1])

pr_df = pd.DataFrame({'Class': list(range(0, 2)),
                      'TrainPrecision': train_precision,
                      'TrainRecall': train_recall,
                      'ValidationPrecision': val_precision,
                      'ValidationRecall': val_recall})
pr_df<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup