机器学习(吴恩达)第二周作业的python实现

ex1.py:

# coding=UTF-8

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D #绘制3D坐标的函数
import threading
import myFun

## Machine Learning Online Class - Exercise 1: Linear Regression

#  Instructions
#  ------------
#
#  This file contains code that helps you get started on the
#  linear exercise. You will need to complete the following functions
#  in this exericse:
#
#     warmUpExercise.m
#     plotData.m
#     gradientDescent.m
#     computeCost.m
#     gradientDescentMulti.m
#     computeCostMulti.m
#     featureNormalize.m
#     normalEqn.m
#
#  For this exercise, you will not need to change any code in this file,
#  or any other files other than those mentioned above.
#
# x refers to the population size in 10,000s
# y refers to the profit in $10,000s
#

## Initialization
# clear ; close all; clc

## ==================== Part 1: Basic Function ====================
# Complete warmUpExercise.m
print('Running warmUpExercise ... ')
print('5x5 Identity Matrix: ')
print(myFun.warmUpExercise())

input("按回车继续:")

## ======================= Part 2: Plotting =======================
print('Plotting Data ...')
data = np.loadtxt('ex1data1.txt', delimiter=',')
# print("显示data的shape:")
# print(data.shape)  #显示data的shape
X = data[:, 0]
y = data[:, 1]
# 下面两句是对X,y进行整形,整形的目的是化一维为二维(哪怕二维数组中只有一行),因为只有在二维数组中才有行与列的区分
X = np.reshape(X, (X.shape[0], 1))
y = np.reshape(y, (y.shape[0], 1))
# print(X.shape,y.shape)

m = y.shape[0]  # 获取行数
# print(m)

# Plot Data
# Note: You have to complete the code in plotData.m
# 下面二行是给绘图函数单开线程运行,以达到绘图后不用关闭图主程序也能继续运行的目的,args为参数;
# 但目前有个问题:图像关闭后会产生一个Tcl_AsyncDelete异常
# t1 = threading.Thread(target=myFun.plotData, args=(X, y))
# t1.start()
myFun.plotData(X, y)
input("按回车继续:")

## =================== Part 3: Cost and Gradient descent ===================

X = np.c_[np.ones(m), data[:, 0]]  # Add a column of ones to x
# print(X)
theta = np.zeros((2, 1))  # initialize fitting parameters(注意此处theta的初始化方法,theta是2*1大小数组
# print(theta.shape)
# Some gradient descent settings
iterations = 1500
alpha = 0.01

print('Testing the cost function ...')
# compute and display initial cost
J = myFun.computeCost(X, y, theta)
print('With theta = [0 ; 0]\nCost computed = ' + repr(J))
print('Expected cost value (approx) 32.07')

# further testing of the cost function
J = myFun.computeCost(X, y, np.array([[-1], [2]]))
print('\nWith theta = [-1 ; 2]\nCost computed = ' + repr(J))
print('Expected cost value (approx) 54.24\n')

input("按回车继续:")

print('\nRunning Gradient Descent ...')
# run gradient descent
(theta, J_history) = myFun.gradientDescent(X, y, theta, alpha, iterations)
# print("debug:" + repr(J_history))
# print("debug:" + repr(J_history.shape))
# print theta to screen
print('Theta found by gradient descent:')
print(theta)
print('Expected theta values (approx)')
print("-3.6303\n  1.1664\n")
# Plot the linear fit
# hold on; # keep previous plot visible
plt.scatter(X[:, 1], y, label='Training data', color='r', s=50, marker="x")  # s参数代表符号大小
plt.plot(X[:, 1], np.dot(X, theta), 'b-', label='Linear regression', linewidth=2)
plt.xlabel('population')
plt.ylabel('revenue')
plt.legend()
plt.show()
# legend('Training data', 'Linear regression')
# hold off # don't overlay any more plots on this figure

# Predict values for population sizes of 35,000 and 70,000
predict1 = np.dot([[1, 3.5]], theta)  # 注意此处构建theta参数的方法,numpy中[1,3.5]是个一维数组,但[[1,3.5]]是个二维数组(只不过这个二维数组只有一行罢了)
print('For population = 35,000, we predict a profit of ' + repr(predict1 * 10000))
predict2 = np.dot([[1, 7]], theta)
print('For population = 70,000, we predict a profit of ' + repr(predict2 * 10000))

print('Program paused. Press enter to continue.')
input("按回车继续:")

## ============= Part 4: Visualizing J(theta_0, theta_1) =============
print('Visualizing J(theta_0, theta_1) ...')

# Grid over which we will calculate J
theta0_vals = np.linspace(-10, 10, 20)
theta1_vals = np.linspace(-1, 4, 20)

# initialize J_vals to a matrix of 0's
J_vals = np.zeros((theta0_vals.shape[0] , theta1_vals.shape[0]))

