{"id":4298,"date":"2024-04-30T07:34:23","date_gmt":"2024-04-30T07:34:23","guid":{"rendered":"https:\/\/favtutor.com\/articles\/?p=4298"},"modified":"2024-04-30T07:34:25","modified_gmt":"2024-04-30T07:34:25","slug":"jupyter-notebooks-tips-data-science","status":"publish","type":"post","link":"https:\/\/favtutor.com\/articles\/jupyter-notebooks-tips-data-science\/","title":{"rendered":"10 Jupyter Notebook Tips To Make You a Data Science Wizard"},"content":{"rendered":"\n<p>Python is used by Data Scientists and Machine Learning Engineers a lot. It is highly optimal for training and building datasets on various day-to-day ML algorithms, both supervised and unsupervised. However, which platform do most ML enthusiasts prefer for working with Python? The answer is Jupyter Notebook. <\/p>\n\n\n\n<p>Jupyter Notebook is the perfect place to run your Python project codes and get your well-running outputs. It is one of the <a href=\"https:\/\/survey.stackoverflow.co\/2023\/\" target=\"_blank\" rel=\"noopener\">10 most used IDEs<\/a>, beating Sublime Text and Eclipse. Its cell-by-cell structure makes it easy to comprehend and divide different portions of the code while running it as a whole.<\/p>\n\n\n\n<p>In this article, we will provide you with 10 wonderful tips &amp; tricks for Jupyter Notebook which will improve your navigation experience and also improve the quality of your project codes. So, let\u2019s get right into it!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>10 Jupyter Notebook Tips and Tricks for Data Science:<\/strong><\/h2>\n\n\n\n<p>Here are 10 Jupyter Notebook Tips and Tricks for Data Science. The various chosen methods revolve mostly around built-in commands, functions, kernels and much more. Let&#8217;s explore them one by one!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1) Running Shell Commands<\/strong><\/h3>\n\n\n\n<p>As demonstrated here, you can use ! to execute Shell and Bash commands inside the Jupyter Notebook cell. It gives you additional capability to use tools and commands based on Linux or Unix.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>!git push origin<\/code><\/pre>\n\n\n\n<p>Installing Python libraries while on the go is the most common usage for this command. You can install several Python packages like Pandas, Numpy, and Keras.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>!pip install Keras<\/code><\/pre>\n\n\n\n<p>Another way to install Python libraries is by using the magic command &#8216;%&#8217;.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>%pip install Pandas<\/code><\/pre>\n\n\n\n<p>This tip will be highly beneficial for you to install and import several useful Python packages as you keep working on your Machine Learning and Data Science projects. Imagine importing tons of functionalities in one go and improving your Jupyter Notebook experience.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2) Plotting in Notebooks<\/strong><\/h3>\n\n\n\n<p>If you are working on a Data Science or Machine Learning project, a specific portion of your code will be reserved for plotting visualizations such as graphs and multivariate statistics. Plotting can be done in your Jupyter Notebook in a variety of ways.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Plot.ly, a once-paid service that is now open-sourced, can produce some attractive plots.<\/li>\n\n\n\n<li>Seaborn activates Matplotlib, the de-facto standard, which is built on top of Matplotlib and facilitates the creation of more visually appealing plots. Without changing any code, you can make your matplotlib graphs &#8220;prettier&#8221; only by importing Seaborn.<\/li>\n\n\n\n<li>For matplotlib code, &#8216;mpld3&#8217; offers an alternate renderer (using d3). It\u2019s very efficient but lacks some details.<\/li>\n\n\n\n<li>For Python, <strong>&#8216;Altair&#8217;<\/strong> is a relatively new declarative visualization package. It is simple to use and produces beautiful plots, although it lacks Matplotlib&#8217;s level of customization capability.<\/li>\n<\/ul>\n\n\n\n<p>By utilizing these tips you will be able to plot several visualizations in your Python codes such as comparison graphs, data summaries, accuracy statistics, and much more.<\/p>\n\n\n\n<p>Here are some <a href=\"https:\/\/favtutor.com\/articles\/best-python-libraries\/\">amazing Python libraries<\/a> that you must know about to master it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3) Multimedia Outputs<\/strong><\/h3>\n\n\n\n<p>Images, Videos, and Audio may all be viewed without the need to install extra Python packages.<\/p>\n\n\n\n<p>All you need to do to access the Image, Video, and Audio features is import IPython.display. When working with unstructured information and machine learning applications, it is very helpful.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"730\" height=\"228\" src=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2024\/04\/audio_notebook.png\" alt=\"audio_notebook\" class=\"wp-image-4300\"\/><\/figure>\n<\/div>\n\n\n<p>So go ahead and enjoy the fully functional multimodal capabilities offered by Jupyter Notebook. You will take your Data Science projects to another level with this functionality.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4) Keyboard Shortcuts<\/strong><\/h2>\n\n\n\n<p>Any Notebook user will tell you that using keyboard shortcuts can save you a tonne of time. Keyboard shortcuts in Jupyter can be accessed through the <em>Help > Keyboard Shortcuts<\/em> menu at the top of the screen or by hitting H to enter command mode (more on that later). Every time you upgrade Jupyter, it&#8217;s important to check this because new shortcuts are always being introduced.<\/p>\n\n\n\n<p>A VSCode-like command palette is the most convenient and widely used method for quickly accessing commands while on the run. The command palette can be accessed by pressing <em>Ctrl + Shift + P<\/em>. You can use it to search and run commands or to browse through all of the commands and find the one you want to run.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"1913\" height=\"855\" src=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2024\/04\/Screenshot-601.png\" alt=\"Keyboard Shortcuts in Jupyter Notebook\" class=\"wp-image-4301\" srcset=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2024\/04\/Screenshot-601.png 1913w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2024\/04\/Screenshot-601-768x343.png 768w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2024\/04\/Screenshot-601-1536x687.png 1536w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2024\/04\/Screenshot-601-750x335.png 750w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2024\/04\/Screenshot-601-1140x510.png 1140w\" sizes=\"(max-width: 1913px) 100vw, 1913px\" \/><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\"><strong>5) Adding Additional Kernels to Jupyter Notebook<\/strong><\/h3>\n\n\n\n<p>While the Python kernel is well-known, you can run code in any language by installing different kernels.<\/p>\n\n\n\n<p>For instance, you must install Almond and Scala libs inside the Scala environment to use the Scala programming language in the Jupyter Notebook.\u00a0<\/p>\n\n\n\n<p>Download almond and scala libs<\/p>\n\n\n\n<p>(coursier is a scala tool used to install almond)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ curl -Lo coursier https:\/\/git.io\/coursier-cli &amp;&amp; chmod +x coursier<\/code><\/pre>\n\n\n\n<p>(replace scala and almond versions if you need to)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ .\/coursier bootstrap \\\n&nbsp;&nbsp;&nbsp; -r jitpack \\\n&nbsp;&nbsp;&nbsp; -i user -I user:sh.almond:scala-kernel-api_2.12.11:0.10.0 \\\n&nbsp;&nbsp;&nbsp; sh.almond:scala-kernel_2.12.11:0.10.0 \\\n&nbsp;&nbsp;&nbsp; -o almond<\/code><\/pre>\n\n\n\n<p>Install Scala kernel in Jupyter<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ .\/almond --install --id scala_2_12_11&nbsp; --display-name \"Scala 2.12.11\"<\/code><\/pre>\n\n\n\n<p>Open Jupyter notebook and select Scala kernel<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ jupyter notebook<\/code><\/pre>\n\n\n\n<p><strong>Some widely used available Kernels in Jupyter Notebook are:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>global-tf-python-3&nbsp;&nbsp;&nbsp; \/home\/felipe\/.local\/share\/jupyter\/kernels\/global-tf-python-3<\/li>\n\n\n\n<li>local_venv2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;     \/home\/felipe\/.local\/share\/jupyter\/kernels\/local_venv2<\/li>\n\n\n\n<li>python2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;       \/home\/felipe\/.local\/share\/jupyter\/kernels\/python2<\/li>\n\n\n\n<li>python36&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      \/home\/felipe\/.local\/share\/jupyter\/kernels\/python36<\/li>\n\n\n\n<li>scala&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;          \/home\/felipe\/.local\/share\/jupyter\/kernels\/scala<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6) IPython Magic Commands<\/strong><\/h3>\n\n\n\n<p>The Jupyter Notebook contains all of the IPython Magic commands. You can execute the code with additional capabilities thanks to these commands.<\/p>\n\n\n\n<p>A single line of code can have additional functionality added to it with the help of line magic commands, which start with &#8216;%&#8217; and end with the line command. Run &#8220;%lsmagic&#8221; to view all accessible magic commands; this command will display all available magic commands.<\/p>\n\n\n\n<p>Here are the available magic commands in Jupyter Notebook:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>%alias, %alias_magic, %autoawait, %autocall, %automagic, %autosave, %bookmark, %cat, %cd, %clear, %colors, %conda, %config, %connect_info, %cp, %debug, %dhist, %dirs, %doctest_mode, %ed, %edit, %env, %gui, %hist, %history, %killbgscripts, %ldir, %less, %lf, %lk, %ll, %load, %load_ext, %loadpy, %logoff, %logon, %logstart, %logstate, %logstop, %ls, %lsmagic, %lx, %macro, %magic, %man, %matplotlib, %mkdir, %more, %mv, %notebook, %page, %pastebin, %pdb, %pdef, %pdoc, %pfile, %pinfo, %pinfo2, %pip, %popd, %pprint, %precision, %prun, %psearch, %psource, %pushd, %pwd, %pycat, %pylab, %qtconsole, %quickref, %recall, %rehashx, %reload_ext, %rep, %rerun, %reset, %reset_selective, %rm, %rmdir, %run, %save, %sc, %set_env, %store, %sx, %system, %tb, %time, %timeit, %unalias, %unload_ext, %who, %who_ls, %whos, %xdel, %xmode<\/code><\/pre>\n\n\n\n<p>Let\u2019s show a demonstration of the %time magic command which shows the running time of a specific code snippet in a cell.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">def fibonacci(n):\n\n\u00a0\u00a0\u00a0\u00a0 if n == 0 or n == 1: return n\n\n\u00a0\u00a0\u00a0\u00a0 return fibonacci(n-1)+fibonacci(n-2)\n\n%time fibonacci(10)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CPU times: total: user 20 ns, sys:4 ns, total: 24 ns\nWall time: 26.2 ns\n55<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7) Simple Connections to the Documentation<\/strong><\/h3>\n\n\n\n<p>You may easily access the online documentation for popular libraries such as NumPy, Pandas, SciPy, and Matplotlib by clicking on the Help menu. Remember that by preceding a variable, library, or method with <strong>\u2018?\u2019<\/strong> for a fast syntactic reference, you can utilize the Docstring.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>?str.replace()<\/code><\/pre>\n\n\n\n<p><strong>Docstring:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>S.replace(old, new&#91;, count]) -&gt; str\n\nReturn a copy of S with all occurrences of substring\n\nold replaced by new.&nbsp; If the optional argument count is\n\ngiven, only the first count occurrences are replaced.\n\nType:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; method_descriptor<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>8) Exporting Notebooks<\/strong><\/h3>\n\n\n\n<p>Sharing notebooks not only helps you collaborate with others on your project but also increase the efficiency of your work. Your notebooks can be shared both locally and also via the cloud. It&#8217;s crucial to share the reports or code source along with the outcomes, and there are several ways to do this:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the File &gt; Download as &gt; HTML option to convert Notebooks into HTML files.<\/li>\n\n\n\n<li>Use File &gt; Download as &gt; PDF to save notebooks as PDFs.<\/li>\n\n\n\n<li>Notebooks can be downloaded as Markdown or saved as a Markdown file.<\/li>\n\n\n\n<li>Build blogs with Pelican.<\/li>\n\n\n\n<li>Share the.ipynb file with others by uploading it to Google Colab.<\/li>\n\n\n\n<li>Use GitHub Gits to distribute the Notebook file to the general audience.<\/li>\n\n\n\n<li>Use nbviewer to render the notebook after hosting your file on an external server or the cloud.<\/li>\n<\/ul>\n\n\n\n<p>These techniques will not only help you to share your source code but also make it available on a variety of platforms where several others can view and work on it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>9) Using LaTeX for Formulas<\/strong><\/h3>\n\n\n\n<p>You must include statistical or mathematical equations when developing the data analytics report, and Jupyter Notebook allows you to render complex equations using Latex formulas.<\/p>\n\n\n\n<p>To enclose your Latex formula, simply create a Markdown cell and use the dollar $ symbol, as demonstrated below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$P(A \\mid B) = \\frac{P(B \\mid A)P(A)}{P(B)}$<\/code><\/pre>\n\n\n\n<p>This is what happens to the equation as a result of the latex function:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img decoding=\"async\" width=\"154\" height=\"31\" src=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2024\/04\/render.png\" alt=\"Latex Formula in Jupyter Notebook\" class=\"wp-image-4303\" style=\"width:1170px;height:auto\"\/><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\"><strong>10) Running Code from different Kernels<\/strong><\/h3>\n\n\n\n<p>You can merge code from different kernels into a single notebook if you&#8217;d like. Using Magic instructions like these, you can use Python Jupyter Notebook to execute code from various kernels:<\/p>\n\n\n\n<p>%%html<\/p>\n\n\n\n<p>%%javascript<\/p>\n\n\n\n<p>%%python3<\/p>\n\n\n\n<p>%%ruby<\/p>\n\n\n\n<p>%%perl<\/p>\n\n\n\n<p>%%bash<\/p>\n\n\n\n<p>Let\u2019s try running a Javascript snippet in Jupyter Notebook using the %%javascript magic command:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">%%javascript\nalert(&quot;This is a JavaScript alert!&quot;)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>This is a JavaScript alert!<\/code><\/pre>\n\n\n\n<p>Simply enter the name of your kernel into IPython Magics at the beginning of each column that you wish to use that kernel for. You will get the benefits of running the same code under different kernels in one go.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>These  Jupyter Notebook tips and tricks will help you become more efficient and optimal with your Data Science and Machine Learning projects. It&#8217;s time to brush up your skills again and be the best in your career!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Check out these wonderful tips and tricks when using Jupyter Notebook to brush up your Data Science knowledge and maximize productivity.<\/p>\n","protected":false},"author":15,"featured_media":4315,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jnews-multi-image_gallery":[],"jnews_single_post":null,"jnews_primary_category":{"id":"","hide":""},"footnotes":""},"categories":[42],"tags":[217,218,219,32],"class_list":["post-4298","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-trending","tag-jupyter","tag-notebook","tag-programming-coding","tag-python"],"_links":{"self":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/4298","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/comments?post=4298"}],"version-history":[{"count":10,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/4298\/revisions"}],"predecessor-version":[{"id":4316,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/4298\/revisions\/4316"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media\/4315"}],"wp:attachment":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media?parent=4298"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/categories?post=4298"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/tags?post=4298"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}