繁体   English   中英

将脚本导入 IBM WATSON STUDIO 中的笔记本

[英]Importing scripts into a notebook in IBM WATSON STUDIO

我正在 IBM WATSON Studio 免费版上的 CIFAR 10 图像上进行 PCA,所以我上传了用于在工作室上下载 CIFAR10 的 python 文件

下图。 在此处输入图片说明

但是当我尝试导入cache ,显示以下错误。 下图—— 在此处输入图片说明

在谷歌上花了一些时间后,我找到了一个解决方案,但我无法理解。 链接https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/add-script-to-notebook.html

the solution is as follows:- 
Click the Add Data icon (Shows the Add Data icon), and then browse the script file or drag it into your notebook sidebar.

Click in an empty code cell in your notebook and then click the Insert to code link below the file. Take the returned string, and write to a file in the file system that comes with the runtime session.

To import the classes to access the methods in a script in your notebook, use the following command:

For Python:

from <python file name> import <class name>

我无法理解这一行

` 并写入运行时会话附带的文件系统中的文件。``

在哪里可以找到运行时会话附带的文件? 文件系统位于何处?

任何人都可以帮助我了解在哪里可以找到该文件的详细信息

您有导入错误,因为您要导入的脚本在Python运行时的本地文件系统中不可用。 您上载的文件( cache.pycifar10.py等)被上载到与Watson Studio项目关联的对象存储桶中。 要使用这些文件,您需要使它们可用于Python运行时,例如,将脚本下载到运行时本地文件系统。

更新:同时,有一个选项可以直接插入StreamingBody对象。 这还将包括所有必需的凭据。 如果使用insert StreamingBody object选项,则可以跳过此操作, writing it to a file in the local runtime filesystem此答案writing it to a file in the local runtime filesystem部分。

在此处输入图片说明

要么,

您可以使用下面的代码片段读取StreamingBody对象中的脚本:

import types
import pandas as pd
from botocore.client import Config
import ibm_boto3

def __iter__(self): return 0
os_client= ibm_boto3.client(service_name='s3',
ibm_api_key_id='<IBM_API_KEY_ID>',
ibm_auth_endpoint="<IBM_AUTH_ENDPOINT>",
config=Config(signature_version='oauth'),
endpoint_url='<ENDPOINT>')

# Your data file was loaded into a botocore.response.StreamingBody object.
# Please read the documentation of ibm_boto3 and pandas to learn more about the possibilities to load the data.
# ibm_boto3 documentation: https://ibm.github.io/ibm-cos-sdk-python/
# pandas documentation: http://pandas.pydata.org/
streaming_body_1 = os_client.get_object(Bucket='<BUCKET>', Key='cifar.py')['Body']
# add missing __iter__ method, so pandas accepts body as file-like object
if not hasattr(streaming_body_1, "__iter__"): streaming_body_1.__iter__ = types.MethodType( __iter__, streaming_body_1 ) 

然后将其写入本地运行时文件系统中的文件。

f = open('cifar.py', 'wb')
f.write(streaming_body_1.read())

这将打开一个具有写访问权的文件,并调用write方法写入该文件。 然后,您应该能够简单地导入脚本。

import cifar

注:通过单击文件下拉菜单上的“ Insert credentials选项,可以获取文件的凭证,例如IBM_API_KEY_ID

op 发现指令遗漏了一行关键的代码。 我跟着他们,能够导入模块,但无法使用这些模块中的任何函数或类。 这是通过在写入后关闭文件来解决的。 说明中的这部分:

f = open('<myScript>.py', 'wb')
f.write(streaming_body_1.read())

应该是(至少在我的情况下这有效):

f = open('<myScript>.py', 'wb')
f.write(streaming_body_1.read())
f.close()

希望这有助于某人。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM