Will Jupyter notebooks eventually replace scientific papers?

https://www.sohu.com/a/233205181_633698

 

[Artificial Intelligence Headline Guide] Jupyter Notebook is a web application that makes it easy to create and share literary program documents. It supports real-time code, math equations, visualization and markdown. Its uses include data cleansing and conversion, numerical simulation, statistical modeling, and machines. Study and so on. Currently, the data in Kaggle, the most popular competition in data mining, is in Jupyter format. For machine learning beginners, learning to use Jupyter Notebook is very important.

The following guide to getting started with Jupyter Notebook was written by Pranav Dar. By the time you've read this article, you'll understand why you should use it to build your machine learning projects, and why Jupyter Notebook is better than other standard tools in the field!

▌What is Jupyter Notebook?

Jupyter Notebook is an open source web application that we use to create and share code and documents.

It provides an environment in which you can record code, run code, view results, visualize data, and view output results. These features make it a handy tool for performing end-to-end data science workflows that can be used for data cleansing, statistical modeling, machine learning model creation and training, data visualization, and many other purposes.

If you're still creating a prototype project, Jupyter Notebooks is very useful because your code is written and executed separately in a separate unit. This allows users to test specific blocks of code in the project without having to run the code from the beginning of the script. Many other Integrated Development Environment (IDE) environments (like RStudio) do this in various other ways as well, but I find Jupyter's single unit structure to be the best.

As you will see in this article, these notebooks are very flexible, interactive, and powerful tools in the hands of the data scientist. You can even run languages ​​other than Python, such as: B. R, SQL, etc. Because they are more interactive than IDE platforms, they are often used in educational scenarios.

▌How do I install Jupyter Notebook?

First, you need to install Python on your computer, either Python 2.7 or Python 3.3 (or higher).

Anaconda

New users can use the Anaconda distribution to install Python and Jupyter notebooks at the same time.

Anaconda installs these two tools and includes many software packages commonly used in data science and machine learning. You can download the latest version of Anaconda here.

Download link: https://jupyter.readthedocs.io/en/latest/install.html

Pip installation method

If you decide against Anaconda for any reason, you need to make sure that your computer is running the latest version of pip. How it goes? If you've already installed Python, pip is already installed. For information on upgrading to the latest version, see the following code:

#Linux and OSX

pip install-U pip setuptools

#Windows

python -m pip install-U pip setuptools

Once pip is installed, you can continue to install Jupyter:

#For Python2

pip installjupyter

#For Python3

pip3 installjupyter

You can go here (address: https://jupyter.readthedocs.io/en/latest/install.html)

) See the official Jupyter installation documentation.

"Start!

Now we understand what notebooks are and how notebooks are installed and set up on our own computer. It's time to start!

To run Jupyter Notebooks just enter and use the following command!

jupyternotebook

After executing the above command, Jupyter Notebook will open in your default browser at http: // localhost: 8888 / tree

In some cases, it may not open automatically. In this case, a URL with a token key (token key) is generated in your terminal or at your command prompt. To open Notebook you need to copy and paste the entire URL (including the token key) into your browser.

After opening Notebook, you will see three tabs at the top: Files, Run, and Clusters. Files basically lists all files, Running shows the terminals and notebooks you currently have open, and clusters are provided by the IPython Parallel Package for parallel computing.

To open a new Jupyter notebook, click the New tab on the right side of the page. Here you have four options to choose from:

⦁ Python 3

⦁ text file (text file)

⦁ folder

⦁ Terminal (Terminal)

The "Text file" option gives you an empty document. Enter any letters, words, and numbers. It's basically a text editor (similar to an application on Ubuntu). You can also choose a language (support many languages) and then write a script in that language. You can also find and replace words in the file.

With the Folder option, the function, as the name suggests, is to create a folder. You can create a new folder to store, rename, or delete files.

"Terminal" works just like the terminal on your Mac or Linux computer (or cmd on Windows). It supports terminal sessions in your web browser. Type python into that terminal and voila! You are now ready to start writing Python scripts.

However, in this article we will focus on notebooks so we will select the Python 3 option from the New option. You can see the screenshot below:

You can then import the most popular Python libraries (Pandas and Numpy) to start your project. The menu bar above the code offers various options for operating cells: insert (add), edit (edit), cut (cut), move the cell up / down (move the cell up and down), execute cells (in the Cell) execute code), interrupt (stop code), save (save work) and restart (restart kernel).



