02 Jupyter入門(Getting started with Jupyter notebooks)
《Python數(shù)據(jù)分析技術(shù)?!返?1章 02 Jupyter入門(Getting started with Jupyter notebooks)
Before we discuss the essentials of Jupyter notebooks, let us discuss what an integrated development environment (or IDE) is. An IDE brings together the various activities involved in programming, like including writing and editing code, debugging, and creating executables. It also includes features like autocompletion (completing what the user wants to type, thus enabling the user to focus on logic and problem-solving) and syntax highlighting (highlighting the various elements and keywords of the language). There are many IDEs for Python, apart from Jupyter, including Enthought Canopy, Spyder, PyCharm, and Rodeo. There are several reasons for Jupyter becoming a ubiquitous, de facto standard in the data science community. These include ease of use and customization, support for several programming languages, platform independence, facilitation of access to remote data, and the benefit of combining output, code, and multimedia under one roof.
在討論 Jupyter 筆記本的要點之前,讓我們先討論一下什么是集成開發(fā)環(huán)境(或 IDE)。集成開發(fā)環(huán)境匯集了編程過程中涉及的各種活動,如編寫和編輯代碼、調(diào)試和創(chuàng)建可執(zhí)行文件。它還包括自動完成(完成用戶想要輸入的內(nèi)容,從而使用戶能夠?qū)W⒂谶壿嫼徒鉀Q問題)和語法高亮(高亮顯示語言的各種元素和關(guān)鍵字)等功能。除了 Jupyter 之外,還有許多 Python 集成開發(fā)環(huán)境,包括 Enthought Canopy、Spyder、PyCharm 和 Rodeo。Jupyter 成為數(shù)據(jù)科學界無處不在的事實標準有幾個原因。這些原因包括:易于使用和定制、支持多種編程語言、平臺獨立性、便于訪問遠程數(shù)據(jù),以及將輸出、代碼和多媒體整合在一個屋檐下的好處。
JupyterLab is the IDE for Jupyter notebooks. Jupyter notebooks are web applications that run locally on a user’s machine. They can be used for loading, cleaning, analyzing, and modeling data. You can add code, equations, images, and markdown text in a Jupyter notebook. Jupyter notebooks serve the dual purpose of running your code as well as serving as a platform for presenting and sharing your work with others. Let us look at the various features of this application.
JupyterLab 是 Jupyter 筆記本的集成開發(fā)環(huán)境。Jupyter 筆記本是在用戶機器上本地運行的網(wǎng)絡(luò)應用程序。它們可用于加載、清理、分析和建模數(shù)據(jù)。您可以在 Jupyter 筆記本中添加代碼、方程式、圖像和標記文本。Jupyter 筆記本具有雙重用途,既可以運行代碼,也可以作為與他人展示和分享工作的平臺。讓我們來看看這款應用程序的各種功能。
基本用法
打開面板(Opening the dashboard)
Type “jupyter notebook” in the search bar next to the start menu. This will open the Jupyter dashboard. The dashboard can be used to create new notebooks or open an existing one.
在開始菜單旁邊的搜索欄中輸入 “jupyter notebook”。這將打開 Jupyter 面板。儀表板可用于創(chuàng)建新筆記本或打開現(xiàn)有筆記本。
創(chuàng)建新的筆記(Creating a new notebook)
Create a new Jupyter notebook by selecting New from the upper right corner of the Jupyter dashboard and then select Python 3 from the drop-down list that appears, as shown in Figure 1-3.
如圖 1-3 所示,從 Jupyter 面板右上角選擇新建,然后從出現(xiàn)的下拉列表中選擇 Python 3,創(chuàng)建一個新的 Jupyter 筆記本。
添加要執(zhí)行的代碼(Entering and executing code)
Click inside the first cell in your notebook and type a simple line of code, as shown in Figure 1-4. Execute the code by selecting Run Cells from the “Cell” menu, or use the shortcut keys Ctrl+Enter.
單擊筆記本中的第一個單元格,輸入一行簡單的代碼,如圖 1-4 所示。從 "單元格 "菜單中選擇 "運行單元格 "或使用快捷鍵 Ctrl+Enter 執(zhí)行代碼。
print("Hello World")
添加MarkDown文本(Adding markdown text or headings)
In the new cell, change the formatting by selecting Markdown as shown in Figure 1-5, or by pressing the keys Esc+M on your keyboard. You can also add a heading to your Jupyter notebook by selecting Heading from the drop-down list shown in the following or pressing the shortcut keys Esc+(1/2/3/4).
在新單元格中,選擇 Markdown(如圖 1-5 所示)或按鍵盤上的快捷鍵 Esc+M,更改格式。您還可以從下拉列表中選擇標題或按快捷鍵 Esc+(1/2/3/4),為 Jupyter 筆記本添加標題。
重命名筆記(Renaming a notebook)
Click the default name of the notebook and type a new name, as shown in Figure 1-6.
單擊筆記本的默認名稱并鍵入新名稱,如圖 1-6 所示。
You can also rename a notebook by selecting File ? Rename.
您還可以通過選擇文件 ? 重命名來重新命名筆記本。
保存筆記(Saving a notebook)
Press Ctrl+S or choose File ? Save and Checkpoint.
按 Ctrl+S 或選擇文件 ? 保存和檢查點。
下載筆記(Downloading the notebook)
You can email or share your notebook by downloading your notebook using the option File ? Download as ? notebook (.ipynb), as shown in Figure 1-7.
如圖 1-7 所示,您可以使用 "文件"?"下載為?筆記本(.ipynb)"選項下載筆記本,通過電子郵件發(fā)送或共享筆記本。
Jupyter 中的快捷方式和其他功能(Shortcuts and other features in Jupyter)
Let us look at some key features of Jupyter notebooks, including shortcuts, tab completions, and magic commands.
讓我們看看 Jupyter 筆記本的一些關(guān)鍵功能,包括快捷方式、選項卡補全和神奇命令。
Table 1-1 gives some of the familiar icons found in Jupyter notebooks, the corresponding menu functions, and the keyboard shortcuts.
表 1-1 列出了 Jupyter 筆記本中一些熟悉的圖標、相應的菜單功能和鍵盤快捷鍵。
If you are not sure about which keyboard shortcut to use, go to: Help ? Keyboard Shortcuts, as shown in Figure 1-8.
如果不確定使用哪個鍵盤快捷鍵,請轉(zhuǎn)到 幫助 ? 鍵盤快捷鍵,如圖 1-8 所示。
Commonly used keyboard shortcuts include
- Shift+Enter to run the code in the current cell and move to the next cell.
- Esc to leave a cell.
- Esc+M changes the mode for a cell to “Markdown” mode.
- Esc+Y changes the mode for a cell to “Code”
常用的快捷鍵包括
- Shift+Enter 運行當前單元格中的代碼并移動到下一單元格。
- Esc 離開單元格。
- Esc+M 可將單元格模式更改為 "Markdown "模式。
- Esc+Y 可將單元格模式更改為 "代碼 "模式。
使用tab提示(Tab Completion)
This is a feature that can be used in Jupyter notebooks to help you complete the code being written. Usage of tab completions can speed up the workflow, reduce bugs, and quickly complete function names, thus reducing typos and saving you from having to remember the names of all the modules and functions.
這是一項可用于 Jupyter 筆記本的功能,可幫助您完成正在編寫的代碼。使用制表符補全可以加快工作流程,減少錯誤,并快速補全函數(shù)名稱,從而減少錯別字,免去記住所有模塊和函數(shù)名稱的麻煩。
For example, if you want to import the Matplotlib library but don’t remember the spelling, you could type the first three letters, mat, and press Tab. You would see a dropdown list, as shown in Figure 1-9. The correct name of the library is the second name in the drop-down list.
例如,如果想導入 Matplotlib 庫,但不記得拼寫,可以鍵入前三個字母 mat,然后按 Tab 鍵。你會看到一個下拉列表,如圖 1-9 所示。該庫的正確名稱是下拉列表中的第二個名稱。
Jupyter 中使用的神奇命令(Magic commands used in Jupyter)
Magic commands are special commands that start with one or more % signs, followed by a command. The commands that start with one % symbol are applicable for a single line of code, and those beginning with two % signs are applicable for the entire cell (all lines of code within a cell).
魔法命令是一種特殊命令,以一個或多個 % 符號開頭,后面跟一個命令。以一個 % 符號開頭的命令適用于一行代碼,而以兩個 % 符號開頭的命令適用于整個單元格(單元格內(nèi)的所有代碼行)。
One commonly used magic command, shown in the following, is used to display Matplotlib graphs inside the notebook. Adding this magic command avoids the need to call the plt.show function separately for showing graphs (the Matplotlib library is discussed in detail in Chapter 7).
一個常用的神奇命令如下所示,用于在筆記本中顯示 Matplotlib 圖形。添加該神奇命令后,就無需單獨調(diào)用 plt.show 函數(shù)來顯示圖形了(第 7 章將詳細討論 Matplotlib 庫)。
%matplotlib inline
Magic commands, like timeit, can also be used to time the execution of a script, as shown in the following.
魔法命令(如 timeit)也可用于為腳本的執(zhí)行計時,如下所示。
%%timeit
for i in range(100000):
i*i
Output:
16.1 ms ± 283 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)
Now that you understand the basics of using Jupyter notebooks, let us get started with Python and understand the core aspects of this language.文章來源:http://www.zghlxwxcb.cn/news/detail-809067.html
現(xiàn)在,您已經(jīng)了解了使用 Jupyter 筆記本的基礎(chǔ)知識,讓我們從 Python 開始,了解這種語言的核心內(nèi)容。文章來源地址http://www.zghlxwxcb.cn/news/detail-809067.html
到了這里,關(guān)于《Python數(shù)據(jù)分析技術(shù)?!返?1章 02 Jupyter入門(Getting started with Jupyter notebooks)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!