{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Solving MNIST digit recognition with logistic regression\n\nhttps://www.kaggle.com/competitions/digit-recognizer","metadata":{}},{"cell_type":"code","source":"import os\nimport zipfile\nfrom pathlib import Path\n\ncompetition_name = \"digit-recognizer\"\n\niskaggle = os.environ.get(\"KAGGLE_KERNEL_RUN_TYPE\", \"\")\nif iskaggle:\n    path = Path(f\"../input/{competition_name}\")\nelse:\n    import kaggle\n    \n    path = Path(competition_name)\n    if not path.exists():\n        kaggle.api.competition_download_cli(str(path))\n        zipfile.ZipFile(f\"{path}.zip\").extractall(path)","metadata":{"execution":{"iopub.status.busy":"2023-03-17T03:15:13.881167Z","iopub.execute_input":"2023-03-17T03:15:13.881635Z","iopub.status.idle":"2023-03-17T03:15:13.891261Z","shell.execute_reply.started":"2023-03-17T03:15:13.881594Z","shell.execute_reply":"2023-03-17T03:15:13.888788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nimport torch\nimport torch.nn.functional as F\n\nnp.set_printoptions(linewidth=140)\ntorch.set_printoptions(linewidth=140, sci_mode=False, edgeitems=7)\npd.set_option(\"display.width\", 140)","metadata":{"execution":{"iopub.status.busy":"2023-03-17T03:15:13.894540Z","iopub.execute_input":"2023-03-17T03:15:13.895015Z","iopub.status.idle":"2023-03-17T03:15:16.858154Z","shell.execute_reply.started":"2023-03-17T03:15:13.894976Z","shell.execute_reply":"2023-03-17T03:15:16.856800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load the data","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv(path / \"train.csv\")\ndf","metadata":{"execution":{"iopub.status.busy":"2023-03-17T03:15:16.859826Z","iopub.execute_input":"2023-03-17T03:15:16.860931Z","iopub.status.idle":"2023-03-17T03:15:20.956979Z","shell.execute_reply.started":"2023-03-17T03:15:16.860876Z","shell.execute_reply":"2023-03-17T03:15:20.955531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"80-20 split between training and validation","metadata":{}},{"cell_type":"code","source":"split_idx = int(0.8 * df.shape[0])\nsplit_idx","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-03-17T03:15:20.960588Z","iopub.execute_input":"2023-03-17T03:15:20.961134Z","iopub.status.idle":"2023-03-17T03:15:20.970886Z","shell.execute_reply.started":"2023-03-17T03:15:20.961083Z","shell.execute_reply":"2023-03-17T03:15:20.969488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels = df.iloc[:split_idx, 0]\ntrain_images = df.iloc[:split_idx, 1:]\n\nvalidation_labels = df.iloc[split_idx:, 0]\nvalidation_images = df.iloc[split_idx:, 1:]","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-03-17T03:15:20.972499Z","iopub.execute_input":"2023-03-17T03:15:20.972846Z","iopub.status.idle":"2023-03-17T03:15:20.983657Z","shell.execute_reply.started":"2023-03-17T03:15:20.972814Z","shell.execute_reply":"2023-03-17T03:15:20.982249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Divide by 255 to normalize the pixel values to be between [0, 1]\ntrain_images_normalized = torch.tensor(\n    train_images.divide(255).values,\n    dtype=torch.float32,\n)\nvalidation_images_normalized = torch.tensor(\n    validation_images.divide(255).values,\n    dtype=torch.float32,\n)\ntrain_images_normalized.shape, train_images_normalized.dtype","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-03-17T03:15:20.985479Z","iopub.execute_input":"2023-03-17T03:15:20.985959Z","iopub.status.idle":"2023-03-17T03:15:21.214062Z","shell.execute_reply.started":"2023-03-17T03:15:20.985907Z","shell.execute_reply":"2023-03-17T03:15:21.212734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images_normalized.max(), train_images_normalized.min()","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-03-17T03:15:21.215725Z","iopub.execute_input":"2023-03-17T03:15:21.216321Z","iopub.status.idle":"2023-03-17T03:15:21.554450Z","shell.execute_reply.started":"2023-03-17T03:15:21.216269Z","shell.execute_reply":"2023-03-17T03:15:21.553039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualize an example","metadata":{}},{"cell_type":"code","source":"def visualize_example(i, data_matrix, label_matrix=None):\n    img = data_matrix[i]\n    img = img.reshape((28, 28))\n    plt.imshow(img, cmap=\"gray\")\n    if label_matrix is not None:\n        plt.title(label_matrix.iloc[i])","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-03-17T03:15:21.555666Z","iopub.execute_input":"2023-03-17T03:15:21.556418Z","iopub.status.idle":"2023-03-17T03:15:21.697308Z","shell.execute_reply.started":"2023-03-17T03:15:21.556380Z","shell.execute_reply":"2023-03-17T03:15:21.695987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualize_example(8903, train_images_normalized, train_labels)","metadata":{"execution":{"iopub.status.busy":"2023-03-17T03:15:21.699181Z","iopub.execute_input":"2023-03-17T03:15:21.699683Z","iopub.status.idle":"2023-03-17T03:15:21.971889Z","shell.execute_reply.started":"2023-03-17T03:15:21.699634Z","shell.execute_reply":"2023-03-17T03:15:21.970425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Determine functions and parameters for model","metadata":{}},{"cell_type":"code","source":"train_images.shape","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-03-17T03:15:21.977701Z","iopub.execute_input":"2023-03-17T03:15:21.978133Z","iopub.status.idle":"2023-03-17T03:15:21.986966Z","shell.execute_reply.started":"2023-03-17T03:15:21.978078Z","shell.execute_reply":"2023-03-17T03:15:21.985591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_input_coeff = train_images.shape[1]\nnum_input_coeff","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-03-17T03:15:21.989078Z","iopub.execute_input":"2023-03-17T03:15:21.989840Z","iopub.status.idle":"2023-03-17T03:15:22.002746Z","shell.execute_reply.started":"2023-03-17T03:15:21.989799Z","shell.execute_reply":"2023-03-17T03:15:22.001186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# For numbers 0 - 9\noutputs = 10","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-03-17T03:15:22.004898Z","iopub.execute_input":"2023-03-17T03:15:22.005433Z","iopub.status.idle":"2023-03-17T03:15:22.015851Z","shell.execute_reply.started":"2023-03-17T03:15:22.005377Z","shell.execute_reply":"2023-03-17T03:15:22.014168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"coeffs = torch.rand(num_input_coeff, outputs) - 0.5\ncoeffs","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-03-17T03:15:22.017813Z","iopub.execute_input":"2023-03-17T03:15:22.018205Z","iopub.status.idle":"2023-03-17T03:15:22.048677Z","shell.execute_reply.started":"2023-03-17T03:15:22.018169Z","shell.execute_reply":"2023-03-17T03:15:22.047235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = train_images_normalized @ coeffs\nresult, result.shape","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-03-17T03:15:22.050094Z","iopub.execute_input":"2023-03-17T03:15:22.050515Z","iopub.status.idle":"2023-03-17T03:15:22.103415Z","shell.execute_reply.started":"2023-03-17T03:15:22.050475Z","shell.execute_reply":"2023-03-17T03:15:22.101999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def calc_predictions(coeffs, indeps):\n    r1 = indeps @ coeffs\n    lsm = F.log_softmax(r1, dim=1)\n    values, indices = torch.max(lsm, dim=1)\n    return indices","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-03-17T03:15:22.106403Z","iopub.execute_input":"2023-03-17T03:15:22.107345Z","iopub.status.idle":"2023-03-17T03:15:22.114360Z","shell.execute_reply.started":"2023-03-17T03:15:22.107290Z","shell.execute_reply":"2023-03-17T03:15:22.112886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = calc_predictions(coeffs, train_images_normalized)\npreds, preds.shape","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-03-17T03:15:22.116218Z","iopub.execute_input":"2023-03-17T03:15:22.116723Z","iopub.status.idle":"2023-03-17T03:15:22.171362Z","shell.execute_reply.started":"2023-03-17T03:15:22.116669Z","shell.execute_reply":"2023-03-17T03:15:22.170013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def calc_loss(coeffs, indeps, labels, regularization_value=0.5):\n    loss = (\n        F.cross_entropy(indeps @ coeffs, labels)\n        + regularization_value * coeffs.abs().mean()\n    )\n    return loss","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-03-17T03:15:22.172924Z","iopub.execute_input":"2023-03-17T03:15:22.173412Z","iopub.status.idle":"2023-03-17T03:15:22.181445Z","shell.execute_reply.started":"2023-03-17T03:15:22.173361Z","shell.execute_reply":"2023-03-17T03:15:22.179929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"coeffs.requires_grad_()","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-03-17T03:15:22.183441Z","iopub.execute_input":"2023-03-17T03:15:22.184610Z","iopub.status.idle":"2023-03-17T03:15:22.198792Z","shell.execute_reply.started":"2023-03-17T03:15:22.184380Z","shell.execute_reply":"2023-03-17T03:15:22.197336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels_tensor = torch.tensor(train_labels.values)\nvalidation_labels_tensor = torch.tensor(validation_labels.values)\n\ntrain_labels_tensor","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-03-17T03:15:22.200768Z","iopub.execute_input":"2023-03-17T03:15:22.201752Z","iopub.status.idle":"2023-03-17T03:15:22.211522Z","shell.execute_reply.started":"2023-03-17T03:15:22.201686Z","shell.execute_reply":"2023-03-17T03:15:22.210441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = calc_loss(coeffs, train_images_normalized, train_labels_tensor)\nloss","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-03-17T03:15:22.212807Z","iopub.execute_input":"2023-03-17T03:15:22.213786Z","iopub.status.idle":"2023-03-17T03:15:22.260461Z","shell.execute_reply.started":"2023-03-17T03:15:22.213714Z","shell.execute_reply":"2023-03-17T03:15:22.259406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss.backward()","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-03-17T03:15:22.262078Z","iopub.execute_input":"2023-03-17T03:15:22.263094Z","iopub.status.idle":"2023-03-17T03:15:22.303471Z","shell.execute_reply.started":"2023-03-17T03:15:22.263050Z","shell.execute_reply":"2023-03-17T03:15:22.302226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"coeffs.grad","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-03-17T03:15:22.304899Z","iopub.execute_input":"2023-03-17T03:15:22.305330Z","iopub.status.idle":"2023-03-17T03:15:22.315841Z","shell.execute_reply.started":"2023-03-17T03:15:22.305291Z","shell.execute_reply":"2023-03-17T03:15:22.314683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def update_coeffs(coeffs, lr):\n    coeffs -= coeffs.grad * lr\n    coeffs.grad.zero_()","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-03-17T03:15:22.317669Z","iopub.execute_input":"2023-03-17T03:15:22.318373Z","iopub.status.idle":"2023-03-17T03:15:22.324185Z","shell.execute_reply.started":"2023-03-17T03:15:22.318331Z","shell.execute_reply":"2023-03-17T03:15:22.322420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def one_epoch(images, labels, coeffs, lr):\n    loss = calc_loss(coeffs, images, labels)\n    loss.backward()\n    with torch.no_grad():\n        update_coeffs(coeffs, lr)\n    print(f\"{loss:.3f}\", end=\"; \")","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-03-17T03:15:22.325731Z","iopub.execute_input":"2023-03-17T03:15:22.326394Z","iopub.status.idle":"2023-03-17T03:15:22.336470Z","shell.execute_reply.started":"2023-03-17T03:15:22.326356Z","shell.execute_reply":"2023-03-17T03:15:22.335196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def init_coeffs():\n    return (torch.rand(num_input_coeff, outputs) - 0.5).requires_grad_()","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-03-17T03:15:22.338218Z","iopub.execute_input":"2023-03-17T03:15:22.338745Z","iopub.status.idle":"2023-03-17T03:15:22.349594Z","shell.execute_reply.started":"2023-03-17T03:15:22.338615Z","shell.execute_reply":"2023-03-17T03:15:22.348203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_input_coeff, outputs","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-03-17T03:15:22.351588Z","iopub.execute_input":"2023-03-17T03:15:22.351982Z","iopub.status.idle":"2023-03-17T03:15:22.365598Z","shell.execute_reply.started":"2023-03-17T03:15:22.351943Z","shell.execute_reply":"2023-03-17T03:15:22.364103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_model(images, labels, epochs=30, lr=0.01):\n    coeffs = init_coeffs()\n    for i in range(epochs):\n        one_epoch(images, labels, coeffs, lr)\n    coeffs.requires_grad_(False)\n    return coeffs","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-03-17T03:15:22.367625Z","iopub.execute_input":"2023-03-17T03:15:22.368548Z","iopub.status.idle":"2023-03-17T03:15:22.378637Z","shell.execute_reply.started":"2023-03-17T03:15:22.368482Z","shell.execute_reply":"2023-03-17T03:15:22.377283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"coeffs = train_model(train_images_normalized, train_labels_tensor, epochs=300, lr=1)","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-03-17T03:15:22.385237Z","iopub.execute_input":"2023-03-17T03:15:22.385955Z","iopub.status.idle":"2023-03-17T03:15:33.631746Z","shell.execute_reply.started":"2023-03-17T03:15:22.385912Z","shell.execute_reply":"2023-03-17T03:15:33.630204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"coeffs.shape","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-03-17T03:15:33.633108Z","iopub.execute_input":"2023-03-17T03:15:33.633544Z","iopub.status.idle":"2023-03-17T03:15:33.642443Z","shell.execute_reply.started":"2023-03-17T03:15:33.633497Z","shell.execute_reply":"2023-03-17T03:15:33.640599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Measuring accuracy","metadata":{}},{"cell_type":"code","source":"preds = calc_predictions(coeffs, validation_images_normalized)\npreds","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-03-17T03:15:33.644863Z","iopub.execute_input":"2023-03-17T03:15:33.645293Z","iopub.status.idle":"2023-03-17T03:15:33.685623Z","shell.execute_reply.started":"2023-03-17T03:15:33.645250Z","shell.execute_reply":"2023-03-17T03:15:33.684294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(preds == validation_labels_tensor).float().mean()","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-03-17T03:15:33.687570Z","iopub.execute_input":"2023-03-17T03:15:33.687973Z","iopub.status.idle":"2023-03-17T03:15:33.696625Z","shell.execute_reply.started":"2023-03-17T03:15:33.687934Z","shell.execute_reply":"2023-03-17T03:15:33.695383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def calc_accuracy(images, labels, coeffs):\n    preds = calc_predictions(coeffs, validation_images_normalized)\n    return (preds == validation_labels_tensor).float().mean()","metadata":{"execution":{"iopub.status.busy":"2023-03-17T03:15:33.698525Z","iopub.execute_input":"2023-03-17T03:15:33.699312Z","iopub.status.idle":"2023-03-17T03:15:33.707106Z","shell.execute_reply.started":"2023-03-17T03:15:33.699269Z","shell.execute_reply":"2023-03-17T03:15:33.705587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random_coeffs = init_coeffs()\nrandom_accuracy = calc_accuracy(\n    validation_images_normalized, validation_labels_tensor, random_coeffs\n)\nrandom_accuracy","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-03-17T03:15:33.708541Z","iopub.execute_input":"2023-03-17T03:15:33.708974Z","iopub.status.idle":"2023-03-17T03:15:33.736576Z","shell.execute_reply.started":"2023-03-17T03:15:33.708932Z","shell.execute_reply":"2023-03-17T03:15:33.735246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy = calc_accuracy(validation_images_normalized, validation_labels_tensor, coeffs)\naccuracy","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-03-17T03:15:33.738396Z","iopub.execute_input":"2023-03-17T03:15:33.739277Z","iopub.status.idle":"2023-03-17T03:15:33.758063Z","shell.execute_reply.started":"2023-03-17T03:15:33.739096Z","shell.execute_reply":"2023-03-17T03:15:33.756774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualize the coefficients\nThey look vaguely like each of the target digits.\nThe visualization in http://physics.bu.edu/~pankajm/ML-Notebooks/HTML/NB7_CVII-logreg_mnist.html is much more recognizable as the digits.","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10, 5))\n\nfor i in range(10):\n    sp = plt.subplot(2, 5, i + 1)\n    img = coeffs[:, i]\n    # img=train_images_normalized[i, :]\n    img = img.reshape((28, 28))\n    sp.set_title(i)\n    sp.imshow(img, cmap=plt.cm.Greys, interpolation=\"nearest\")\n    sp.set_xticks(())\n    sp.set_yticks(())","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-03-17T03:15:33.759844Z","iopub.execute_input":"2023-03-17T03:15:33.760522Z","iopub.status.idle":"2023-03-17T03:15:34.326788Z","shell.execute_reply.started":"2023-03-17T03:15:33.760477Z","shell.execute_reply":"2023-03-17T03:15:34.325345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Show classifier mistakes in the validation set","metadata":{}},{"cell_type":"code","source":"preds = calc_predictions(coeffs, validation_images_normalized)\nmistake_mask = preds != validation_labels_tensor\nmistake_preds = preds[mistake_mask]\nmistake_images = validation_images_normalized[mistake_mask]\nmistake_labels = validation_labels_tensor[mistake_mask]","metadata":{"execution":{"iopub.status.busy":"2023-03-17T03:15:34.328708Z","iopub.execute_input":"2023-03-17T03:15:34.329149Z","iopub.status.idle":"2023-03-17T03:15:34.348768Z","shell.execute_reply.started":"2023-03-17T03:15:34.329094Z","shell.execute_reply":"2023-03-17T03:15:34.347583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i = 11\nplt.imshow(mistake_images[i].reshape(28, 28))\nplt.title(f\"Label: {mistake_labels[i].item()} Predicted: {mistake_preds[i].item()}\")","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-03-17T03:15:34.350034Z","iopub.execute_input":"2023-03-17T03:15:34.351044Z","iopub.status.idle":"2023-03-17T03:15:34.597944Z","shell.execute_reply.started":"2023-03-17T03:15:34.351000Z","shell.execute_reply":"2023-03-17T03:15:34.596523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Test data","metadata":{}},{"cell_type":"code","source":"test_images = pd.read_csv(path / \"test.csv\")\n\ntest_images_normalized = torch.tensor(\n    test_images.divide(255).values,\n    dtype=torch.float32,\n)","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-03-17T03:15:34.599567Z","iopub.execute_input":"2023-03-17T03:15:34.600073Z","iopub.status.idle":"2023-03-17T03:15:37.014720Z","shell.execute_reply.started":"2023-03-17T03:15:34.600032Z","shell.execute_reply":"2023-03-17T03:15:37.013524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualize_example(60, test_images_normalized)","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-03-17T03:15:37.016679Z","iopub.execute_input":"2023-03-17T03:15:37.017259Z","iopub.status.idle":"2023-03-17T03:15:37.253644Z","shell.execute_reply.started":"2023-03-17T03:15:37.017204Z","shell.execute_reply":"2023-03-17T03:15:37.252223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_preds = calc_predictions(coeffs, test_images_normalized)\ntest_preds","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-03-17T03:15:37.255469Z","iopub.execute_input":"2023-03-17T03:15:37.255852Z","iopub.status.idle":"2023-03-17T03:15:37.283959Z","shell.execute_reply.started":"2023-03-17T03:15:37.255817Z","shell.execute_reply":"2023-03-17T03:15:37.283033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Submit to Kaggle","metadata":{}},{"cell_type":"code","source":"ss = pd.read_csv(path / \"sample_submission.csv\")\nss","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-03-17T03:15:37.285712Z","iopub.execute_input":"2023-03-17T03:15:37.286084Z","iopub.status.idle":"2023-03-17T03:15:37.311314Z","shell.execute_reply.started":"2023-03-17T03:15:37.286049Z","shell.execute_reply":"2023-03-17T03:15:37.309826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ss[\"Label\"] = test_preds\nss.to_csv(\"subm.csv\", index=False)\n!head subm.csv","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-03-17T03:15:37.313271Z","iopub.execute_input":"2023-03-17T03:15:37.313955Z","iopub.status.idle":"2023-03-17T03:15:38.472963Z","shell.execute_reply.started":"2023-03-17T03:15:37.313899Z","shell.execute_reply":"2023-03-17T03:15:38.471578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not iskaggle:\n    from kaggle import api\n\n    api.competition_submit(\"subm.csv\", \"logistic_regression_1\", competition_name)","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-03-17T03:15:38.474694Z","iopub.execute_input":"2023-03-17T03:15:38.475924Z","iopub.status.idle":"2023-03-17T03:15:38.481209Z","shell.execute_reply.started":"2023-03-17T03:15:38.475876Z","shell.execute_reply":"2023-03-17T03:15:38.480101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Push notebook to Kaggle","metadata":{}},{"cell_type":"code","source":"if not iskaggle:\n    from fastkaggle import push_notebook\n\n    push_notebook(\n        \"andrewc137\",\n        \"mnist-digit-recognition-using-logistic-regression\",\n        title=\"MNIST Digit Recognition Using Logistic Regression\",\n        file=\"mnist.ipynb\",\n        competition=competition_name,\n        private=False,\n        gpu=True,\n    )","metadata":{"tags":[],"execution":{"iopub.status.busy":"2023-03-17T03:15:38.483013Z","iopub.execute_input":"2023-03-17T03:15:38.483866Z","iopub.status.idle":"2023-03-17T03:15:38.496211Z","shell.execute_reply.started":"2023-03-17T03:15:38.483822Z","shell.execute_reply":"2023-03-17T03:15:38.494973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}