吴恩达机器学习笔记第一课——线性回归

import numpy as np import matplotlib.pyplot as plt import pandas as pd from mpl_toolkits.mplot3d import Axes3D 


def plot_data(data):    data.plot(kind = 'scatter', x= 'population', y = 'profit', figsize=(8, 5))    plt.xlabel('population of city in 10000s')    plt.ylabel('profit')    plt.title('scatter plot of training data')    plt.show()
def computer_cost(x, y, theta):             #计算损失函数    inner = np.power((x.dot(theta.T) - y), 2)   #计算(x0 - y) ** 2    return np.mean(inner) / 2 
def gradientdecent(X, y, theta, alpha, epoch):           #alpha表示的是学习率; epoch表示的迭代次数    cost = np.zeros(epoch)    m = X.shape[0]    for i in range(epoch):        theta = theta - (alpha / m) * ((X.dot(theta.T) - y).T).dot(X)        """        b = alpha / m   应该是可以不除的 可以表示为 学习率  如果不是矩阵,只是一个一般函数,应该是不用除的        a = X.dot(theta.T) - y   表示的是预测值和实际值之间的残差            a.T.dot(X) * b  表示的就是每次迭代的改变值了
        """        cost[i] = computer_cost(X, y, theta)        #计算损失值        return theta, cost
def plot_cost(theta0, theta1, cost):    figure = plt.figure()    axes = Axes3D(figure)    axes.plot3D(theta0, theta1, cost)    plt.show()
def linear_regression_one():    data = 'ex1data1.txt'    data = pd.read_csv(data, header=None, names=['population', 'profit'])    plot_data(data)    data.insert(0, 'Ones', 1)           #insert(插的位置,列名, 值 )    #print(data)    cols = data.shape[1]    x = data.iloc[:, : -1].values       #取除去最后一列的所有列    #print(x)    y = data.iloc[:, cols -1 : ].values       theta = np.array([0, 0])

    alpha = 0.01    epoch = 1000    final_theta, cost = gradientdecent(x, y, theta, alpha, epoch)    minCost = computer_cost(x, y, final_theta)    print(minCost)
    x = np.linspace(data.population.min(), data.population.max(), 100)    h = final_theta[0, 0] + (final_theta[0, 1] * x) #纵坐标,利润        fig, ax = plt.subplots(figsize=(6, 4))    ax.plot(x, h, 'r', label='Prediction')    ax.scatter(data['population'], data.profit, label='Traning Data')    ax.legend(loc=2)  # 2表示在左上角    ax.set_xlabel('population')    ax.set_ylabel('Profit')    ax.set_title('Predicted Profit vs. population Size')    plt.show()

def main():    linear_regression_one()

if __name__ =='__main__':    main()

        

你可能感兴趣的:(机器学习)