【问题标题】:How to access np array in blob storage in azure notebook如何在 azure notebook 中访问 blob 存储中的 np 数组
【发布时间】:2021-05-27 21:16:29
【问题描述】:

我已经看到了几个答案,但似乎都没有。

我在 blob 存储容器中有一个 .npy 文件,并想在机器学习工作区中使用它(我使用的是 azure notebooks)

如何访问它并将其加载到内存中以开始在其上训练模型?

Dataset.Tabular 没有 npy 作为可接受的文件类型来导入笔记本,但有 csv 和 parquet。我有多个维度,所以不确定其中任何一个对我有用吗?还是有一种简单的方法可以将我的 .npy 更改为 .csv,同时保持相同的结构?

【问题讨论】:

  • 你想知道如何将np数组转换为csv吗?
  • 是的,或者从 blob 存储中将我的 np 数组读入一个 azure notebook

标签: python azure blob


【解决方案1】:

关于这个问题,您可以将 np 数组转换为 pandas 数据框。然后您可以使用 pandas 数据框创建 TabularDataSet 或将 pandas 数据框转换为 csv 或 parquet 然后创建 TabularDataSet。

例如

# convert the np array into pandas dataframe
from azure.storage.blob.baseblobservice import BaseBlobService
import numpy as np

account_name = '<your account name>'
account_key = '<your account key>'
container_name = '<your container name>'
blob_name = '<your blob name>'

blob_service = BaseBlobService(
    account_name=account_name,
    account_key=account_key
)
sas_token = blob_service.generate_blob_shared_access_signature(container_name, blob_name, permission=BlobPermissions.READ, expiry=datetime.utcnow() + timedelta(hours=1))
print(sas_token)
url_with_sas = blob_service.make_blob_url(container_name, blob_name, sas_token=sas_token)
print(url_with_sas)
ds = np.DataSource()
# ds = np.DataSource(None)  # use with temporary file
# ds = np.DataSource(path) # use with path like `data/`
f = ds.open(url_with_sas)
dat = np.fromfile(f)

import pandas as pd

df = pd.DataFrame(my_array)

#create dataset
from azureml.core import Workspace, Dataset

ws = Workspace.from_config()
datastore = ws.get_default_datastore()
training_data = Dataset.Tabular.register_pandas_dataframe(
    df , datastore, 'EthereumRates')

更多详情请参考blogblog

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-07
    • 1970-01-01
    • 2017-02-07
    • 1970-01-01
    • 2016-05-16
    • 2017-09-16
    • 2020-06-03
    • 2021-12-18
    相关资源
    最近更新 更多