{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"grade": false,
"locked": true,
"solution": false
}
},
"source": [
"# Logistic Regression"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"grade": false,
"locked": true,
"solution": false
}
},
"source": [
"-----------"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"grade": false,
"locked": true,
"solution": false
}
},
"source": [
"### Assignment Contents"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"grade": false,
"locked": true,
"solution": false
}
},
"source": [
"- [Question 1](#q-digit-plot)\n",
"- [Question 2](#q-digit-counts)\n",
"- [Question 3](#q-binarize)\n",
"- [Question 4](#q-count-bin)\n",
"- [Question 5](#q-dummy-conf)\n",
"- [Question 6](#q-accuracy)\n",
"- [Question 7](#q-linear)\n",
"- [Question 8](#q-linear2)\n",
"- [Question 9](#q-logreg)\n",
"- [Question 10](#q-proba)\n",
"- [Question 11](#q-logit)\n",
"- [Question 12](#q-logistic)"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"grade": false,
"locked": true,
"solution": false
}
},
"source": [
"#### EXPECTED TIME 1.5 HRS "
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"grade": false,
"locked": true,
"solution": false
}
},
"source": [
"## Activities in this Assignment\n",
"\n",
"This assignment provides an overview of *Classification* problems. You will use Scikit-Learn's implementation of Logistic Regression to evaluate a few approaches to classification using the [MNIST handwritten digit dataset](https://en.wikipedia.org/wiki/MNIST_database). Along the way, you'll get a chance to practice the skills you've developed for working with data using Numpy and Pandas to advantage.\n",
"\n",
"The primary goals are:\n",
"+ to use the Scikit-Learn, Pandas, & Numpy APIs to formulate & solve classification problems\n",
"+ to increase familiarity with confusion matrices & accuracy in the context of binary classification."
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"grade": false,
"locked": true,
"solution": false
}
},
"source": [
"---\n",
"\n",
"## Examining the Digits data set"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"nbgrader": {
"grade": false,
"locked": false,
"solution": false
}
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"# Our standard data imports\n",
"import pandas as pd\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from warnings import filterwarnings\n",
"filterwarnings('ignore')"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
".. _digits_dataset:\n",
"\n",
"Optical recognition of handwritten digits dataset\n",
"--------------------------------------------------\n",
"\n",
"**Data Set Characteristics:**\n",
"\n",
" :Number of Instances: 5620\n",
" :Number of Attributes: 64\n",
" :Attribute Information: 8x8 image of integer pixels in the range 0..16.\n",
" :Missing Attribute Values: None\n",
" :Creator: E. Alpaydin (alpaydin '@' boun.edu.tr)\n",
" :Date: July; 1998\n",
"\n",
"This is a copy of the test set of the UCI ML hand-written digits datasets\n",
"http://archive.ics.uci.edu/ml/datasets/Optical+Recognition+of+Handwritten+Digits\n",
"\n",
"The data set contains images of hand-written digits: 10 classes where\n",
"each class refers to a digit.\n",
"\n",
"Preprocessing programs made available by NIST were used to extract\n",
"normalized bitmaps of handwritten digits from a preprinted form. From a\n",
"total of 43 people, 30 contributed to the training set and different 13\n",
"to the test set. 32x32 bitmaps are divided into nonoverlapping blocks of\n",
"4x4 and the number of on pixels are counted in each block. This generates\n",
"an input matrix of 8x8 where each element is an integer in the range\n",
"0..16. This reduces dimensionality and gives invariance to small\n",
"distortions.\n",
"\n",
"For info on NIST preprocessing routines, see M. D. Garris, J. L. Blue, G.\n",
"T. Candela, D. L. Dimmick, J. Geist, P. J. Grother, S. A. Janet, and C.\n",
"L. Wilson, NIST Form-Based Handprint Recognition System, NISTIR 5469,\n",
"1994.\n",
"\n",
".. topic:: References\n",
"\n",
" - C. Kaynak (1995) Methods of Combining Multiple Classifiers and Their\n",
" Applications to Handwritten Digit Recognition, MSc Thesis, Institute of\n",
" Graduate Studies in Science and Engineering, Bogazici University.\n",
" - E. Alpaydin, C. Kaynak (1998) Cascading Classifiers, Kybernetika.\n",
" - Ken Tang and Ponnuthurai N. Suganthan and Xi Yao and A. Kai Qin.\n",
" Linear dimensionalityreduction using relevance weighted LDA. School of\n",
" Electrical and Electronic Engineering Nanyang Technological University.\n",
" 2005.\n",
" - Claudio Gentile. A New Approximate Maximal Margin Classification\n",
" Algorithm. NIPS. 2000.\n",
"Input data shape: (1797, 64)\tTarget data shape: (1797,)\n"
]
}
],
"source": [
"from sklearn.datasets import load_digits\n",
"digits = load_digits()\n",
"print(digits.DESCR)\n",
"\n",
"# Extract data and targets as Numpy arrays\n",
"X_digits, y_digits = digits.data, digits.target\n",
"print('Input data shape: {}\\tTarget data shape: {}'.format(X_digits.shape, y_digits.shape))"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"grade": false,
"locked": true,
"solution": false
}
},
"source": [
"To get a better feel of what the input data is, extract a row of 64 numbers, reshape it into an $8\\times8$ array, and examine the resulting matrix by printing the numeric values & by plotting it as an image. "
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"grade": false,
"locked": true,
"solution": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 0. 0. 2. 12. 9. 0. 0. 0.]\n",
" [ 0. 0. 11. 15. 12. 5. 0. 0.]\n",
" [ 0. 0. 15. 5. 0. 14. 0. 0.]\n",
" [ 0. 2. 15. 1. 0. 9. 7. 0.]\n",
" [ 0. 4. 10. 0. 0. 7. 8. 0.]\n",
" [ 0. 0. 12. 0. 0. 8. 10. 0.]\n",
" [ 0. 2. 15. 5. 10. 16. 1. 0.]\n",
" [ 0. 0. 5. 14. 12. 4. 0. 0.]]\n",
"y_digits[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