-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
123 lines (90 loc) · 3.39 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import sys
import numpy as np
from scipy import optimize
from matplotlib import pyplot
sys.path.append('..')
def trainLinearReg(linearRegCostFunction, X, y, lambda_=0.0, maxiter=200):
"""
Trains linear regression using scipy's optimize.minimize.
Parameters
----------
X : array_like
The dataset with shape (m x n+1). The bias term is assumed to be concatenated.
y : array_like
Function values at each datapoint. A vector of shape (m,).
lambda_ : float, optional
The regularization parameter.
maxiter : int, optional
Maximum number of iteration for the optimization algorithm.
Returns
-------
theta : array_like
The parameters for linear regression. This is a vector of shape (n+1,).
"""
# Initialize Theta
initial_theta = np.zeros(X.shape[1])
# Create "short hand" for the cost function to be minimized
costFunction = lambda t: linearRegCostFunction(X, y, t, lambda_)
# Now, costFunction is a function that takes in only one argument
options = {'maxiter': maxiter}
# Minimize using scipy
res = optimize.minimize(costFunction, initial_theta, jac=True, method='TNC', options=options)
return res.x
def featureNormalize(X):
"""
Normalizes the features in 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.
Parameters
----------
X : array_like
An dataset which is a (m x n) matrix, where m is the number of examples,
and n is the number of dimensions for each example.
Returns
-------
X_norm : array_like
The normalized input dataset.
mu : array_like
A vector of size n corresponding to the mean for each dimension across all examples.
sigma : array_like
A vector of size n corresponding to the standard deviations for each dimension across
all examples.
"""
mu = np.mean(X, axis=0)
X_norm = X - mu
sigma = np.std(X_norm, axis=0, ddof=1)
X_norm /= sigma
return X_norm, mu, sigma
def plotFit(polyFeatures, min_x, max_x, mu, sigma, theta, p):
"""
Plots a learned polynomial regression fit over an existing figure.
Also works with linear regression.
Plots the learned polynomial fit with power p and feature normalization (mu, sigma).
Parameters
----------
polyFeatures : func
A function which generators polynomial features from a single feature.
min_x : float
The minimum value for the feature.
max_x : float
The maximum value for the feature.
mu : float
The mean feature value over the training dataset.
sigma : float
The feature standard deviation of the training dataset.
theta : array_like
The parameters for the trained polynomial linear regression.
p : int
The polynomial order.
"""
# We plot a range slightly bigger than the min and max values to get
# an idea of how the fit will vary outside the range of the data points
x = np.arange(min_x - 15, max_x + 25, 0.05).reshape(-1, 1)
# Map the X values
X_poly = polyFeatures(x, p)
X_poly -= mu
X_poly /= sigma
# Add ones
X_poly = np.concatenate([np.ones((x.shape[0], 1)), X_poly], axis=1)
# Plot
pyplot.plot(x, np.dot(X_poly, theta), '--', lw=2)