1 of 12

HW1 TA hours

TAs

ntu.mlta@gmail.com

2 of 12

Outline

Simple linear regression using gradient descent (with adagrad)

  1. 如何抽取feature�
  2. 實做linear regression�
  3. 使用步驟(2)的model預測pm2.5

3 of 12

如何抽取feature

24

18

18

18

2014/1/1

2014/1/2

2014/1/3

...

...

4 of 12

如何抽取feature

(Pseudo code)�1. 宣告一個18維vector (Data)�2. for i_th row in training data : �3. Data[i_th row%18].append(every element in i_th row)�4. (可以順便處理rainfall的NR->設成0)

�Data會變成一個 的vector�

...

2014/1/1

2014/1/2

2014/1/3

5 of 12

如何抽取feature

一月份data

二月份data

三月份data

480

480

480

18

…...

18

…...

每10小時為一筆資料

6 of 12

如何抽取feature

(Pseudo code)�1. 宣告train_x儲存前9小時data,以及train_y紀錄第十小時pm2.5值�2. for i =1月、2月......�3. 取樣每連續10個小時:�4. train_x.append(前9小時所有data)�5. train_y.append(第10小時pm2.5值)�6. 在train_x每筆data中加入bias

7 of 12

實做linear regression

(Pseudo code)�1. 宣告weight vector、初始learning rate、# of iteration�2. for i_th iteration :�3. y’ = train_x 和 weight vector 的 內積�4. L = y’ - train_y �5. gra = 2*np.dot( (train_x)’ , L )�6. weight vector -= learning rate * gra

8 of 12

2. for i_th iteration :�3. y’ = train_x 和 weight vector 的 內積�4. L = y’ - train_y �5. gra = 2*np.dot( (train_x)T , L )�6. weight vector -= learning rate * gra

L =

gra = 2 *

p-dim vector

3.

4.

5.

9 of 12

Adagrad

10 of 12

實做linear regression

(Pseudo code)�1. 宣告weight vector、初始learning rate、# of iteration� 宣告prev_gra儲存每個iteration的gradient�2. for i_th iteration :�3. y’ = train_x 和 weight vector 的 內積�4. L = y’ - train_y�5. gra = 2*np.dot( (train_x)’ , L )� prev_gra += gra**2� ada = np.sqrt(prev_gra)�6. weight vector -= learning rate * gra / ada

11 of 12

預測 PM 2.5

(Pseudo code)�1. read test_x.csv�2. every 18 rows : �3. test_x.append([1])�4. test_x.append(這9小時的data)�5. test_y = np.dot( weight vector, test_x)

12 of 12

Reference

  1. Adagrad : �https://youtu.be/yKKNr-QKz2Q?list=PLJV_el3uVTsPy9oCRY30oBPNLCo89yu49&t=705