lab2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Hồi quy tuyến tính

import numpy as np

import matplotlib.pyplot as plt

from utils import *

import copy

import math

%matplotlib inline

x_train, y_train = load_data()

print("Type of x_train:",type(x_train))

print("First five elements of x_train are:\n", x_train[:5])

Type of x_train: <class 'numpy.ndarray'>

First five elements of x_train are:

[6.1101 5.5277 8.5186 7.0032 5.8598]

print("Type of y_train:",type(y_train))

print("First five elements of y_train are:\n", y_train[:5])

Type of y_train: <class 'numpy.ndarray'>

First five elements of y_train are:

[17.592 9.1302 13.662 11.854 6.8233]

print ('The shape of x_train is:', x_train.shape)

print ('The shape of y_train is: ', y_train.shape)

print ('Number of training examples (m):', len(x_train))

The shape of x_train is: (97,)

The shape of y_train is: (97,)

Number of training examples (m): 97

plt.scatter(x_train, y_train, marker='x', c='r')

# Set the title

plt.title("Profits vs. Population per city")

# Set the y-axis label

1
plt.ylabel('Profit in $10,000')

# Set the x-axis label

plt.xlabel('Population of City in 10,000s')

plt.show()

5 - Tính toán chi phí

def compute_cost(x, y, w, b):

"""

Computes the cost function for linear regression.

Args:

x (ndarray): Shape (m,) Input to the model (Population of cities)

y (ndarray): Shape (m,) Label (Actual profits for the cities)

w, b (scalar): Parameters of the model

Returns

total_cost (float): The cost of using w,b as the parameters for linear regression

to fit the data points in x and y

"""

# number of training examples

m = x.shape[0]

# You need to return this variable correctly

total_cost = 0

### START CODE HERE ###

for i in range(m):

f_wb = w*x[i] + b

# Tính toán dự đoán cho ví dụ thứ i

cost = (f_wb - y[i])**2

# Tính toán chi phí cho ví dụ thứ i

total_cost += cost

total_cost *= 1/(2*m)

2
### END CODE HERE ###

return total_cost

initial_w = 2

initial_b = 1

cost = compute_cost(x_train, y_train, initial_w, initial_b)

print(type(cost))

print(f'Cost at initial w: {cost:.3f}')

<class 'numpy.float64'>

Cost at initial w: 75.203

6 - Giảm dần độ dốc

def compute_gradient(x, y, w, b):

"""

Computes the gradient for linear regression

Args:

x (ndarray): Shape (m,) Input to the model (Population of cities)

y (ndarray): Shape (m,) Label (Actual profits for the cities)

w, b (scalar): Parameters of the model

Returns

dj_dw (scalar): The gradient of the cost w.r.t. the parameters w

dj_db (scalar): The gradient of the cost w.r.t. the parameter b

"""

# Number of training examples

m = x.shape[0]

# You need to return the following variables correctly

dj_dw = 0

dj_db = 0

### START CODE HERE ###

for i in range(m):

3
f_wb = w * x[i] + b

dj_db += f_wb - y[i]

dj_dw += (f_wb - y[i]) * x[i]

dj_db *= 1/m

dj_dw *= 1/m

### END CODE HERE ###

return dj_dw, dj_db

initial_w = 0

initial_b = 0

tmp_dj_dw, tmp_dj_db = compute_gradient(x_train, y_train, initial_w, initial_b)

print('Gradient at initial w, b (zeros):', tmp_dj_dw, tmp_dj_db)

Gradient at initial w, b (zeros): -65.32884974555672 -5.83913505154639

Using X with shape (4, 1)

test_w = 0.2

test_b = 0.2

tmp_dj_dw, tmp_dj_db = compute_gradient(x_train, y_train, test_w, test_b)

print('Gradient at test w, b:', tmp_dj_dw, tmp_dj_db)

Gradient at test w, b: -47.41610118114434 -4.007175051546391

2.6 Học các tham số sử dụng giảm dần theo lô

def gradient_descent(x, y, w_in, b_in, cost_function, gradient_function, alpha, num_iters):

"""

Performs batch gradient descent to learn theta. Updates theta by taking

num_iters gradient steps with learning rate alpha

Args:

x : (ndarray): Shape (m,)

y : (ndarray): Shape (m,)

w_in, b_in : (scalar) Initial values of parameters of the model

cost_function: function to compute cost

4
gradient_function: function to compute the gradient

alpha : (float) Learning rate

num_iters : (int) number of iterations to run gradient descent

Returns

w : (ndarray): Shape (1,) Updated values of parameters of the model after

running gradient descent

b : (scalar) Updated value of parameter of the model after

running gradient descent

"""

# number of training examples

m = len(x)

# An array to store cost J and w's at each iteration — primarily for graphing later

J_history = []

w_history = []

w = copy.deepcopy(w_in) #avoid modifying global w within function

b = b_in

for i in range(num_iters):

# Calculate the gradient and update the parameters

dj_dw, dj_db = gradient_function(x, y, w, b )

# Update Parameters using w, b, alpha and gradient

w = w - alpha * dj_dw

b = b - alpha * dj_db

# Save cost J at each iteration

if i<100000: # prevent resource exhaustion

cost = cost_function(x, y, w, b)

J_history.append(cost)

# Print cost every at intervals 10 times or as many iterations if < 10

if i% math.ceil(num_iters/10) == 0:

5
w_history.append(w)

print(f"Iteration {i:4}: Cost {float(J_history[-1]):8.2f} ")

return w, b, J_history, w_history #return w and J,w history for graphing

initial_w = 0.

initial_b = 0.

# some gradient descent settings

iterations = 1500

alpha = 0.01

w,b,_,_ = gradient_descent(x_train ,y_train, initial_w, initial_b,

compute_cost, compute_gradient, alpha, iterations)

print("w,b found by gradient descent:", w, b)

Iteration 0: Cost 6.74

Iteration 150: Cost 5.31

Iteration 300: Cost 4.96

Iteration 450: Cost 4.76

Iteration 600: Cost 4.64

Iteration 750: Cost 4.57

Iteration 900: Cost 4.53

Iteration 1050: Cost 4.51

Iteration 1200: Cost 4.50

Iteration 1350: Cost 4.49

w,b found by gradient descent: 1.166362350335582 -3.63029143940436

m = x_train.shape[0]

predicted = np.zeros(m)

for i in range(m):

predicted[i] = w * x_train[i] + b

plt.plot(x_train, predicted, c = "b")

# Create a scatter plot of the data.

6
plt.scatter(x_train, y_train, marker='x', c='r')

# Set the title

plt.title("Profits vs. Population per city")

# Set the y-axis label

plt.ylabel('Profit in $10,000')

# Set the x-axis label

plt.xlabel('Population of City in 10,000s')

predict1 = 3.5 * w + b

print('For population = 35,000, we predict a profit of $%.2f' % (predict1*10000))

predict2 = 7.0 * w + b

print('For population = 70,000, we predict a profit of $%.2f' % (predict2*10000))

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy