吳恩達機器學習課后作業--ex1


單變量的線性回歸

問題:在ex1data1.txt中有兩列數據,第一列是城市人口數量,第二列是該城市小吃店利潤,目的是根據城市人口數量來預測小吃店的利潤

需要用到的公式

實現

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

numpy用來進行矩陣的運算,pandas常用來進行數據的操作,matplotlib主要用來可視化

讀取數據集

path= "D:\\ML\\ML\\Linear Regression with One Variable\\ex1data1.txt"
data = pd.read_csv(path,header=None,names=['Population','Profit'])#文件讀取
data.head()#顯示數據
data.plot(kind='scatter',x='Population',y='Profit',figsize=(12,8))
plt.show()

計算損失函數J(Ѳ)

def computeCost(X,y,theta):
    inner = np.power(((X*theta.T)-y),2)
    return np.sum(inner)/(2*len(X))

對數據集進行處理

data.insert(0,'Ones',1)#在第0列插入1,列名為Ones
#初始化變量X和y
#data.shape輸出(行數,列數),所以shape[1]就為列數
cols = data.shape[1]#計算列數
#[起始行:終止行,起始列:終止列]
X = data.iloc[:,:-1]#除最后一列  iloc是前閉后開,從0開始,提取指定行和列
y = data.iloc[:,cols-1:cols]#最后一列

X = np.matrix(X.values)
y = np.matrix(y.values)
theta = np.matrix(np.array([0,0]))#array轉matrix,具體看array和matrix的區別

梯度下降算法

def gradientDescent(X,y,theta,alpha,iters):
    temp = np.matrix(np.zeros(theta.shape))
    parameters = int(theta.ravel().shape[1])#ravel扁平化操作變成一維,計算參數的個數
    cost = np.zeros(iters)
    for i in range(iters):
        error = (X*theta.T)-y
        for j in range(parameters):
            term = np.multiply(error,X[:,j])
            temp[0,j] = theta[0,j]-((alpha/len(X))*np.sum(term))
        theta = temp
        cost[i] = computeCost(X,y,theta)
    return theta,cost

設置學習率和迭代次數並執行

alpha = 0.01
iters = 1500
g,cost = gradientDescent(X,y,theta,alpha,iters)

可視化

#可視化
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()
plt.plot(range(0,1500),cost)
plt.show()

pandas:

pd.read_csv(filename):從CSV文件導入數據

data.head(n)默認顯示數據的前5行

data.plot(kind='scatter',x='Population',y='Profit',figsize=(12,8))畫圖

data.shape() 獲取data的行數和列數,是一個元組

data.shape[1] 得到data的列數

data.iloc[:,:-1]  獲取data的部分數據,里面的含義是[起始行:終止行,起始列:終止列],包含的數據是前閉后開

data.insert(0,'Ones',1) 在第0列插入,值為1 

numpy:

np.power(data,2) 對矩陣中的元素求平方

np.sum(data)對矩陣中的元素求和

np.matrix(X.values)轉化為矩陣

data.ravel()對數據進行扁平化

np.multiply對應位置相乘,*是矩陣乘法

x = np.linspace(data.Population.min(), data.Population.max(), 100)生成元素個數為100的等間隔數列。而前兩個參數分別是數列的開頭與結尾。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM