Answer To: Homework 5 ESE 402/542 Due on 11/20/2019 (For Problems 1 and 2, no other package except numpy and...
Kshitij answered on Nov 14 2021
Annotation 2019-11-13 021225.jpg
Annotation 2019-11-13 031900.jpg
Problem-1.ipynb
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problem 1 (a)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from mpl_toolkits.mplot3d import axes3d # for 3-d plots\n",
"import matplotlib.pyplot as plt\n",
"%matplotlib inline"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# Defining the sigmoid function\n",
"def sigmoid(f):\n",
" return 1/(1 + np.exp(-f))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# P(Y = +1|X) = S(beta_0 + beta_1.X)\n",
"# We need to take X = 1 and beta_0 and beta_1 both between [-2, 2] with step size of 0.1 to plot the 3d plot\n",
"\n",
"# Initializing X\n",
"X = 1\n",
"\n",
"# Initializing beta_0 and beta_1, -2 to 2.1 because np.arange exclude the latter/stoping range\n",
"# np.round will round off your created array to 2 values\n",
"beta_0 = np.round(np.arange(-2, 2.1, 0.1), 2)\n",
"beta_1 = np.round(np.arange(-2, 2.1, 0.1), 2)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# np.meshgrid - Return coordinate matrices from coordinate vectors.\n",
"# Make N-D coordinate arrays for vectorized evaluations of N-D scalar/vector fields over N-D grids, \n",
"# given one-dimensional coordinate arrays x1, x2,..., xn.\n",
"x, y = np.meshgrid(beta_0, beta_1)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[-2. -2. ]\n",
" [-1.9 -2. ]\n",
" [-1.8 -2. ]\n",
" [-1.7 -2. ]\n",
" [-1.6 -2. ]\n",
" [-1.5 -2. ]\n",
" [-1.4 -2. ]\n",
" [-1.3 -2. ]\n",
" [-1.2 -2. ]\n",
" [-1.1 -2. ]\n",
" [-1. -2. ]\n",
" [-0.9 -2. ]\n",
" [-0.8 -2. ]\n",
" [-0.7 -2. ]\n",
" [-0.6 -2. ]\n",
" [-0.5 -2. ]\n",
" [-0.4 -2. ]\n",
" [-0.3 -2. ]\n",
" [-0.2 -2. ]\n",
" [-0.1 -2. ]\n",
" [ 0. -2. ]\n",
" [ 0.1 -2. ]\n",
" [ 0.2 -2. ]\n",
" [ 0.3 -2. ]\n",
" [ 0.4 -2. ]\n",
" [ 0.5 -2. ]\n",
" [ 0.6 -2. ]\n",
" [ 0.7 -2. ]\n",
" [ 0.8 -2. ]\n",
" [ 0.9 -2. ]\n",
" [ 1. -2. ]\n",
" [ 1.1 -2. ]\n",
" [ 1.2 -2. ]\n",
" [ 1.3 -2. ]\n",
" [ 1.4 -2. ]\n",
" [ 1.5 -2. ]\n",
" [ 1.6 -2. ]\n",
" [ 1.7 -2. ]\n",
" [ 1.8 -2. ]\n",
" [ 1.9 -2. ]\n",
" [ 2. -2. ]\n",
" [-2. -1.9]\n",
" [-1.9 -1.9]\n",
" [-1.8 -1.9]\n",
" [-1.7 -1.9]\n",
" [-1.6 -1.9]\n",
" [-1.5 -1.9]\n",
" [-1.4 -1.9]\n",
" [-1.3 -1.9]\n",
" [-1.2 -1.9]\n",
" [-1.1 -1.9]\n",
" [-1. -1.9]\n",
" [-0.9 -1.9]\n",
" [-0.8 -1.9]\n",
" [-0.7 -1.9]\n",
" [-0.6 -1.9]\n",
" [-0.5 -1.9]\n",
" [-0.4 -1.9]\n",
" [-0.3 -1.9]\n",
" [-0.2 -1.9]]\n"
]
}
],
"source": [
"# This is what we need and we got it.\n",
"xy = np.array([x.flatten(), y.flatten()]).T\n",
"print(xy[:60])"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1681, 2)"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xy.shape"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"# Now we have to plot the above with sigmoid as our output, let output be z\n",
"# z = sigmoid(beta_0 + beta_1) # this also works for this case, as X = 1\n",
"z = sigmoid(x + np.dot(y, X))"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.01798621, 0.01984031, 0.02188127, ..., 0.450166 , 0.47502081,\n",
" 0.5 ],\n",
" [0.01984031, 0.02188127, 0.02412702, ..., 0.47502081, 0.5 ,\n",
" 0.52497919],\n",
" [0.02188127, 0.02412702, 0.02659699, ..., 0.5 , 0.52497919,\n",
" 0.549834 ],\n",
" ...,\n",
" [0.450166 , 0.47502081, 0.5 , ..., 0.97340301, 0.97587298,\n",
" 0.97811873],\n",
" [0.47502081, 0.5 , 0.52497919, ..., 0.97587298, 0.97811873,\n",
" 0.98015969],\n",
" [0.5 , 0.52497919, 0.549834 , ..., 0.97811873, 0.98015969,\n",
" 0.98201379]])"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"z"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"image/png":...