In the drop-down menu (as shown above) you have four options:

  • Code - Enter the code here
  • Markdown-This is where you enter text. You can add conclusions, add comments, etc. after running the code.
  • Raw NBConvert- This is a command line tool that you can use to convert your notebook to another format (such as HTML).
  • Heading - You can add headings to individual sections and keep your notebook looking neat and tidy. This option is now built into the Markdown Options. Add a "##" to make sure what you enter after it is treated as a title

▌Use magical functions

Developers have included predefined magical functions to make your work more convenient and interactive. You can run the following command to see the list of these features (note: you normally don't need to type "%" as Automagic is usually enabled by default):

% lsmagic

You will see many options and you may even see features like% clear,% autosave,% debug and% mkdir. There are two ways to perform a magical command:

  • Line-by-line operation
  • Execute block by block (cell by cell)

As the name suggests, line by line is used when you want to run a single line command, and cell by line is used when the command to be executed is not just one line but the entire block of code in the entire cell.

In the line-by-line operating mode, all specified commands must begin with the% character, and in the block-by-line operating mode, all commands must begin with %%. Let's look at the following example to better understand:

Progressive mode:

% time a = range (10)

Block by block mode:

%% timeit a = range (10)

min (a)

I suggest you run these commands to see the difference!

▌Not limited to Python-R, Julia and Java in notebooks

The magic of notebooks doesn't stop there. You can even use other languages ​​in notebooks, such as: B. R, Julia, JavaScript etc. I personally like the ggplot2 package in R, so it is very convenient to use notebooks for exploratory data analysis.

To enable R in Jupyter, you'll need IRKernel (the dedicated kernel for R), which is available on GitHub. Here is a detailed guide (address: https://discuss.analyticsvidhya.com/t/how-to-run-r-on-jupyter-ipython-notebooks/5512) that requires a total of eight steps and screenshots guide you step by Step.

If you are a Julia user, you can use it in Jupyter notebooks too! Read this in-depth article (address: address: https://discuss.analyticsvidhya.com/t/how-to-run-r-on-jupyter-ipython-notebooks/5512) which is about how Julia- Users are learning data science and includes a chapter on using Julia in the Jupyter environment.

If you prefer Java, I recommend using the IJava kernel. Take a look at this GitHub repository (address: https://github.com/n-riesco/ijava). It will help you install this kernel on different operating systems. Please note that you need to install Node.js and npm before you can use it.

▌Interactive command card in Jupyter notebooks

Before adding widgets, you need to import the widgets package:

fromipywidgets importwidgets

The basic types of widgets are typical text entry boxes and buttons. For more information on how to use the interactive widget, see the following example from Dominodatalab:

You can find the detailed development guide here (address: https://blog.dominodatalab.com/interactive-dashboards-in-jupyter/).

▌Key combinations save time and improve efficiency!

Keyboard shortcuts are one of the most useful functions of Jupyter Notebooks. When you want to run a block of code, all you have to do is press Ctrl + Enter. Jupyter Notebooks offers many keyboard shortcuts, which saves us a lot of time.

Below are some of the keyboard shortcuts we selected. You use these keyboard shortcuts a lot when using notebooks. I strongly recommend that you read these links one by one as you read this content.

Jupyter Notebook offers two different keyboard input modes - command and edit. The command mode binds the keyboard to notebook commands and is represented by a gray cell border with a blue left border. In edit mode, you can enter text (or code) in the active cell. This is indicated by a green cell border.

Use Esc and Enter to switch between command and edit mode. Try it now!

Once you switch to Command mode (i.e. there is no active cell), here are some keyboard shortcuts that you can try:

  • The A key inserts a new cell above the selected cell, and the B key inserts a cell below the selected cell
  • To delete a cell, press the D key twice.
  • Press the Z key to undo a deleted cell
  • The Y key turns the currently selected cell into a code cell
  • Hold Shift + Shift up or down to select multiple cells. In multiple selection mode, press Shift + M to merge the selected cells
  • The "Find and Replace" menu is called up with the F key

When you are in edit mode (in command mode, press enter to enter edit mode) the following keyboard shortcuts are very useful:

  • Ctrl + Home Go to the beginning of the cell
  • Ctrl + S saves your work
  • As mentioned above, Ctrl + Enter will run your entire cell
  • Alt + Enter not only executes your cell, it also adds a new cell
  • Ctrl + Shift + F Open the Command Palette

To see the full list of keyboard shortcuts, press H in Command mode or go to Keyboard Shortcuts on the Help menu bar. Please check this content regularly as new keyboard shortcuts are added frequently.

▌Useful Jupyter notebook extension

Extensions can effectively increase the productivity of your Jupyter notebooks. One of the best tools for installing and using extensions is Nbextensions. Installation takes two simple steps (there are other methods, but I found this to be the most convenient):

Step 1: use pip to install it:

pipinstall jupyter_contrib_nbextensions

Step 2: Install the associated Java and CSS files:

jupyter contrib nbextension install - user

After that, a Nbextensions tab will appear at the top of the Jupyter Notebook homepage. There are a lot of great extensions that you can use.

To activate an extension, simply click on it to activate it. I mentioned 4 extensions below that I think are the most useful:

  • Beautifying code: Formatting and beautifying the content of blocks of code.

  • Print View: This extension adds a toolbar button that can be used to bring up the current notebook's jupyter nbconvert and can display the converted file in a new browser tab.
  • Scratchpad: This extension adds a handy cell that allows you to run your code without changing your notebook. This is a very handy extension, especially if you want to test your code but don't want to do it in your active notebook.
  • Table of Contents (2): This extension can collect all the titles in your notebook and display them in a floating window.

These are just a few of the extensions you can use. I strongly recommend checking out and testing the entire list of extensions.

▌Save and share your notebook

This is one of the most important and impressive features in Jupyter Notebook. If I need to write a blog post but my code and comments are in a Jupyter file, I need to convert it to another format first. Note that these notebooks are in JSON format, which isn't very helpful when sharing. I can't post different cells or blocks of code in emails or blogs, can I?

When you go to the Files menu, you will see the Download As option:

You can save your notebook as one of 7 options. Most commonly, the IPynb file is used so that other users can copy the code to their own computer or save it as an HTML file, which opens a webpage (if you want to save the image embedded in the notebook it will use sent)

You can also use the nbconvert option to manually convert your notebook to various formats such as HTML or PDF.

You can also use jupyterhub, with which you can host notebooks on the server and share them with multiple users. Many top research projects use this function for collaboration.

▌JupyterLab

JupyterLab was introduced in February of this year and enables a more flexible and efficient way of handling projects, but uses the same components as Jupyter Notebooks. The JupyterLab environment is exactly the same as the Jupyter notebooks, but the users have a more efficient experience.

In JupyterLab you only need one window to arrange the workspace, the terminal, the text file and the output of the notebook! All you have to do is drag and drop the cell to the desired position. You can also edit popular file formats like Markdown, CSV and JSON via real-time preview to see the changes in the actual file in real time.

If you want to try it on your computer, you can view the installation instructions here (address: http://jupyterlab.readthedocs.io/en/stable/getting_started/installation.html). The developer's goal is that JupyterLab can eventually replace Jupyter notebooks. However, this will take some time.

EstBest exercise

While it is fun to work alone, most of the time you will be working in a team. In this case, it is very important to follow guidelines and best practices to make sure that your code and Jupyter notebooks are properly tagged to match your teammates. I have listed some best practice guidelines below.You need to follow these guidelines when working on Jupyter notebooks:

  • One of the most important things for any programmer: always make sure to add appropriate comments to the code!
  • Make sure you have the necessary documentation for the code.
  • Consider a naming scheme and stick to it throughout your code to ensure consistency. This will make your code easier for others to read.
  • Whichever libraries you need, import them at the top of notebooks (and add a note next to them to explain the purpose of the import).
  • Make sure there is enough space between lines in your code and don't put loops and functions on the same line.
  • Sometimes you will find that your files get very heavy. Check to see if there is a way to hide code that you don't think is important for future reference. Notebooks look neat and clean, and pleasing to the eye is also very important.
  • Matplotlib can display your notebook nice and tidy, see how to use it! Address: http://nbviewer.jupyter.org/github/jrjohansson/scientific-python-lectures/blob/master/Lecture-4-Matplotlib.ipynb

Another tip! When you want to create a presentation, PowerPoint and Google Slides are the first tools that come to mind. In fact, your Jupyter notebooks can create slideshows too! Do you remember when I said it was super flexible? I'm not exaggerating at all.

To convert your notebooks to slides, go to View -> Cell Toolbar and click Slideshow. Look! Now each block of code will display a Slide Type drop-down option on the right. It offers the following 5 options:



Try each option to better understand them. This changes the way you present your code!

▌ Conclusion

Please note that not all functions of Jupyter Notebooks are fully listed in this article. There are other functions waiting to be explored by you. As with many other things, the key is practice.

Finally, share a GitHub repository (address: https://github.com/jupyter/jupyter/wiki/A-gallery-of-interesting-Jupyter-Notebooks) with lots of useful content.