划痕实验 迁移面积自动统计_从Jupyter迁移到合作实验室
劃痕實(shí)驗(yàn) 遷移面積自動(dòng)統(tǒng)計(jì)
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進(jìn)行數(shù)據(jù)分析,以建立數(shù)據(jù)管道和數(shù)據(jù)可視化,這是初學(xué)者指南,可以從一種工具遷移到另一種工具。
為什么要遷移? (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.
老實(shí)說(shuō),我之所以開(kāi)始使用Colab,是因?yàn)槲倚枰粋€(gè)iOS友好型工具,而無(wú)需進(jìn)行長(zhǎng)時(shí)間的安裝。 iOS版的Colab(實(shí)際上是任何瀏覽器的iOS版)都有兩個(gè)問(wèn)題,但我目前非常滿意。
The article is based upon my MacOS experience with Colab and the screenshots are also originate from MacOS.
本文基于我對(duì)Colab的MacOS經(jīng)驗(yàn),并且屏幕截圖也來(lái)自MacOS。
創(chuàng)建一個(gè)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.
這是第一步,因?yàn)槟鷮⒁贕oogle生態(tài)系統(tǒng)的一部分中工作。 Google合作實(shí)驗(yàn)室(又稱為Colab)在云中運(yùn)行。 您將在瀏覽器中使用它。
下載谷歌瀏覽器 (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(開(kāi)個(gè)玩笑!),但我仍然想在此添加這個(gè)概念。 要訪問(wèn)您的Colab工作空間,您每次都需要登錄到您的Google帳戶。
In Chrome, you can stay logged and manage other spaces that you can integrate with Colab.
在Chrome中,您可以保持登錄狀態(tài)并管理可以與Colab集成的其他空間。
首次訪問(wèn)Colab (Access Colab for the first time)
You will need to open the following URL in your browser:
您將需要在瀏覽器中打開(kāi)以下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.
創(chuàng)建任何Colab文件后(我將在下一步中進(jìn)行介紹),您還可以從Google云端硬盤訪問(wèn)您的工作空間。
上傳您的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,您將不會(huì)在本地使用Jupyter筆記本。 您需要將它們添加到您的Google云端硬盤。 創(chuàng)建Google帳戶后,您會(huì)自動(dòng)獲得一個(gè)Google云端硬盤。
The pop-up window you see after opening the Colab URL: your recently used notebooks打開(kāi)Colab URL后看到的彈出窗口:您最近使用過(guò)的筆記本The first two are the official documentation.
前兩個(gè)是官方文檔。
The upload tab in the same pop-up window: upload your Jupyter notebooks同一彈出窗口中的上載選項(xiàng)卡:上載Jupyter筆記本Exactly, you do not need to convert your notebooks at all!
確實(shí),您根本不需要轉(zhuǎn)換筆記本!
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構(gòu)建,并且格式相同。 如果您決定再次回到當(dāng)?shù)氐腏upyter,那就太酷了!
An upload form will appear each time you’ll open the Colab URL.
每次您打開(kāi)Colab URL時(shí),都會(huì)出現(xiàn)一個(gè)上傳表單。
再次訪問(wèn)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”:
如前所述,一旦您有任何筆記本,就會(huì)在Google云端硬盤中自動(dòng)創(chuàng)建一個(gè)Colab文件夾。 圖標(biāo)將顯示為黃色。 您可以轉(zhuǎn)到文件夾,將其打開(kāi),雙擊筆記本,然后單擊“使用Google Colaboratory打開(kāi)”:
Colab Notebooks folder in your Google Drive spaceGoogle云端硬盤空間中的Colab Notebooks文件夾 Select and a notebook and double-click on it選擇一個(gè)筆記本,然后雙擊它 Click “Open with Colab” on in the top middle (sometimes it appears in the bottom, too)單擊頂部中間的“ Open with Colab”(有時(shí)也顯示在底部)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中輸入入口,則將彈出您最近使用過(guò)的筆記本的列表。 您可以切換到上傳筆記本,也可以從Google云端硬盤甚至是GitHub中選擇筆記本。 了解更多關(guān)于GitHub上這里 。
Opening your notebooks stored in Google Drive and on GitHub打開(kāi)存儲(chǔ)在Google云端硬盤和GitHub中的筆記本開(kāi)始工作 (Start working)
The interface is pretty much the same. You access the usual features through the panel atop of the notebook.
界面幾乎相同。 您可以通過(guò)筆記本頂部的面板訪問(wèn)常規(guī)功能。
Inside the notebook: the top panel筆記本內(nèi)部:頂部面板You have a table of the contents on the left side and an overview of your files.
您在左側(cè)有一個(gè)目錄表,并提供了文件概述。
Easy navigate in your notebook: the table of the contents在筆記本中輕松瀏覽:目錄But …
但是...
安裝套件 (Installing packages)
You’ll soon notice that the import command sometimes fails.
您很快就會(huì)注意到,導(dǎo)入命令有時(shí)會(huì)失敗。
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有熊貓,但是對(duì)于不太受歡迎的軟件包,您需要安裝pip。 由于您無(wú)權(quán)訪問(wèn)各自的終端,請(qǐng)記住,Colab在云中運(yùn)行! —您必須在筆記本電腦中鍵入命令。
And put an exclamation mark before pip:
并在點(diǎn)子前加上一個(gè)感嘆號(hào):
!pip install nest_asyncioimport nest_asyncioDo not forget to import the packages you’ve installed.
不要忘記導(dǎo)入已安裝的軟件包。
插入外部數(shù)據(jù) (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.
您必須使用本機(jī)解決方法從本地存儲(chǔ)在PC中的文件中導(dǎo)入數(shù)據(jù)。
You will have to use an upload package that Google created for you.
您將必須使用Google為您創(chuàng)建的上傳包。
Just go ahead and run the code to be asked to select your files:
只需繼續(xù)運(yùn)行并運(yùn)行要求您選擇文件的代碼即可:
from google.colab import filesuploaded = files.upload()Choose files to upload from your local drive選擇要從本地驅(qū)動(dòng)器上載的文件 Results of your upload: ‘local test.txt” is now stored on your virtual machine上傳結(jié)果:“ local test.txt”現(xiàn)在存儲(chǔ)在虛擬機(jī)上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())Data from a .TXT file.TXT文件中的數(shù)據(jù)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.
如果您要上傳下一個(gè),只需重新運(yùn)行上傳代碼即可。
Of course, you can upload .CSV and easily turn it into a data frame:
當(dāng)然,您可以上傳.CSV并將其輕松轉(zhuǎn)換為數(shù)據(jù)框:
The same procedure to upload a .CSV file …上載.CSV文件的步驟相同... … which you easily convert into a data frame!…您可以輕松將其轉(zhuǎn)換為數(shù)據(jù)框!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中處理文件沒(méi)有區(qū)別。 在Colab中,您僅添加本機(jī)導(dǎo)入命令來(lái)調(diào)用彈出窗口以從筆記本計(jì)算機(jī)訪問(wèn)文件系統(tǒng)。
連接到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云端硬盤導(dǎo)入數(shù)據(jù)/將數(shù)據(jù)導(dǎo)出到Google云端硬盤。 您不必安裝Google云端硬盤應(yīng)用。 您將在瀏覽器中連接到它。
from google.colab import drivedrive.mount(‘/content/drive’)Filling in the form to connect to Google Drive填寫表格以連接到Google云端硬盤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,單擊“允許”,然后復(fù)制授權(quán)代碼。 將代碼粘貼到長(zhǎng)單元格中,然后單擊“ 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命令可從存儲(chǔ)在驅(qū)動(dòng)器中的文件中插入數(shù)據(jù)。 您只需要添加Google云端硬盤路徑:
Accessing the same txt-data in Google Drive在Google云端硬盤中訪問(wèn)相同的txt數(shù)據(jù)使用Google云端硬盤 (Use Google Drive Sheets)
Google developed a python package to read data from Google Sheets.
Google開(kāi)發(fā)了一個(gè)python包來(lái)從Google表格中讀取數(shù)據(jù)。
!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.
您通過(guò)“云”點(diǎn)安裝它,然后獲得使用表格的授權(quán)。 請(qǐng)注意,此連接與上一個(gè)連接(使用drive.mount啟動(dòng))分開(kāi)。
You pick up your file and then the first sheet: “sheet1” stands for the sheet number one and not a name of your sheet!
您先提取文件,然后提取第一個(gè)工作表:“ sheet1”代表工作表編號(hào)1,而不是工作表名稱!
You can directly start with pandas.
您可以直接從熊貓開(kāi)始。
通過(guò)Python本機(jī)包獲取數(shù)據(jù) (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請(qǐng)求中下載數(shù)據(jù),或者使用BeautifulSoup或任何其他本機(jī)Python包解析HTML,那么它通常會(huì)起作用。
匯出資料 (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:
您可以通過(guò)將read命令替換為write命令并添加如下下載行來(lái)導(dǎo)出數(shù)據(jù):
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:
此代碼段允許將數(shù)據(jù)框保存到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和數(shù)據(jù)導(dǎo)入/導(dǎo)出 (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.
我們沒(méi)有通常的文件路徑將任何內(nèi)容保存在iOS文件系統(tǒng)中。 這使得導(dǎo)出數(shù)據(jù)以將其存儲(chǔ)在本地成為重影功能。
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設(shè)備上從Safari下載任何文件,則必須直接通過(guò)以下圖標(biāo)從Safari訪問(wèn)保存的文件:
You can pick up your downloaded files only inside Safari and only during the current session您只能在Safari中并且僅在當(dāng)前會(huì)話期間才能下載下載的文件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設(shè)備確實(shí)具有文件瀏覽器,但就我而言,我找不到從Colab筆記本中導(dǎo)出為.CSV的數(shù)據(jù)。
So, I stick to the Google Sheet or TXT method.
因此,我堅(jiān)持使用Google Sheet或TXT方法。
為什么實(shí)際上叫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.
現(xiàn)在,我想談?wù)勔恍?yīng)該使Colab成為更好品種的功能。
- You can use it instead of arranging a server to run complex pipelines in the Jupyter notebook format. 您可以使用它代替安排服務(wù)器以Jupyter筆記本格式運(yùn)行復(fù)雜的管道。
- You can access it from everywhere, as long as you have a network coverage. 只要您具有網(wǎng)絡(luò)覆蓋范圍,就可以從任何地方訪問(wèn)它。
- For some data manipulations, it is faster, even without a Pro membership. 對(duì)于某些數(shù)據(jù)操作,即使沒(méi)有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.
但是,主要是,它的設(shè)計(jì)目的是讓一個(gè)以上的人可以在同一個(gè)筆記本上工作并與同事進(jìn)行交流,而不必離開(kāi)筆記本并涉及一些其他的交流渠道。
評(píng)論細(xì)胞 (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.
這個(gè)小圖標(biāo)代表注釋功能。 您可以為同事留下評(píng)論,但不必將其包含在代碼中,并以#開(kāi)頭或創(chuàng)建其他文本單元格。
Add comment to a cell在單元格中添加評(píng)論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.
您可以編輯和刪除您的評(píng)論。 解決了您在便箋中詢問(wèn)的問(wèn)題后,請(qǐng)按“解決”以隱藏評(píng)論主題。
Edit or delete your comment編輯或刪除您的評(píng)論 Resolve a comment thread: it will disappear.解決評(píng)論線程:它將消失。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.
如果您只想留下一個(gè)暫時(shí)的注釋,并且不屬于最終代碼,則這樣會(huì)更容易。
另一個(gè)精美的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!
這些軟件包變得更加復(fù)雜,但是有一個(gè)補(bǔ)償:您不需要任何內(nèi)聯(lián)命令來(lái)切換顏色主題!
Change the notebook theme: accessible from the settings menu on the top right (the gear icon)更改筆記本主題:可從右上角的設(shè)置菜單訪問(wèn)(齒輪圖標(biāo))Unfortunately, only three themes — dark, light, and adaptive — are currently available. Less choice, less doubts!
不幸的是,目前只有三個(gè)主題:黑暗,明亮和自適應(yīng)。 更少的選擇,更少的疑問(wèn)!
改變語(yǔ)言 (Changing the language)
The last feature I had to refer to quite often, especially, while writing this manual.
我不得不經(jīng)常參考的最后一個(gè)功能,特別是在編寫本手冊(cè)時(shí)。
A Colab notebook will be shown in your local language, regardless of the language you’ve set for your entire Google profile.
無(wú)論您為整個(gè)Google個(gè)人資料設(shè)置了哪種語(yǔ)言,都會(huì)以您的本地語(yǔ)言顯示Colab筆記本。
There is one single button in Colab, always in the same place, under the “Help” section in the top panel:
在Colab中,只有一個(gè)按鈕始終位于同一位置,位于頂部面板的“幫助”部分下:
Switching to English from any other world language從其他世界語(yǔ)言切換為英語(yǔ)It is always the last one in the drop down.
它始終是下拉列表中的最后一個(gè)。
簡(jiǎn)短的后記 (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一定會(huì)發(fā)現(xiàn)更多有趣的功能和新視野。 我將在這里停止,因?yàn)檫@是到目前為止我發(fā)現(xiàn)很重要的最終清單。
翻譯自: https://medium.com/swlh/migrating-from-jupyter-to-colaboratory-2888332d57a7
劃痕實(shí)驗(yàn) 遷移面積自動(dòng)統(tǒng)計(jì)
總結(jié)
以上是生活随笔為你收集整理的划痕实验 迁移面积自动统计_从Jupyter迁移到合作实验室的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 基于plotly数据可视化_[Plotl
- 下一篇: 数据开放 数据集_除开放式清洗之外:叙述