StanfordMLPython/.ipynb_checkpoints/Untitled-checkpoint.ipynb

141 lines
3.7 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1>**Exercise One : Linear Regression**</h1>\n",
"\n",
"<h3> Introduction </h3>\n",
"\n",
"In this exercise we will implement linear regression and see it work on data\n",
"\n",
"Files included in this exercise:\n",
"\n",
" - ex1data1.txt - Dataset for linear regression with one variable\n",
" - ex1data2.txt - Dataset for linear regression with multiple variables\n",
" \n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# used for manipulating directory paths\n",
"import os\n",
"\n",
"# Scientific and vector computation for python\n",
"import numpy as np\n",
"\n",
"# Plotting library\n",
"from matplotlib import pyplot\n",
"from mpl_toolkits.mplot3d import Axes3D # needed to plot 3-D surfaces\n",
"\n",
"# tells matplotlib to embed plots within the notebook\n",
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3> 1 Simple Python function </h3>\n",
"\n",
"We will warmup by creating a function which returns an n x n identity matrix"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"def warmupexercise(x):\n",
" A = np.identity(x)\n",
" return A"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we can run the function with an input of 5 to create a 5 x 5 identity matrix"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1., 0., 0., 0., 0.],\n",
" [0., 1., 0., 0., 0.],\n",
" [0., 0., 1., 0., 0.],\n",
" [0., 0., 0., 1., 0.],\n",
" [0., 0., 0., 0., 1.]])"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"warmupexercise(5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>2 Linear Regression with One Variable </h3>\n",
"\n",
"In this part of this exercise, we will implement linear regression with one variable to predict profits for a food truck. Suppose you are the CEO of a restaurant franchise and are considering different cities for opening a new outlet. The chain already has trucks in various cities and we have data for profits and populations from the cities.\n",
"\n",
"We would like to use this data to help you select which city to expand to next. The file ex1data1.txt contains the dataset for our linear regression problem. The first column is the population of a city and the second column is the profit of a food truck in that city. A negative value for profit indicates a loss. \n",
"\n",
"We now load the data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Read comma separated data\n",
"data = np.loadtxt(os.path.join('Data', 'ex1data1.txt'), delimiter=',')\n",
"X, y = data[:, 0], data[:, 1]\n",
"\n",
"m = y.size # number of training examples"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}