吴恩达机器学习作业(1):线性回归
目錄
1)導入相關庫和數據
2)代價函數
3)批量梯度下降
4)繪制線性模型
前陣子在網易云課堂學習了吳恩達老師的機器學習課程,今天結合網上資料,用Python實現了線性回歸作業,共勉。建議大家使用Jupyter notebook來編寫程序。
1)導入相關庫和數據
導入相關庫:numpy, pandas, matplotlib
import numpy as np import pandas as pd import matplotlib.pyplot as plt拿到數據之后,建議大家先看看數據長什么樣子,這樣有助于我們進行之后的分析:
path = 'ex1data1.txt' #指定了列名,header=None data = pd.read_csv(path, header=None, names=['Population', 'Profit']) data.head()data.describe()data.plot(kind='scatter', x='Population', y='Profit', figsize=(12,8)) plt.show()2)代價函數
現在我們使用梯度下降來實現線性回歸,以最小化成本函數。
首先,我們將創建一個以參數為特征的代價函數:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
其中:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
def computeCost(X, y, theta):inner = np.power(((X * theta.T) - y), 2)return np.sum(inner) / (2 * len(X))我們需要在訓練集中添加一列,以便我們可以使用向量化解決方案來計算大家函數:
#在第0列插入1,列名為“Ones” data.insert(0, 'Ones', 1)# set X (training data) and y (target variable) #cols = 3 cols = data.shape[1] X = data.iloc[:,0:cols-1] #X選取所有行,去掉最后一列,第一個分號前為行。 y = data.iloc[:,cols-1:cols]#y選取所有行,最后一列代價函數應該是numpy矩陣,所以我們需要轉換X和y,然后才能使用它們。我們還需要初始化參數theta。
X = np.matrix(X.values) y = np.matrix(y.values) #我們這里是單變量線性回歸,故只需要兩個參數 theta = np.matrix(np.array([0,0]))現在我們計算代價函數(theta初始值為0)
computeCost(X, y, theta) 32.0727338774556763)批量梯度下降
我們前面只是計算了初試theta為0時代價函數的值,我們現在要使用梯度下降算法來求我們的參數。
def gradientDescent(X, y, theta, alpha, iters):temp = np.matrix(np.zeros(theta.shape)) #theta.shape=(1,2)parameters = int(theta.ravel().shape[1]) #ravel()降維,parameters=2cost = np.zeros(iters) #iter維零向量for i in range(iters): #迭代iters次error = (X * theta.T) - yfor j in range(parameters): #2個參數term = np.multiply(error, X[:,j])temp[0,j] = theta[0,j] - ((alpha / len(X)) * np.sum(term))theta = tempcost[i] = computeCost(X, y, theta) #保存每次迭代后的cost值return theta, costalpha = 0.01 iters = 1000現在我們運行梯度下降算法來求我們的參數theta并求出擬合后的代價函數值。
g, cost = gradientDescent(X, y, theta, alpha, iters)computeCost(X, y, g) 4.51595550307891184)繪制線性模型
現在我們來繪制線性模型以及數據,直觀地看出它的擬合。
x = np.linspace(data.Population.min(), data.Population.max(), 100) f = g[0, 0] + (g[0, 1] * x)fig, ax = plt.subplots(figsize=(12,8)) ax.plot(x, f, 'r', label='Prediction') ax.scatter(data.Population, data.Profit, label='Traning Data') ax.legend(loc=2) ax.set_xlabel('Population') ax.set_ylabel('Profit') ax.set_title('Predicted Profit vs. Population Size') plt.show()由于梯度方程式函數也在每個訓練迭代中輸出一個代價的向量,所以我們也可以繪制。 請注意,代價總是降低 - 這是凸優化問題的一個例子。
fig, ax = plt.subplots(figsize=(12,8)) ax.plot(np.arange(iters), cost, 'r') ax.set_xlabel('Iterations') ax.set_ylabel('Cost') ax.set_title('Error vs. Training Epoch') plt.show()?
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的吴恩达机器学习作业(1):线性回归的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: javaw.exe是什么进程 javaw
- 下一篇: 50个最有用的Matplotlib数据分