Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

AAB14.ipynb { "cells": [ { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "grade": false, "locked": true, "solution": false } }, "source": [ "# Logistic...

1 answer below »
AAB14.ipyn
{
"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": null,
"metadata": {
"collapsed": true,
"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": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from sklearn.datasets import load_digits\n",
"digits = load_digits()\n",
"print(digits.DESCR)\n",
"\n",
"# Extract data and targets as Numpy a
ays\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$ a
ay, and examine the resulting matrix by printing the numeric values & by plotting it as an image. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"deletable": false,
"editable": false,
"nbgrader": {
"grade": false,
"locked": true,
"solution": false
}
},
"outputs": [],
"source": [
"k = 130\n",
"im = X_digits[k].reshape(8, 8)\n",
"print(im)\n",
"plt.imshow(im)\n",
"plt.axis('off')\n",
"print('y_digits[{}] = {}'.format(k, y_digits[k]))"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"grade": false,
"locked": true,
"solution": false
}
},
"source": [
"Apparently, row 130 of the matrix `X` (remember, indexed from zero, this is the 131st row from the top), when reshaped, yields the image above. The co
esponding entry of the target vector `y` is $0$ which means that this image is intended to represent the numeral $0$. Whether this is obvious depends on the handwriting of the original author."
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"grade": false,
"locked": true,
"solution": false
}
},
"source": [
"[Back to top](#Assignment-Contents)\n",
"a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"grade": false,
"locked": true,
"solution": false
}
},
"source": [
"---\n",
"#### Question 1\n",
"\n",
"Your task is extract the 1674th row from the matrix `X_digits` and determine which numeral that image co
esponds to.\n",
"\n",
"+ Assign the row extracted to the identifier `ans_1_row`.\n",
"+ Assign the associated digit (as an integer) to `ans_1_digit`.\n",
"+ Remember, in extracting a row from `X_digits`, Python a
ays use indexing from 0."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"### GRADED\n",
"### Plot the image from 1674th row of the matrix X_digits.\n",
"### What numeral does this image represent? \n",
"### Assign the row as ans_1_row and the associated digit (as an integer)\n",
"### to the identifier ans_1_digit.\n",
"## e.g., ans_1_digit = 9\n",
"### YOUR SOLUTION HERE:\n",
"ans_1_row = ...\n",
"ans_1_digit = ...\n",
"### For verifying answer:\n",
"image_digit = ans_1_row.reshape(8, 8)\n",
"print('The
Answered 1 days After Jun 30, 2021

Solution

Pritam Kumar answered on Jul 01 2021
159 Votes
{
"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. Ga
is, 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 a
ays\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$ a
ay, 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[130] = 0\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAPUAAAD4CAYAAAA0L6C7AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAABE5JREFU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ba611NLp2PC81xIgaYkQNMaKGGFFDjKghRtQQI2qIETXEiBpiRA0xooYYUUOMqCFG1BAjaogRNcSIGmJEDTGihhhRQ4yoIUbUECNqiBE1xIgaYkQNMaf+ltbR4a9jWwfX525
XWtQ9vjW19/cfFsa2X33t3bOvs/dtjWyeFlxpiRA0xooYYUUOMqCFG1BAjaogRNcSIGmJEDTGihhhRQ4yoIUbUECNqiBE1xIgaYkQNMaKGGFFDjKghRtQQI2qIETXEiBpiRA0xW5vN5kl/B+AR8lJDjKghRtQQI2qIETXEiBpiRA0xooYYUUOMqCFG1BAjaogRNcSIGmJEDTGihhhRQ4yoIUbUECNqiBE1xIgaYkQNMf8B1alGkrGFyOQAAAAASUVORK5CYII=\n",
"text/plain": [
"
"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"k = 130\n",
"im = X_digits[k].reshape(8, 8)\n",
"print(im)\n",
"plt.imshow(im)\n",
"plt.axis('off')\n",
"print('y_digits[{}] = {}'.format(k, y_digits[k]))"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"grade": false,
"locked": true,
"solution": false
}
},
"source": [
"Apparently, row 130 of the matrix `X` (remember, indexed from zero, this is the 131st row from the top), when reshaped, yields the image above. The co
esponding entry of the target vector `y` is $0$ which means that this image is intended to represent the numeral $0$. Whether this is obvious depends on the handwriting of the original author."
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"grade": false,
"locked": true,
"solution": false
}
},
"source": [
"[Back to top](#Assignment-Contents)\n",
"a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"grade": false,
"locked": true,
"solution": false
}
},
"source": [
"---\n",
"#### Question 1\n",
"\n",
"Your task is extract the 1674th row from the matrix `X_digits` and determine which numeral that image co
esponds to.\n",
"\n",
"+ Assign the row extracted to the identifier `ans_1_row`.\n",
"+ Assign the associated digit (as an integer) to `ans_1_digit`.\n",
"+ Remember, in extracting a row from `X_digits`, Python a
ays use indexing from 0."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The 1674th row co
esponds to this image of the digit 6.\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAPgAAAD8CAYAAABaQGkdAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAACvxJREFUeJzt3V2MXHUZx/Hfj21haXlLBAzpFgoGG8GXljQ1pIqxVVOkAU28aBUSiclqCASiCQHvvDB6I8ELISEFJKGCWiASUotEQGyASt9Utts2dQG7FmiJQaCGLi2PFztNalkyZzvnbZ58P0nDzu5k/89QvpyzszPn74gQgJxOaHoAANUhcCAxAgcSI3AgMQIHEiNwIDECBxIjcCAxAgcSm1HFNz3RJ8WgZlfxrRvlGZX86/pQp3x8ora13hk7uba14t2Dta2V1bs6oIk46G73q+S/2EHN1me9rIpv3aiBM8+udb3P/fqV2tbasGpBbWsdHtlZ21pZbYw/Frofp+hAYgQOJEbgQGIEDiRG4EBiBA4kRuBAYgQOJFYocNvLbe+0vdv2LVUPBaAcXQO3PSDpF5Iul3SRpFW2L6p6MAC9K3IEXyxpd0SMRcSEpAclXVXtWADKUCTwOZL2HHV7vPM5AC1X5M0mU71j5QMXU7c9LGlYkgY1q8exAJShyBF8XNLco24PSdp77J0i4q6IWBQRi2bqpLLmA9CDIoG/IOlC2+fbPlHSSkmPVjsWgDJ0PUWPiEO2r5f0uKQBSfdExEjlkwHoWaELPkTEOknrKp4FQMl4JRuQGIEDiRE4kBiBA4kROJAYgQOJETiQGIEDidW7F0+fG/3JubWuN3zys7WttUH17WyC+nAEBxIjcCAxAgcSI3AgMQIHEiNwIDECBxIjcCAxAg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Rvsv9K+kjFswAoGa9kAxIjcCAxAgcSI3AgMQIHEiNwIDECBxIjcCAxR0T539TeL2m6byk9U9IbpQ/TDlkfG4+rOedFxFnd7lRJ4MfD9qaIWNT0HFXI+th4XO3HKTqQGIEDibUp8LuaHqBCWR8bj6vlWvMzOIDytekIDqBkrQjc9nLbO23vtn1L0/OUwfZc20/ZHrU9YvvGpmcqk+0B21ttP9b0LGWyfYbttbZ3dP7uLm16pl40foreudb6Lk1eMWZc0guSVkXE9kYH65HtcySdExF
J8qabOk
X74zrC9vclLZJ0WkSsaHqesti+T9KfI2J150KjsyLizabnOl5tOIIvlrQ7IsYiYkLSg5KuanimnkXEqxGxpfPx25JGJc1pdqpy2B6SdIWk1U3PUi
p0m6TNLdkhQRE/0ct9SOwOdI2nPU7XElCeEI2/MkLZS0sdlJSnO7pJslvd/0ICW7QNJ+Sfd2fvxYbXt200P1og2Be4rPpXlq3/Ypkh6SdFNEvNX0PL2yvULSvojY3PQsFZgh6RJJd0bEQkkHJPX1c0JtCHxc0tyjbg9J2tvQLKWyPVOTca+JiCxXpF0i6U
L2vyx6mltu9vdqTSjEsaj4gjZ1prNRl832pD4C9IutD2+Z0nNVZKerThmXpm25r8WW40Im5rep6yRMStETEUEfM0+Xf1ZERc3fBYpYiI1yTtsT2/86llkvr6SdHp7k1Wuog4ZPt6SY9LGpB0T0SMNDxWGZZIukbS321v63zuhxGxrsGZ0N0NktZ0DjZjkq5teJ6eNP5rMgDVacMpOoCKEDiQGIEDiRE4kBiBA4kROJAYgQOJETiQ2P8AKbKXGXV7MpYAAAAASUVORK5CYII=\n",
"text/plain": [
"
"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"### GRADED\n",
"### Plot the image from 1674th row of the matrix X_digits.\n",
"### What numeral does this image represent? \n",
"### Assign the row as ans_1_row and the associated digit (as an integer)\n",
"### to the identifier ans_1_digit.\n",
"## e.g., ans_1_digit = 9\n",
"### YOUR SOLUTION HERE:\n",
"ans_1_row = X_digits[1673]\n",
"ans_1_digit = 6\n",
"### For verifying answer:\n",
"image_digit = ans_1_row.reshape(8, 8)\n",
"print('The 1674th row co
esponds to this image of the digit {}.'.format(ans_1_digit))\n",
"plt.imshow(image_digit);"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 0. 0. 1. 13. 2. 0. 0. 0.]\n",
" [ 0. 0. 9. 14. 2. 0. 0. 0.]\n",
" [ 0. 3. 16. 7. 0. 0. 0. 0.]\n",
" [ 0. 3. 16. 7. 0. 0. 0. 0.]\n",
" [ 0. 5. 16. 16. 8. 1. 0. 0.]\n",
" [ 0. 3. 15. 11. 14. 13. 2. 0.]\n",
" [ 0. 0. 10. 16. 10. 16. 15. 0.]\n",
" [ 0. 0. 1. 10. 14. 12. 7. 0.]]\n",
"y_digits[1673] = 6\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAPUAAAD4CAYAAAA0L6C7AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAABCpJREFUeJzt3aGLHnQcx/HfnQua1jQIN8QgMhwzihODCAaLwTBNlnMM/DNEi3VBjJqWVkRYEo5pEE3n2AXH1tRkEAS5PfsH
nne96b1ys+4fn8nvDmB0/57Ww2mwV07J72AYAnS9QQI2qIETXEiBpizm3jS9/efT/5l/pTzz07unfl9oOxrYOrl8e2jg/vjW2V3X50c+ekz93UECNqiBE1xIgaYkQNMaKGGFFDjKghRtQQI2qIETXEiBpiRA0xooYYUUOMqCFG1BAjaogRNcSIGmJEDTGihhhRQ4yoIUbUECNqiNnKsztVdz
G93bf+bO2NbBmnt2h+1yU0OMqCFG1BAjaogRNcSIGmJEDTGihhhRQ4yoIUbUECNqiBE1xIgaYkQNMaKGGFFDjKghRtQQI2qIETXEiBpiRA0xooYYUUOMqCHmzD+78+jNV8e27
z1djWWmu9/OX1sa29w7knftguNzXEiBpiRA0xooYYUUOMqCFG1BAjaogRNcSIGmJEDTGihhhRQ4yoIUbUECNqiBE1xIgaYkQNMaKGGFFDjKghRtQQI2qIETXEiBpiRA0xooYYUUOMqCFG1BAjaogRNcSIGmJEDTGihhhRQ4yoIUbUECNqiBE1xIgaYkQNMaKGGFFDjKghRtQQI2qIETXEiBpiRA0xooYYUUOMqCHm3Gkf4L/664WnT/sIW3N3/8bY1ovnr41tvfT5b2Nbx7
M
1f+GmhhhRQ4yoIUbUECNqiBE1xIgaYkQNMaKGGFFDjKghRtQQI2qIETXEiBpiRA0xooYYUUOMqCFG1BAjaogRNcSIGmJEDTGihhhRQ8yZf3bn/P1/xraO/v17bGuttT4++mBs64t3vx7b+vWt58e2Dq5eHttaa63jw3ujeydxU0OMqCFG1BAjaogRNcSIGmJEDTGihhhRQ4yoIUbUECNqiBE1xIgaYkQNMaKGGFFDjKghRtQQI2qIETXEiBpiRA0xooYYUUOMqCHmzD+7s/v9L2Nb7/20P7a11lqHr30ztnXxhw/HtiZ/19G3P49t
XWJxdeH907iZsaYkQNMaKGGFFDjKghRtQQI2qIETXEiBpiRA0xooYYUUOMqCFG1BAjaogRNcSIGmJEDTGihhhRQ4yoIUbUECNqiBE1xIgaYkQNMaKGmDP/ltakC9f/HN27eGPufatPL90a2
o4RtjWz9+98rY1lpr7a07o3sncVNDjKghRtQQI2qIETXEiBpiRA0xooYYUUOMqCFG1BAjaogRNcSIGmJEDTGihhhRQ4yoIUbUECNqiBE1xIgaYkQNMaKGGFFDjKghZmez2Zz2GYAnyE0NMaKGGFFDjKghRtQQI2qIETXEiBpiRA0xooYYUUOMqCFG1BAjaogRNcSIGmJEDTGihhhRQ4yoIUbUECNqiBE1xDwGiMdDfXTeiykAAAAASUVORK5CYII=\n",
"text/plain": [
"
"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"# We know from the following that ans_1_digit is 6\n",
"k = 1673\n",
"im = X_digits[k].reshape(8, 8)\n",
"print(im)\n",
"plt.imshow(im)\n",
"plt.axis('off')\n",
"print('y_digits[{}] = {}'.format(k, y_digits[k]))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"grade": true,
"grade_id": "Question 01",
"locked": true,
"points": "4",
"solution": false
}
},
"outputs": [],
"source": [
"###\n",
"### AUTOGRADER TEST - DO NOT REMOVE\n",
"###\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"grade": false,
"locked": true,
"solution": false
}
},
"source": [
"## Examining the Target Classes\n",
"\n",
"In classification problems, the labels (or targets) are *discrete* or *categorical* values (by contrast with regression problems). That being the case, it is generally preferable when the labelled data are *balanced*; that is, the labels are uniformly distributed in the training data from which models are built. For a binary classification problem (i.e., one with two classes), that means 50% of the data is from one class and 50% of the data from the other class. For a classification problem with $k$ classes, that would mean each class is represented in $(100 \\div k)$% of the data.\n",
"\n",
"Examining the target vector `y` for the MNIST Digits data, it appears that each numeral from the sequence $0$ through $9$ occurs in a random sequence:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[9 5 5 6 5 0 9 8 9 8 4 1 7 7 3 5 1 0 0 2 2 7 8 2 0 1 2 6 3]\n"
]
}
],
"source": [
"y = digits.target\n",
"print(y[31:60])"
]
},
{
"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": [
"[Back to top](#Assignment-Contents)\n",
"
a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"grade": false,
"locked": true,
"solution": false
}
},
"source": [
"---\n",
"#### Question 2\n",
"\n",
"Your task here is to summarize how often each digit from $0$ through $9$ occurs in the vector `y_digits`.\n",
"\n",
"+ The result should be a Pandas Series with the integers $0$ though $9$ as the index (sorted in increasing order) and the co
esponding counts as the data.\n",
"+ HINT: the Pandas Series method `value_counts` can do this easily, as can the Numpy function `numpy.unique`.\n",
"+ Assign the resulting Series to `ans_2`."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Raw counts:\n",
"===========\n",
"3 183\n",
"5 182\n",
"1 182\n",
"6 181\n",
"4 181\n",
"9 180\n",
"7 179\n",
"0 178\n",
"2 177\n",
"8 174\n",
"dtype: int64\n",
"\n",
"Frequencies:\n",
"============\n",
"3 18.3\n",
"5 18.2\n",
"1 18.2\n",
"6 18.1\n",
"4 18.1\n",
"9 18.0\n",
"7 17.9\n",
"0 17.8\n",
"2 17.7\n",
"8 17.4\n",
"dtype: float64\n",
"\n"
]
}
],
"source": [
"### GRADED\n",
"### Create a Pandas Series with a sorted index of the numerals 0 through 9. \n",
"### The co
esponding values are the counts of the occu
ences of each digit\n",
"### in the vector y_digits from the MNIST digits dataset.\n",
"### Assign the result to the identifier ans_2.\n",
"### YOUR SOLUTION HERE:\n",
"values=pd.Series(y_digits)\n",
"ans_2 = values.value_counts()\n",
"print('Raw counts:\\n===========\\n{}\\n'.format(ans_2))\n",
"print('Frequencies:\\n============\\n{}\\n'.format(ans_2/len(ans_2)))"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"grade": true,
"grade_id": "Question 02",
"locked": true,
"points": "5",
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here