iT邦幫忙

2021 iThome 鐵人賽

DAY 21
0
AI & Data

我不太懂 AI,可是我會一點 Python 和 Azure系列 第 21

Day 21 Azure machine learning: Upload data- 自己的資料自己傳

Azure machine learning: Upload data- 自己的資料自己傳

要做匯率的預測模型,就必須準備匯率的歷史資料,我想investing.com是不錯的資料來源,資料的歷史夠久,涵蓋各個國家的各項金融商品:債券、憑證、期貨、指數和股票,應有盡有(但沒有臺灣指數期貨QQ)。也有Python套件可以使用,套件也會隨著 investing.com 更新。取得歷史資料後,經過 normalization 後,就可以將資料上傳到datastore ,以便後續在workspace訓練模型時使用。

安裝Python套件

pip3.7 install investpy
pip3.7 install scikit-learn

示範程式

from datetime import datetime
import os
import pickle
import investpy
from sklearn.preprocessing import MinMaxScaler


# 準備一個名叫 currency 的資料夾
if not os.path.isdir("currency"):
    os.system("mkdir currency")

# 從 investing.com 取得臺幣與美金的歷史匯率,取得每天的開盤價、最高價、最低價和收盤價
# 由於不知道資料從何時開始,我們先設定一個夠古老的日期,西元1900年01月01日開始
usd_twd = investpy.get_currency_cross_historical_data(
    "USD/TWD",
    from_date="01/01/1900", 
    to_date=datetime.now().strftime("%d/%m/%Y"),
)
# 拿到的資料是 pandas DataFrame,所以可以使用 pandas 的功能
usd_twd.reset_index(inplace=True)
usd_twd.to_csv("currency/usd_twd.csv", index=False)
# 將每天的收盤價作 normalization 調整成 0 ~ 1 之間,即 (x - min(x)) / (max(x) - min(x))
currency_data = usd_twd.Close.values.reshape(-1, 1)
scaler = MinMaxScaler(feature_range=(0, 1))
scaler.transform(currency_data)
# 將 scaler 存成 pickle 檔,方便之後使用
with open("currency/scaler.pickle", "wb") as f_h:
    pickle.dump(scaler, f_h)
f_h.close()

# 先取 2010/01/01 至 2020/12/31 的資料作為訓練資料
currency_data = usd_twd[
    (usd_twd.Date >= "2010-01-01") & (usd_twd.Date < "2021-01-01")
]
# 把資料存成 csv 檔,放到 currency 資料夾
currency_data.to_csv("currency/training_data.csv")

upload_file.py

"""
Upload data to Azure machine learning
"""
import os
import argparse
from azureml.core import Workspace, Dataset
from azureml.core.authentication import InteractiveLoginAuthentication


# 為了方便可以重複使用,上傳不同的資料,所以用 command-line 帶入參數執行
# folder:本地端的資料夾,內含欲上傳資料
# target_folder:替上傳到 datastore 後的資料夾命名
# dataname:為上傳的資料集命名,會顯示在 workspace 的資料頁面中
def parse_args():
    """
    Parse arguments
    """
    parser = argparse.ArgumentParser()
    parser.add_argument("-f", "--folder", help="file folder", type=str)
    parser.add_argument(
        "-t", "--target_folder", help="file folder in datastore", type=str
    )
    parser.add_argument("-n", "--dataname", help="name of dataset", type=str)
    args = parser.parse_args()
    return args


def main():
    """
    Upload data to Azure machine learning
    """
    args = parse_args()
    interactive_auth = InteractiveLoginAuthentication(tenant_id=os.getenv("TENANT_ID"))
    work_space = Workspace.from_config(auth=interactive_auth)
    # workspace 有預設的 datastore,把資料存在預設的 datastore
    datastore = work_space.get_default_datastore()
    # 上傳資料
    datastore.upload(
        src_dir=args.folder, target_path=args.target_folder, overwrite=True
    )
    # 上傳資料之後,從 datastore 取得資料所在的資料夾,並將資料集註冊
    dataset = Dataset.File.from_files(path=(datastore, args.target_folder))
    dataset.register(work_space, name=args.dataname)


if __name__ == "__main__":
    main()

取得匯率資料,也準備好upload_file.py,就可以直接在 terminal 執行,上傳資料

python3.7 upload_file.py --folder currency --target_folder currency --dataname currency

從相對路徑currency,上傳到 datastore 的currency資料夾,註冊資料集的名稱也為 currency。

點進瀏覽,也就能看到已經上傳的各個檔案了。


資料準備好了,還要準備環境,確認需要用到的套件都備齊了,才能確保在workspace上程式執行比較不會有問題,所以,下一篇介紹如何設置環境。


上一篇
Day 20 Azure machine learning: "Hello Azure" experiment- 試一下最簡單的實驗
下一篇
Day 22 Azure machine learning: set environment- 準備一個大家都能用的環境
系列文
我不太懂 AI,可是我會一點 Python 和 Azure30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言