<jupyter_start><jupyter_text>Analysis of the Wisconsin Breast Cancer Data<jupyter_code>%load_ext autoreload
%autoreload 2
# Import own modules
from regression_analysis.fit_model import logistic_regression, apply_logistic_regression

# Import other packages
import pandas as pd
import ipywidgets as widget
import seaborn as sns
import numpy as np
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split, GridSearchCV<jupyter_output><empty_output><jupyter_text>Data<jupyter_code># Load data
data = pd.read_csv('data_logistic_regression/data.csv', sep=',')
# Display data
data
data.info()
data.max()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 569 entries, 0 to 568
Data columns (total 32 columns):
 #   Column                   Non-Null Count  Dtype  
---  ------                   --------------  -----  
 0   id                       569 non-null    int64  
 1   diagnosis                569 non-null    object 
 2   radius_mean              569 non-null    float64
 3   texture_mean             569 non-null    float64
 4   perimeter_mean           569 non-null    float64
 5   area_mean                569 non-null    float64
 6   smoothness_mean          569 non-null    float64
 7   compactness_mean         569 non-null    float64
 8   concavity_mean           569 non-null    float64
 9   concave points_mean      569 non-null    float64
 10  symmetry_mean            569 non-null    float64
 11  fractal_dimension_mean   569 non-null    float64
 12  radius_se                569 non-null    float64
 13  texture_se               569 non-null    float64
 14  perimeter_se             5[...]<jupyter_text>For the design matrix we drop the column id and diagnosis from the data. The id is not important for making predictions and the diagnosis is what we want to predict.<jupyter_code># Transform diagnosis to 0 and 1
data.diagnosis = [1 if each == "M" else 0 for each in data.diagnosis]

# Create design matrix and normalise it
X = logistic_regression.design_matrix(data)
X_norm = logistic_regression.normalise_data(X)

# Define to be predicted values
y = data.diagnosis.values<jupyter_output><empty_output><jupyter_text>Fit a Model to the Data  Find the right Kernel for the Support Vector Machine Algorithm<jupyter_code># Split data on test and train datasets
x_train, x_test, y_train, y_test = train_test_split(X_norm, y, test_size=0.1)

# Use grid search to find best fit
classifier = SVC()
parameters = {'kernel':('linear', 'rbf'), 'C':[1, 10, 100]}
grid_svm = GridSearchCV(estimator=classifier, param_grid=parameters, scoring="accuracy")
grid_svm.fit(x_train, y_train)

# Print output
print(grid_svm.cv_results_)
print("Best estimator: " + str(grid_svm.best_estimator_))
print("Best Score: " + str(grid_svm.best_score_))<jupyter_output>{'mean_fit_time': array([0.00095448, 0.00118604, 0.001018  , 0.00117912, 0.00128012,
       0.00124049]), 'std_fit_time': array([1.31534871e-04, 9.92667543e-05, 1.08550007e-04, 7.01476892e-05,
       1.85307742e-04, 9.95119279e-05]), 'mean_score_time': array([0.00026803, 0.00040693, 0.000212  , 0.00034542, 0.00021763,
       0.00027986]), 'std_score_time': array([2.79503053e-05, 2.48515889e-05, 1.54806929e-06, 3.71276447e-05,
       1.02707226e-05, 8.81749140e-06]), 'param_C': masked_array(data=[1, 1, 10, 10, 100, 100],
             mask=[False, False, False, False, False, False],
       fill_value='?',
            dtype=object), 'param_kernel': masked_array(data=['linear', 'rbf', 'linear', 'rbf', 'linear', 'rbf'],
             mask=[False, False, False, False, False, False],
       fill_value='?',
            dtype=object), 'params': [{'C': 1, 'kernel': 'linear'}, {'C': 1, 'kernel': 'rbf'}, {'C': 10, 'kernel': 'linear'}, {'C': 10, 'kernel': 'rbf'}, {'C': 100, 'kernel': 'linear'}, {'C'[...]<jupyter_text>Therefore, using a linear or r  Compare Methods<jupyter_code># For all methods we apply the following parameters
test_ratio = np.round(np.arange(1,5)*0.1, 2)
k_folds = np.array([5, 10], dtype=int)
l2_lambda = np.array([0.001, 0.01, 0.1, 1.0]) #L2 regularization parameter

# Parameters for stochastic gradient descent
learn_rate = np.array([0.001, 0.01, 0.1, 1.0])
num_min_batch = np.array([1, 2, 5, 10, 32])
epochs = np.array([10, 100, 500, 1000])
methods = ["logistic_sgd", "logistic_sgd_crossvalidation", "logistic_scikit",  "logistic_scikit_crossvalidation", 
           "svm", "svm_crossvalidation"]

widget.interact(apply_logistic_regression.plot_heatmap_conf_matrix, test_ratio=test_ratio.tolist(), 
                reg_type=methods, k_fold=k_folds.tolist(), l2_lambda=l2_lambda.tolist(), 
                learn_rate=learn_rate.tolist(), num_min_batch=num_min_batch.tolist(), epoch=epochs.tolist())<jupyter_output><empty_output><jupyter_text>Confusion matrix in percentage<jupyter_code>methods = ["logistic_sgd", "logistic_sgd_crossvalidation", "logistic_scikit",  "logistic_scikit_crossvalidation", 
           "svm", "svm_crossvalidation"]

widget.interact(apply_logistic_regression.plot_heatmap_conf_matrix_percent, test_ratio=test_ratio.tolist(), 
                reg_type=methods, k_fold=k_folds.tolist(), l2_lambda=l2_lambda.tolist(), 
                learn_rate=learn_rate.tolist(), num_min_batch=num_min_batch.tolist(), epoch=epochs.tolist())<jupyter_output><empty_output><jupyter_text>Study Dependence of SGD on learning rate<jupyter_code>widget.interact(apply_logistic_regression.plot_accuracy, test_ratio=test_ratio.tolist(), 
                reg_type=widget.fixed("svm"), k_fold=k_folds.tolist(), l2_lambda=l2_lambda.tolist(), 
                learn_rate=widget.fixed(learn_rate.tolist()), num_min_batch=num_min_batch.tolist(), epoch=epochs.tolist())
widget.interact(apply_logistic_regression.plot_accuracy, test_ratio=test_ratio.tolist(), 
                reg_type=widget.fixed("logistic_sgd_crossvalidation"), k_fold=k_folds.tolist(), l2_lambda=l2_lambda.tolist(), 
                learn_rate=widget.fixed(learn_rate.tolist()), num_min_batch=num_min_batch.tolist(), epoch=epochs.tolist())<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12295488349033218, 'fraction_numerical': 0.07056684845480087, 'mean_word_length': 3.960655737704918, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '230965', 'license': 'Apache-2.0', 'n_tokens_mistral': 2379, 'n_tokens_neox': 2081, 'n_words': 452, 'path': 'regression_analysis/examples/logistic_regression_analysis.ipynb', 'repo_name': 'am-kaiser/CompSci-Project-1', 'size': '6051'}