2023-10-17 課程補充資料 Week 06

Demo Project to show Gradient Descent

  1. PyCharm code, 資料來源: LINK
import tkinter
import matplotlib
import matplotlib.pyplot as plt

matplotlib.use('tkagg')
# %matplotlib inline
import random as random
import numpy as np
import csv

x_data = [338., 333., 328., 207., 226., 25., 179., 60., 208., 606.]
y_data = [640., 633., 619., 393., 428., 27., 193., 66., 226., 1591.]

x = np.arange(-200, -100, 1)  # bias
y = np.arange(-5, 5, 0.1)  # weight
Z = np.zeros((len(x), len(y)))
X, Y = np.meshgrid(x, y)
for i in range(len(x)):
    for j in range(len(y)):
        b = x[i]
        w = y[j]
        Z[j][i] = 0
        for n in range(len(x_data)):
            Z[j][i] = Z[j][i] + (y_data[n] - b - w * x_data[n]) ** 2
        Z[j][i] = Z[j][i] / len(x_data)

# ydata = b + w * xdata
b = -120  # initial b
w = -4  # initial w
lr = 0.000001  # learning rate
iteration = 100000

b_lr = 0.0
w_lr = 0.0

# Store initial values for plotting.
b_history = [b]
w_history = [w]

# Iterations
for i in range(iteration):

    b_grad = 0.0
    w_grad = 0.0
    for n in range(len(x_data)):
        b_grad = b_grad - 2.0 * (y_data[n] - b - w * x_data[n]) * 1.0
        w_grad = w_grad - 2.0 * (y_data[n] - b - w * x_data[n]) * x_data[n]

    b_lr = b_lr + b_grad ** 2
    w_lr = w_lr + w_grad ** 2

    # Update parameters.
    b = b - lr / np.sqrt(b_lr) * b_grad
    w = w - lr / np.sqrt(w_lr) * w_grad

    # Store parameters for plotting
    b_history.append(b)
    w_history.append(w)

# plot the figure
plt.contourf(x, y, Z, 50, alpha=0.5, cmap=plt.get_cmap('jet'))
plt.plot([-188.4], [2.67], 'x', ms=12, markeredgewidth=3, color='orange')
plt.plot(b_history, w_history, 'o-', ms=3, lw=1.5, color='black')
plt.xlim(-200, -100)
plt.ylim(-5, 5)
plt.xlabel(r'$b$', fontsize=16)
plt.ylabel(r'$w$', fontsize=16)
plt.show()

DEMO 1 task
用 python 計算某一個新聞網頁出現 Taiwan 關鍵字的次數

with open('demo_1003.html') as f:
    lines = f.read()

words = lines.split()
#print(words[0])

search_word = "Taiwan"
count = words.count(search_word)
print("搜尋結果(", search_word, ")", count)

DEMO 2 task
1. 登入
2. 修改暱稱
3. 修改密碼
4. 修改大頭貼
可以使用 Postman, 不過最好是嚐試使用 python 來進行.

今天 API 結果 URL
https://www.cutexyz.com/edu/mlearning2309/API/

如果要看透過學號與中文姓名登入的結果:
https://www.cutexyz.com/edu/mlearning2309/API/index2.php

今日會使用的 API List

API 1: login
Protocol: HTTPS POST
API URL: https://www.cutexyz.com/edu/mlearning2309/API/api_login.php
Parameters:
1. student_uid: 學號
2. pwd: 密碼, 預設為 cute301, 沒有忘記密碼功能, 如果有人忘記, 請先與老師通知
Return Data ( JSON FORMAT ):
1. result: 結果, OK 代表沒問題, ERROR 代表錯誤發生
2. data: 內含唯一資料 token: 用於登入之後呼叫別的 API 搭配使用
3. msg: 如果有特殊裝框需要詳述問題時就會在這邊顯示

如果要看透過學號與中文姓名登入的結果:
https://www.cutexyz.com/edu/gmlearning2309/API/index2.php

API List

API 2: updateprofile
Protocol: HTTPS POST
API URL: https://www.cutexyz.com/edu/mlearning2309/API/api_updateprofile.php
Parameters:
1. student_uid: 學號
2. token: 你在登入後, 由系統發給你的動態密碼, 有效期內這個密碼不會變動
3. mode: 你想使用的功能; 1: 修改暱稱, 2: 修改密碼, 3: 上傳大頭貼
4.value1:帶入的參數, mode 1 請帶入暱稱, mode 2 輕帶入新的密碼, mode 3 請帶入檔案( jpg 限定)
Return Data ( JSON FORMAT ):
1. result: 結果, OK 代表沒問題, ERROR 代表錯誤發生
2. data: null 本 API 不會使用
3. msg: 如果有特殊裝框需要詳述問題時就會在這邊顯示