Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sigmoid(X): | |
'''Compute the sigmoid function ''' | |
#d = zeros(shape=(X.shape)) | |
den = 1.0 + e ** (-1.0 * X) | |
d = 1.0 / den | |
return d |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def map_feature(x1, x2): | |
''' | |
Maps the two input features to quadratic features. | |
Returns a new feature array with more features, comprising of | |
X1, X2, X1 ** 2, X2 ** 2, X1*X2, X1*X2 ** 2, etc... | |
Inputs X1, X2 must be the same size | |
''' | |
x1.shape = (x1.size, 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from numpy import loadtxt, where, zeros, e, array, log, ones, append, linspace | |
from pylab import scatter, show, legend, xlabel, ylabel, contour, title | |
from scipy.optimize import fmin_bfgs | |
def sigmoid(X): | |
'''Compute the sigmoid function ''' | |
#d = zeros(shape=(X.shape)) | |
den = 1.0 + e ** (-1.0 * X) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Plot Boundary | |
u = linspace(-1, 1.5, 50) | |
v = linspace(-1, 1.5, 50) | |
z = zeros(shape=(len(u), len(v))) | |
for i in range(len(u)): | |
for j in range(len(v)): | |
z[i, j] = (map_feature(array(u[i]), array(v[j])).dot(array(theta))) | |
z = z.T | |
contour(u, v, z) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def predict(theta, X): | |
'''Predict whether the label | |
is 0 or 1 using learned logistic | |
regression parameters ''' | |
m, n = X.shape | |
p = zeros(shape=(m, 1)) | |
h = sigmoid(X.dot(theta.T)) | |
for it in range(0, h.shape[0]): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from numpy import loadtxt, zeros, ones, array, linspace, logspace, mean, std, arange | |
from mpl_toolkits.mplot3d import Axes3D | |
import matplotlib.pyplot as plt | |
from pylab import plot, show, xlabel, ylabel | |
#Evaluate the linear regression | |
def feature_normalize(X): | |
''' | |
Returns a normalized version of X where |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def feature_normalize(X): | |
''' | |
Returns a normalized version of X where | |
the mean value of each feature is 0 and the standard deviation | |
is 1. This is often a good preprocessing step to do when | |
working with learning algorithms. | |
''' | |
mean_r = [] | |
std_r = [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from numpy import loadtxt, zeros, ones, array, linspace, logspace | |
from pylab import scatter, show, title, xlabel, ylabel, plot, contour | |
#Evaluate the linear regression | |
def compute_cost(X, y, theta): | |
''' | |
Comput cost for linear regression | |
''' | |
#Number of training samples |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from numpy import loadtxt, zeros, ones, array, linspace, logspace | |
from pylab import scatter, show, title, xlabel, ylabel, plot, contour | |
#Load the dataset | |
data = loadtxt('ex1data1.txt', delimiter=',') | |
#Plot the data |
NewerOlder