# Fill out J_vals
for i in range(0,theta0_vals.shape[0]):
    for j in range(0,theta1_vals.shape[0]):
        t = [[theta0_vals[i]],[theta1_vals[j]]]
        J_vals[i, j] = myFun.computeCost(X, y, t)

# Because of the way meshgrids work in the surf command, we need to
# transpose J_vals before calling surf, or else the axes will be flipped
#J_vals = J_vals.T
#print(J_vals.shape)
# Surface plot
#figure;
fig=plt.figure()#创建一个绘图对象
ax=Axes3D(fig)#用这个绘图对象创建一个Axes对象(有3D坐标)
theta0_vals,theta1_vals=np.meshgrid(theta0_vals,theta1_vals)  #用这两个arange对象中的可能取值一一映射去扩充为所有可能的取样点
plt.title("Visualizing J(theta_0, theta_1)")#总标题
ax.plot_surface(theta0_vals, theta1_vals, J_vals, rstride=1, cstride=1, cmap=plt.cm.coolwarm)#用取样点(x,y,z)去构建曲面
ax.set_xlabel('Ɵ0', color='r')
ax.set_ylabel('Ɵ1', color='g')
ax.set_zlabel('J', color='b')#给三个坐标轴注明
plt.show()#显示模块中的所有绘图对象

#下面代码绘制等高线图
#为等高线填充颜色 10表示按照高度分成10层 alpha表示透明度 cmap表示渐变标准
plt.contourf(theta0_vals,theta1_vals,J_vals,10,alpha=0.75,cmap=plt.cm.hot)
#使用contour绘制等高线
C=plt.contour(theta0_vals,theta1_vals,J_vals,10,colors='black')
#在等高线处添加数字
plt.clabel(C,inline=True,fontsize=10)
plt.scatter(theta[0,0], theta[1,0], color='r', s=50, marker="x")
plt.xlabel('theta0')
plt.ylabel('theta1')
#显示图片
plt.show()


myFun.py:

# coding=UTF-8
import numpy as np
import matplotlib.pyplot as plt


def warmUpExercise():
    A = np.eye(5)  # 5维单位矩阵
    return A


def plotData(x, y):
    # PLOTDATA Plots the data points x and y into a new figure
    #   PLOTDATA(x,y) plots the data points and gives the figure axes labels of
    #   population and profit.
    # ====================== YOUR CODE HERE ======================
    # Instructions: Plot the training data into a figure using the
    #               "figure" and "plot" commands. Set the axes labels using
    #               the "xlabel" and "ylabel" commands. Assume the
    #               population and revenue data have been passed in
    #               as the x and y arguments of this function.
    #
    # Hint: You can use the 'rx' option with plot to have the markers
    #       appear as red crosses. Furthermore, you can make the
    #       markers larger by using plot(..., 'rx', 'MarkerSize', 10);
    plt.scatter(x, y, label="Training data", color='r', s=50, marker="x")  # s参数代表符号大小
    plt.xlabel('population')
    plt.ylabel('revenue')
    plt.legend()
    plt.show()


def computeCost(X, y, theta):
    # COMPUTECOST Compute cost for linear regression
    #   J = COMPUTECOST(X, y, theta) computes the cost of using theta as the
    #   parameter for linear regression to fit the data points in X and y
    # Initialize some useful values
    m = y.shape[0]  # number of training examples
    # You need to return the following variables correctly
    J = 0
    # ====================== YOUR CODE HERE ======================
    # Instructions: Compute the cost of a particular choice of theta
    #               You should set J to the cost.
    predictions = np.dot(X, theta)  # 求X与theta的内积
    # print("predictions's size:",predictions.shape)
    # print("y's size:",y.shape)
    # print("p-y's szie:",(predictions-y).shape)

    sqr = np.power(predictions - y, 2)
    # print(sqr)
    # print("sqr's size:",sqr.shape)
    # print("sum:",np.sum(sqr))
    J = 1 / (2 * m) * np.sum(sqr)
    return J


def gradientDescent(X, y, theta, alpha, num_iters):
    # GRADIENTDESCENT Performs gradient descent to learn theta
    #   theta = GRADIENTDESCENT(X, y, theta, alpha, num_iters) updates theta by
    #   taking num_iters gradient steps with learning rate alpha

    # Initialize some useful values
    m = y.shape[0]  # number of training examples
    J_history = np.zeros((num_iters, 1))

    for iter in range(0, num_iters):
        # ====================== YOUR CODE HERE ======================
        # Instructions: Perform a single gradient step on the parameter vector
        #               theta.
        #
        # Hint: While debugging, it can be useful to print out the values
        #       of the cost function (computeCost) and gradient here.
        #
        predictions = np.dot(X , theta)
        theta = theta - alpha / m * (np.dot((predictions - y).T , X)).T
        #print("de:"+repr(theta))
        # ============================================================
        # Save the cost J in every iteration
        J_history[iter] = computeCost(X, y, theta)
    return (theta,J_history)

猜你喜欢

转载自blog.csdn.net/wjs30078111/article/details/80317650
今日推荐