划痕实验 迁移面积自动统计
If you want to use Google Colaboratory to perform your data analysis, for building data pipelines and data visualizations, here is the beginners’ guide to migrate from one tool to the other.
如果您想使用Google Colaboratory进行数据分析,以建立数据管道和数据可视化,这是初学者指南,可以从一种工具迁移到另一种工具。
为什么要迁移? (Why migrate?)
To be honest, I came to using Colab, since I needed an iOS-friendly tool without a long installation adventure. The iOS version of Colab — actually, the iOS version of any browser — has a couple of problems, but I am currently quite satisfied.
老实说,我之所以开始使用Colab,是因为我需要一个iOS友好型工具,而无需进行长时间的安装。 iOS版的Colab(实际上是任何浏览器的iOS版)都有两个问题,但我目前非常满意。
The article is based upon my MacOS experience with Colab and the screenshots are also originate from MacOS.
本文基于我对Colab的MacOS经验,并且屏幕截图也来自MacOS。
创建一个Google帐户 (Create a Google account)
This is the first step, since you are going to work in a part of a Google ecosystem. Google Colaboratory (further referred to as Colab) runs in a cloud. You will use it in a browser.
这是第一步,因为您将要在Google生态系统的一部分中工作。 Google合作实验室(又称为Colab)在云中运行。 您将在浏览器中使用它。
下载谷歌浏览器 (Download Google Chrome)
Although the ultimate part of the world population is using Chrome (just kidding!), I still want to add this notion here. To access your Colab working space, you will need to log in to your Google account every time.
尽管世界人口的最终部分是使用Chrome(开个玩笑!),但我仍然想在此添加这个概念。 要访问您的Colab工作空间,您每次都需要登录到您的Google帐户。
In Chrome, you can stay logged and manage other spaces that you can integrate with Colab.
在Chrome中,您可以保持登录状态并管理可以与Colab集成的其他空间。
首次访问Colab (Access Colab for the first time)
You will need to open the following URL in your browser:
您将需要在浏览器中打开以下URL:
https://colab.research.google.com/
https://colab.research.google.com/
Or just type the “google colab” in the Google search engine.
或者,只需在Google搜索引擎中输入“ google colab”。
After you’ve created any Colab files (I’ll go through it in the next step) you will be also able to access your working space from Google Drive.
创建任何Colab文件后(我将在下一步中进行介绍),您还可以从Google云端硬盘访问您的工作空间。
上传您的Jupyter笔记本 (Upload your Jupyter notebooks)
With Colab, you are not using your Jupyter notebooks locally. You will need to add them to your Google Drive. You’ll get a Google Drive automatically when you create your Google account.
使用Colab,您将不会在本地使用Jupyter笔记本。 您需要将它们添加到您的Google云端硬盘。 创建Google帐户后,您会自动获得一个Google云端硬盘。
The first two are the official documentation.
前两个是官方文档。
Exactly, you do not need to convert your notebooks at all!
确实,您根本不需要转换笔记本!
Colab is built upon Jupyter and the format is the same. Pretty cool, if you decide to get back to the local Jupyter again!
Colab基于Jupyter构建,并且格式相同。 如果您决定再次回到当地的Jupyter,那就太酷了!
An upload form will appear each time you’ll open the Colab URL.
每次您打开Colab URL时,都会出现一个上传表单。
再次访问Colab (Access Colab again)
As I mentioned before, once you have any notebooks in there, a Colab folder will be automatically created in your Google Drive. The icon will have a yellow color. You can go to the folder, open it, double-click on a notebook, and then on “Open with Google Colaboratory”:
如前所述,一旦您有任何笔记本,就会在Google云端硬盘中自动创建一个Colab文件夹。 图标将显示为黄色。 您可以转到文件夹,将其打开,双击笔记本,然后单击“使用Google Colaboratory打开”:
If you entry point in the Colab URL, then the list of your recently used notebooks will pop up. You can switch to uploading notebooks, or pick them from you Google Drive, or even from you GitHub. Read more on GitHub here.
如果您在Colab URL中输入入口,则将弹出您最近使用过的笔记本的列表。 您可以切换到上传笔记本,也可以从Google云端硬盘甚至是GitHub中选择笔记本。 了解更多关于GitHub上这里 。
开始工作 (Start working)
The interface is pretty much the same. You access the usual features through the panel atop of the notebook.
界面几乎相同。 您可以通过笔记本顶部的面板访问常规功能。
You have a table of the contents on the left side and an overview of your files.
您在左侧有一个目录表,并提供了文件概述。
But …
但是...
安装套件 (Installing packages)
You’ll soon notice that the import command sometimes fails.
您很快就会注意到,导入命令有时会失败。
Colab has pandas, but for less popular packages you’ll need a pip installation. Since you do not have access to the respective Terminal, — remember, Colab runs in a cloud! — you’ll have to type the command inside the notebook.
Colab有熊猫,但是对于不太受欢迎的软件包,您需要安装pip。 由于您无权访问各自的终端,请记住,Colab在云中运行! —您必须在笔记本电脑中键入命令。
And put an exclamation mark before pip:
并在点子前加上一个感叹号:
!pip install nest_asyncioimport nest_asyncio
Do not forget to import the packages you’ve installed.
不要忘记导入已安装的软件包。
插入外部数据 (Inserting external data)
从本地硬盘上传 (Upload from your local hard drive)
You’ll have to use a native workaround to import data from the files stored on your PC locally.
您必须使用本机解决方法从本地存储在PC中的文件中导入数据。
You will have to use an upload package that Google created for you.
您将必须使用Google为您创建的上传包。
Just go ahead and run the code to be asked to select your files:
只需继续运行并运行要求您选择文件的代码即可:
from google.colab import filesuploaded = files.upload()
Then you can refer to this file in your code, for instance:
然后,您可以在代码中引用此文件,例如:
with open(‘local test.txt’, ‘r’, newline=’\r\n’) as f: print(f.read().splitlines())
You can replace read().splitlines()) with any other method you prefer.
您可以使用您喜欢的任何其他方法替换read()。splitlines())。
If you want to upload the next one, you can just re-run the upload code.
如果您要上传下一个,只需重新运行上传代码即可。
Of course, you can upload .CSV and easily turn it into a data frame:
当然,您可以上传.CSV并将其轻松转换为数据框:
In this regard, there is no difference between handling the files in Jupyter and in Colab. In Colab you only add the native import command to call the pop-up window to access your file system from the notebook.
在这方面,在Jupyter和Colab中处理文件没有区别。 在Colab中,您仅添加本机导入命令来调用弹出窗口以从笔记本计算机访问文件系统。
连接到Google云端硬盘 (Connect to Google Drive)
Basically, you can import/export data from/to Google Drive in the form of either TXT or CSV. You do not have to install a Google Drive app. You are going to connect to it in the browser.
基本上,您可以使用TXT或CSV格式从Google云端硬盘导入数据/将数据导出到Google云端硬盘。 您不必安装Google云端硬盘应用。 您将在浏览器中连接到它。
from google.colab import drivedrive.mount(‘/content/drive’)
Click on the URL, click “Allow” and then copy the authorization code. Paste the code into the long cell and click “Enter”. The connection is there.
单击URL,单击“允许”,然后复制授权代码。 将代码粘贴到长单元格中,然后单击“ Enter”。 连接在那里。
You can read more about it in the official documentation.
您可以在官方文档中了解更多信息。
The same open command works to insert data from a file stored in the Drive. You only need to add the Google Drive path:
相同的open命令可从存储在驱动器中的文件中插入数据。 您只需要添加Google云端硬盘路径:
使用Google云端硬盘 (Use Google Drive Sheets)
Google developed a python package to read data from Google Sheets.
Google开发了一个python包来从Google表格中读取数据。
!pip install — upgrade gspreadfrom google.colab import authauth.authenticate_user()import gspreadfrom oauth2client.client import GoogleCredentialsgc = gspread.authorize(GoogleCredentials.get_application_default())worksheet = gc.open(‘grandresult_2020–07–29 13:50:19.096165’).sheet1rows = worksheet.get_all_values()grandresult = pandas.DataFrame.from_records(rows)
You install it over the “cloud” pip, then get an authorization to use Sheets. Be carefully, this connection and the previous one (that you launch with drive.mount) are separated.
您通过“云”点安装它,然后获得使用表格的授权。 请注意,此连接与上一个连接(使用drive.mount启动)分开。
You pick up your file and then the first sheet: “sheet1” stands for the sheet number one and not a name of your sheet!
您先提取文件,然后提取第一个工作表:“ sheet1”代表工作表编号1,而不是工作表名称!
You can directly start with pandas.
您可以直接从熊猫开始。
通过Python本机包获取数据 (Fetching data by means of Python native packages)
If you download data from an API request, or parse an HTML with a BeautifulSoup, or any other native Python package, then it works in a usual way.
如果您从API请求中下载数据,或者使用BeautifulSoup或任何其他本机Python包解析HTML,那么它通常会起作用。
汇出资料 (Exporting data)
作为.TXT (As .TXT)
You can either export data by replacing the read command with the write command and adding a download line like that:
您可以通过将read命令替换为write命令并添加如下下载行来导出数据:
df = df.to_string() #to convert your data frame into .TXT filewith open(‘local test.txt’, ‘w’) as f: f.write(df)files.download(‘local text.txt’)
作为.CSV (As .CSV)
Or you can download a .CSV:
或者,您可以下载.CSV:
df.to_csv(‘local test.csv’)files.download(‘local test.csv’)
作为Google表格 (As a Google Sheet)
This code snippet allows to save a data frame into a Google Sheet:
此代码段允许将数据框保存到Google表格中:
from gspread_dataframe import set_with_dataframegc.create(‘Title of your Google Sheet’)sheet = gc.open(‘Title of your Google Sheet’).sheet1set_with_dataframe(‘Title of your Google Sheet’, df)
iOS和数据导入/导出 (iOS and data import/export)
We do not have the usual file path to save anything in the iOS file system. This makes exporting your data for storing it locally a ghost feature.
我们没有通常的文件路径将任何内容保存在iOS文件系统中。 这使得导出数据以将其存储在本地成为重影功能。
In general, if you download any file from Safari on an iOS device, you’ll have to access the saved files directly from the Safari by clicking on the icon:
通常,如果您在iOS设备上从Safari下载任何文件,则必须直接通过以下图标从Safari访问保存的文件:
Although many recent iOS devices do have a file browser, in my case, I could not find the data I’ve exported from a Colab notebook as a .CSV.
尽管最近的许多iOS设备确实具有文件浏览器,但就我而言,我找不到从Colab笔记本中导出为.CSV的数据。
So, I stick to the Google Sheet or TXT method.
因此,我坚持使用Google Sheet或TXT方法。
为什么实际上叫Colab? (Why is it actually called Colab?)
Now, I want to mention a couple of features that were supposed to make Colab a better breed.
现在,我想谈谈一些应该使Colab成为更好品种的功能。
- You can use it instead of arranging a server to run complex pipelines in the Jupyter notebook format. 您可以使用它代替安排服务器以Jupyter笔记本格式运行复杂的管道。
- You can access it from everywhere, as long as you have a network coverage. 只要您具有网络覆盖范围,就可以从任何地方访问它。
- For some data manipulations, it is faster, even without a Pro membership. 对于某些数据操作,即使没有Pro成员身份,它也更快。
But, mainly, it was designed to let more than one person to work on the same notebook and communicate with co-workers without leaving the notebook and involving some additional communication channel.
但是,主要是,它的设计目的是让一个以上的人可以在同一个笔记本上工作并与同事进行交流,而不必离开笔记本并涉及一些其他的交流渠道。
评论细胞 (Commenting on the cells)
This small icon stands for the commenting function. You can leave comments for your co-workers, but you do not have to include them into the code with # on the beginning or create an additional text cell.
这个小图标代表注释功能。 您可以为同事留下评论,但不必将其包含在代码中,并以#开头或创建其他文本单元格。
You can edit and delete your comments. After the issues you were asking about in your note were solved press “Resolve” to hide the comment thread.
您可以编辑和删除您的评论。 解决了您在便笺中询问的问题后,请按“解决”以隐藏评论主题。
This is easier if you just want to leave a note that is only a temporary one and does not belong into the final code.
如果您只想留下一个暂时的注释,并且不属于最终代码,则这样会更容易。
另一个精美的UI功能:更改主题 (Another fancy UI feature: changing the theme)
It got more complicated with the packages, but there is a compensation for it: you do not need any inline command to switch the color theme!
这些软件包变得更加复杂,但是有一个补偿:您不需要任何内联命令来切换颜色主题!
Unfortunately, only three themes — dark, light, and adaptive — are currently available. Less choice, less doubts!
不幸的是,目前只有三个主题:黑暗,明亮和自适应。 更少的选择,更少的疑问!
改变语言 (Changing the language)
The last feature I had to refer to quite often, especially, while writing this manual.
我不得不经常参考的最后一个功能,特别是在编写本手册时。
A Colab notebook will be shown in your local language, regardless of the language you’ve set for your entire Google profile.
无论您为整个Google个人资料设置了哪种语言,都会以您的本地语言显示Colab笔记本。
There is one single button in Colab, always in the same place, under the “Help” section in the top panel:
在Colab中,只有一个按钮始终位于同一位置,位于顶部面板的“帮助”部分下:
It is always the last one in the drop down.
它始终是下拉列表中的最后一个。
简短的后记 (A short afterword)
You will definitely discover even more interesting features and new horizons in using Colab. I’ll stop here, since this is the ultimate list I found important so far.
使用Colab一定会发现更多有趣的功能和新视野。 我将在这里停止,因为这是到目前为止我发现很重要的最终清单。
翻译自: https://medium.com/swlh/migrating-from-jupyter-to-colaboratory-2888332d57a7
划痕实验 迁移面积自动统计
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/388518.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!