import numpy as np import pandas as pd import matplotlib.pyplot as plt path = 'ex1data1.txt' data = pd.read_csv(path, names=['Population', 'Profit']) print(data.head()) print(data.describe()) data.plot(kind='scatter', x='Population', y='Profit', label='population', figsize=(8, 5)) print(plt.show()) data.insert(0, 'ones', 1) print(data.head()) X = data.iloc[:, 0:-1] y = data.iloc[:, -1] X = X.values y = y.values y = y.reshape(97, 1) def costFunction(X, y, theta): inner = np.power(X @ theta - y, 2) return np.sum(inner) / (2 * len(X)) theta = np.zeros((2, 1)) cost_init = costFunction(X, y, theta) print(cost_init) def gradientDescent(X, y, theta, alpha, iters): costs = [] for i in range(iters): theta = theta - (X.T @ (X @ theta - y)) * alpha / len(X) cost = costFunction(X, y, theta) costs.append(cost) if i % 100 == 0: print(cost) return theta, costs alpha = 0.02 iters = 2000 theta, costs = gradientDescent(X, y, theta, alpha, iters) fig, ax = plt.subplots() ax.plot(np.arange(iters), costs) ax.set(xlabel='iters', ylabel='cost', title='cost vs iters') print(plt.show()) x = np.linspace(y.min(), y.max(), 100) y_ = theta[0, 0] + theta[1, 0] * x fig, ax = plt.subplots() ax.scatter(X[:, 1], y, label='training data') ax.plot(x, y_, 'r', label='predict') ax.legend() ax.set(xlabel='populaiton', ylabel='profit') print(plt.show())
吴恩达机器学习编程题一(单变量线性回归)(Python)
最新推荐文章于 2022-09-28 21:03:04 发布