Coursera机器学习编程作业Python实现(Andrew Ng)—— 1.1 Linear regression with one variable

1.1 Linear regression with one variable

import numpy as np
import matplotlib.pyplot as plt
data1 = np.loadtxt('ex1data1.txt', delimiter=',')

Plotting the Data

plt.scatter(data1[:,0], data1[:,1], c='red', marker='x')
plt.xlabel('Population of City in 10,000s')
plt.ylabel('Profit in $10,000s')
plt.show()

数据预处理及参数初始化

x0 = np.ones((len(data1),1))
x1 = data1[:,0]
x1 = x1.reshape([len(x1), 1])
X = np.hstack((x0, x1))
y = data1[:,1] y = y.reshape([len(y), 1])
theta = np.zeros((2,1))
iterations = 1500 alpha = 0.01

定义假设函数

def h(X, theta):
    return np.dot(X, theta)

定义代价函数

def computeCost(X, theta, y):
    return 0.5 * np.mean(np.square(h(X, theta) - y))

定义梯度下降函数

def gradientDescent(X, theta, y, iterations, alpha):
    Cost = []
    Cost.append(computeCost(X, theta, y))
    for i in range(iterations):
        grad0 = np.mean(h(X, theta) - y)
        grad1 = np.mean((h(X, theta) - y) * (X[:,1].reshape([len(X), 1])))
        theta[0] = theta[0] - alpha * grad0
        theta[1] = theta[1] - alpha * grad1
        Cost.append(computeCost(X, theta, y))
    return theta, Cost

运行并观察结果

theta_result, Cost_result = gradientDescent(X, theta, y, iterations, alpha)
theta_result
array([[-3.63029144],
       [ 1.16636235]])
predict1 = np.dot(np.array([1, 3.5]), theta_result)
predict1
array([0.45197679])
x_predict = [X[:,1].min(), X[:,1].max()]
y_predict = [theta_result[0]+theta_result[1]*(X[:,1].min()), theta_result[0]+theta_result[1]*(X[:,1].max())]
plt.plot(x_predict, y_predict, c='blue', label='predict')
plt.scatter(data1[:,0], data1[:,1], c='red', marker='x', label='train_data')
plt.xlabel('Population of City in 10,000s')
plt.ylabel('Profit in $10,000s')
plt.legend()
plt.show()
plt.plot(Cost_result)
plt.xlabel('Iterations')
plt.ylabel('Cost')
plt.show()

猜你喜欢

转载自www.cnblogs.com/shouzhenghouchuqi/p/10585924.html