Lab5 Linear Regression
Lab5 Linear Regression
Python is a popular programming language that is reliable, flexible, easy to learn, free to use on all operating systems, and supported by
both a strong developer community and many free libraries. Python supports all manners of development, including web applications, web
services, desktop apps, scripting, data science, scientific computing, and Jupyter notebooks. Python is a language used by many
universities, scientists, casual developers, and professional developers alike.
You can learn more about the language on python.org and Python for Beginners.
An understanding of different number types is important for working with data in analysis. This lab exercise is based on Chapter 1 of the
book Doing Math with Python by Amit Saha. Because all the code is inside code cells, you can just run each code cell inline rather than
using a separate Python interactive window.
Note: This notebook is designed to have you run code cells one by one, and several code cells contain deliberate errors for
demonstration purposes. As a result, if you use the Cell > Run All command, some code cells past the error won't be run. To
resume running the code in each case, use Cell > Run All Below from the cell after the error.
Comments
Many of the examples in this lab exercise include comments. Comments in Python start with the hash character, # , and extend to the end
of the physical line. A comment may appear at the start of a line or following whitespace or code, but not within a string literal. A hash
character within a string literal is just a hash character. Since comments are to clarify code and are not interpreted by Python, they may be
omitted when typing in examples.
In [1]:
%matplotlib inline
#imports
from numpy import *
import matplotlib.pyplot as plt
In [2]:
points = genfromtxt('data.csv', delimiter=',')
#Extract columns
x = array(points[:,0])
y = array(points[:,1])
#Calculate Gradient
for i in range(0, len(points)):
x = points[i, 0]
y = points[i, 1]
m_gradient += - (2/N) * x * (y - (m_current * x + b_current))
b_gradient += - (2/N) * (y - (m_current * x + b_current))
Optimized b: 0.02963934787473239
Optimized m: 1.4774173755483797
Minimized cost: 112.65585181499746