# 2024-04-30 行銷系 Python課程 筆記
## Python x ChatGPT
#### Introduction
https://platform.openai.com/docs/models/overview
#### Official API documentation
https://platform.openai.com/docs/api-reference
#### API Endpoint種類
| 名稱 | 說明 |
| ----------- | ---------------------- |
| Audio | 語音辨識 |
| Chat | 聊天API |
| Completions | 聊天API(舊版) |
| Embeddeding | 取得文字的向量值 |
| Fine-tuning | 微調自己的模型 |
| Files | 檔案操作 |
| Images | 圖片生成 |
| Models | 取得可用的模組相關資訊 |
#### Install OpenAI module
1. 打開 cmd
2. `cd C:\Users\user\anaconda3\Scripts`
3. `pip install openai`
#### Say hello to OpenAI
```python=
from openai import OpenAI
# create openai object
client = OpenAI(api_key='xxxxxxxx')
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user", "content": "Chào buổi sang"}
])
print(completion.choices[0].message.content)
```
> Reference: https://platform.openai.com/docs/api-reference/chat/create?lang=python
####
```python=
from openai import OpenAI
# create openai object
client = OpenAI(api_key='xxxxxx')
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "Please answer me in English"},
{"role": "user", "content": "你在哪裡"}
])
print(completion.choices[0].message.content)
```
#### Translator
```python=
from openai import OpenAI
# create openai object
client = OpenAI(api_key='xxxx')
while True:
user = input('Please input Chinese(quit=exit): ')
if user == 'quit':
break
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "Please translate my sentence in Vietnamese"},
{"role": "user", "content": user}
],
temperature=0)
print(completion.choices[0].message.content)
```
#### Translate to emoji
```python=
from openai import OpenAI
# create openai object
client = OpenAI(api_key='xxxx')
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You will be provided with text, and your task is to translate it into emojis. Do not use any regular text. Do your best with emojis only."},
{"role": "user", "content": 'Hate'}
],
temperature=1)
print(completion.choices[0].message.content)
```
#### More example
ChatGPT提供了更多官方API的各種應用可以參考。
https://platform.openai.com/examples
#### Image generate
```python=
from openai import OpenAI
# create openai object
client = OpenAI(api_key='{your-api-key}')
response = client.images.generate(
model='dall-e-3',
prompt='sun and tree',
n=1,
size='1024x1024')
print(response.data[0].url)
```
> Reference: https://platform.openai.com/docs/api-reference/images/create?lang=python
## Q&A
1. API回傳「You exceeded your current quota, please check your plan and billing details」訊息,例如:
```
{
"error": {
"message": "You exceeded your current quota, please check your plan and billing details.",
"type": "insufficient_quota",
"param": null,
"code": "insufficient_quota"
}
}
```
##### 答:
```
所有的API呼叫都是以Token為單位來計費,新帳號有18美元額度可以使用,期限三個月,過期或是使用完畢後,就必須付費使用。
```
2. Windows在安裝openai模組時出現「error: Microsoft Visual C++ 14.0 or greater is required" when installing Python packages?」錯誤。
##### 答:
```
因為有些相依模組需要透過build tool進行編譯與安裝,如果系統內沒有相對應版本的build tool就會發生失敗,可以試著安裝較舊版本的Python開發套件試試,例如: Python 3.9
```
> **補充:**
>
> Python各版本的官方載點:https://www.python.org/downloads/windows/
# 2024-04-09 行銷系 Python課程 筆記
#### 以關鍵字爬取Google Trends的趨勢資料
```python=
from pytrends.request import TrendReq
import openpyxl # import excel module
import time # import time module
# import LineChart module
from openpyxl.chart import LineChart, Reference
keyword = 'ChatGPT'
pytrend = TrendReq()
# 12 month
pytrend.build_payload(kw_list=[keyword], cat=0, timeframe='today 5-y', geo='TW')
data = pytrend.interest_over_time()
print(data)
# # Get current time
nowstr = time.strftime('%Y-%m-%d') # 以 2024-04-09 的格式來顯示時間
# nowstr = time.strftime('%Y-%m-%d-%H-%M') # 以 2024-04-09-11-37 的格式來顯示時間
print(nowstr)
# save google trend data to excel
data.to_excel(f'goolge-trends-{nowstr}.xlsx', sheet_name=keyword, index=True)
# Make excel line chart
wb = openpyxl.load_workbook(f'goolge-trends-{nowstr}.xlsx') # open excel file
# get active work sheet
ws = wb.active
print(ws)
# Create new line chart
chart = LineChart()
# do something
# set data
ref = Reference(ws, min_col=2, max_col=2, min_row=1, max_row=ws.max_row)
# set data to chart
chart.add_data(ref, titles_from_data=True)
# set line chart title
chart.title = 'Google Trends'
# change y-axis title
chart.y_axis.title = '資料筆數'
# set x-axis date format
chart.x_axis.number_format = 'yyyy/mm/dd'
# set date reference
xlabels = Reference(ws, min_col=1, min_row=2, max_row=ws.max_row)
# set date reference to chart
chart.set_categories(xlabels)
chart.width = 10 # cm
chart.height = 6 # cm
chart.style = 8
# put line chart to excel worksheet
ws.add_chart(chart, 'E2')
# wb = workbook
wb.save(f'goolge-trends-{nowstr}.xlsx')
```
# 2024-04-02 行銷系 Python課程 筆記
##### `list` 教學
清單方法:
| 方法名稱 | 說明 |
| ----------- | -------------------------- |
| `append()` | 新增一筆資料到清單的最後面 |
| `remove()` | 移除清單內某個元素值 |
| `reverse()` | 將清單內元素順序反過來 |
| `sort()` | 排序清單 |
##### 群集型態通用方法
通用方法可以使用在Python支援的各種群集型態上:
| 方法名稱 | 說明 |
| -------- | ------------------------------------ |
| `len()` | 取回該群基資料的長度(共有幾筆資料) |
| `del` | 刪除指定索引位置的元素 |
| `in` | 判斷資料是否存在於該群集資料中 |
```python=
# Creae a list
a = [3, 4, 5, 6, 7]
# Read list element(Using index operator [])
print(a[0])
# Get type of variable
print( type(a) )
b = 99
print( type(b) ) # int type = integer
# Update element in list
a[2] = 'ok'
print(a)
# delete element
a.remove(7) # by value
del a[0] # by index
print('After delete 7:', a)
# add data to list
a.append(100)
a.insert(0, 'aaron')
print(a)
```
## if
```python=
if False:
print('Run1')
print('Run2')
print('Run3')
print('Run4')
print('Run5')
print('Run6')
```
#### 比較運算
| 比較運算子 | 說明 |
| ---------- | ---------------------- |
| > | 大於 |
| >= | 大於或等於 |
| < | 小於 |
| <= | 小於或等於 |
| == | 等於 |
| != | 不等於 |
```python=
print('3 > 4 =', 3 > 4)
print('9 == 9', 9 == 9)
print('7 % 5 = ', 7 % 5)
print('10 % 3 = ', 10 % 3)
print('10 % 3 == 1 =', 10 % 3 == 1)
```
#### 加分題
1. `8 % 3`
> `2`
2. `19 % 5`
> `4`
3. `47 % 14`
> `8`
#### 奇數偶數判斷
```python=
a = 9998
if a % 2 == 1:
print('奇数: số lẻ')
else:
print('偶数: số chẵn')
```
#### 使用者輸入數字後判斷奇偶數
```python=
user = input('Please input a number: ')
print('user type:', type(user))
user = int( user ) # change type from string to integer
print('user type:', type(user))
if user % 2 == 1:
print(f'{user} is a 奇数: số lẻ')
else:
print(f'{user} is a 偶数: số chẵn')
```
#### str無法做數字計算
```python=
a = '35' # This is a str type
b = '62' # This is a str type
a = int(a) # Change a variable from str to int type
b = int(b)
print('a type is: ', type(a))
c = 3
d = 6
print('c type is: ', type(c))
print(a + b)
print(c + d)
```
## Google Trends
安裝`pytrends`
```python=
import pytrends
```
1. 打開 cmd
2. `cd C:\Users\user\anaconda3\Scripts`
3. `pip install pytrends`
> #### 並將結果儲存到Excel檔案後繪製成折線圖
![](https://hackmd.io/_uploads/ry-JjdHza.png)
#### !!! 注意 !!!
因為關鍵字熱度Google Trend有非常嚴格的存取次數限制,而該限制目前不明,因此建議盡可能完成程式功能後再進行執行程式。
如果遇到錯誤代碼:429,即表示因為超過次數被Google Trend封鎖了,此時可以嘗試切換`geo`參數,例如:
```python=
pytrend.build_payload(kw_list=[keyword], cat=0, timeframe='today 12-m', geo='TW')
```
變成:
```
pytrend.build_payload(kw_list=[keyword], cat=0, timeframe='today 12-m', geo='JP')
```
>geo code清單(ISO-3166 alpha 2欄位):https://www.geonames.org/countries/
## 需安裝的模組
| 模組名稱 | 安裝指令 | 說明 |
| -------- | ----------------------- | ---------------- |
| openpyxl | `pip3 install openpyxl` | Excel模組 |
| pytrends | `pip3 install pytrends` | Google Trend模組 |
#### 官方文件
pytrends: https://pypi.org/project/pytrends/#installation
openpyxl: https://openpyxl.readthedocs.io/en/stable/tutorial.html
## 常見例外
1. `pytrends.exceptions.TooManyRequestsError: The request failed: Google returned a response with code 429`
##### 說明
因為太頻繁用程式存取,被Google Trend網站擋住了。
2. `pytrends.exceptions.ResponseError: The request failed: Google returned a response with code 400`
##### 說明
呼叫Google Trend時,傳遞了錯誤的參數格式。
## 程式碼說明
```
pytrend.build_payload(kw_list=[keyword], cat=0, timeframe='today 12-m')
```
##### 說明:
設定Google Trend參數
##### 參數:
###### kw_list
要搜尋關鍵字清單,資料類型為list,可以輸入多個關鍵字
###### cat
要搜尋的類別
> 所有類別代號:https://github.com/pat310/google-trends-api/wiki/Google-Trends-Categories
###### geo
國家代碼,如果沒有提供則表示:全球
> geo code清單(ISO-3166 alpha 2欄位):https://www.geonames.org/countries/
###### timeframe
要取得資料的日期區間:
- Defaults to last 5yrs, `'today 5-y'`.
- Everything `'all'`
- Specific dates, 'YYYY-MM-DD YYYY-MM-DD' example `'2016-12-14 2017-01-25'`
- Specific datetimes, 'YYYY-MM-DDTHH YYYY-MM-DDTHH' example `'2017-02-06T10 2017-02-12T07'`
- Note Time component is based off UTC
- Current Time Minus Time Pattern:
- By Month: `'today #-m'` where # is the number of months from that date to pull data for
- For example: `'today 3-m'` would get data from today to 3months ago
- **NOTE** Google uses UTC date as *'today'*
- **Works for 1, 3, 12 months only!**
- Daily: `'now #-d'` where # is the number of days from that date to pull data for
- For example: `'now 7-d'` would get data from the last week
- **Works for 1, 7 days only!**
- Hourly: `'now #-H'` where # is the number of hours from that date to pull data for
- For example: `'now 1-H'` would get data from the last hour
- **Works for 1, 4 hours only!**
```python=
data = pytrend.interest_over_time()
```
##### 說明:
取得指定時間內被搜尋的次數,回傳為Pandas的DataFrame物件
```python=
from pytrends.request import TrendReq
pytrend = TrendReq()
pytrend.build_payload(kw_list=['Inter đấu với Empoli'], cat=0, timeframe='today 12-m', geo='TW')
data = pytrend.interest_over_time()
print(data)
```
# 2024-03-26 行銷系 Python課程 筆記
## 函式與變數
```python=
abc = 5
print('Aaron')
`
**說明:**
- abc為一個變數,裡面可以用來存放資料
- print後面有一對小括號,代表它是一個函式,可以用來完成某件事情
```
#### range()函式
```python=
# 產生連續數字
a = range(3)
# a目前裡面是range物件,轉型成list
a = list( a )
print(a)
```
**說明:**
1. 因為`range()`函式回傳的會是一個`range`物件,因此我們會先做一個資料轉型成`list`後才看的到裡面的資料。
2. 當一個函式匯回傳資料,我們必須使用變數和指派運算子把它存下來,否則資料會消失。
#### 關於變數
```python=
a = 3
a = 4
a = 5
```
輸出:
```
5
```
當一個變數多次被指派資料時,舊資料會被覆蓋,當舊資料已經不需要使用時,使用同一個變數可以節省記憶體。
## 桃園Ubike查詢程式
#### Google search keyword
```
桃園市 ubike opendata
```
#### URL
https://data.tycg.gov.tw/opendata/datalist/datasetMeta?oid=5ca2bfc7-9ace-4719-88ae-4034b9a5a55c
API = Application Program Interface(給程式使用的資料)
![Screen Shot 2024-03-26 at 10.27.08 AM](https://hackmd.io/_uploads/SJG6f2J10.png)
## 資料欄位說明
|索引|欄位名稱|說明|
|-|-|-|
|0|sareaen|行政區英文名|
|1|sarea|行政區中文名|
|2|lng|經度|
|3|sna|中文站名|
|4|snaen|英文站名|
|5|bemp|空位數量|
|6|ar|中文地址|
|7|act|全站禁用狀態(0:禁用、1:啟用)|
|8|sno|站編號|
|9|aren|英文地址|
|10|tot|場站總停車格|
|11|_id|資料編號|
|12|sbi|場站目前車輛數量|
|13|mday|微笑單車各場站來源資料更新時間|
|14|lat|緯度|
> **資料來源:**
>
> https://data.gov.tw/dataset/137993
## Code
```python=
import requests # 引入requests套件
import csv # 引入csv套件
# 將網址存放到變數
url = 'http://data.tycg.gov.tw/api/v1/rest/datastore/a1b4714b-3b75-4ff8-a8f2-cc377e4eaa0f?format=csv'
response = requests.get(url) # 到網路上去抓資料
print(response.text)
# 將資料切割成獨立的每一筆資料
data = response.text.splitlines()
# 將每一筆資料內的每一個欄位資料拆開
rows = list(csv.reader(data))
# 使用者輸入
user = input('請輸入要搜尋的站名: ')
# 使用for-in迴圈將每一筆資料取出
for row in rows:
# 判斷使用者輸入的站名
if user in row[3]:
print('Station:' + row[3]) # 站名
print('Space:' + row[5]) # 空位數量
print('Available:' + row[12]) # 可借數量
print()
```
**說明:**
- `requests`套件用來到網路上查資料
- CSV格式的檔案可以使用excel打開
## 可用關鍵字
#### 健行 火車站 中央大學
## 練習
1. 將目前範例的搜尋中文站名,改成搜尋英文站名