https://ift.tt/3CvryU1 A Beginner’s Guide to Switching from Notebooks to Python Files Photo by Chris Ried on Unsplash When I started ...
A Beginner’s Guide to Switching from Notebooks to Python Files
When I started my first Machine Learning job, all of my previous coding had taken place in Jupyter Notebooks. However, the company I started to work for didn’t use them, so I felt a little bit scared not knowing how to code without them and instead having to use Python files. Maybe you are finding yourself in a similar position as I was in back then, starting to learn how to use files and scrips but not knowing how to start. Alas, here’s my tutorial on how to code when all you know are Jupyter Notebooks.
Jupyter Notebook is a handy little thing: you can write code, merge cells, divide cells, run individual cells — no matter where they are in the notebook — and see the output directly below the executed cell. Nevertheless, like everything in life, notebooks also come with their problems: they can be messy, the ability to execute cells in a non-linear order makes reproducibility tricky, and it’s hard to put code in notebooks into production. Python files do not come with these problems and once you have learned to work with them, they actually make coding a lot easier overall.
So what even is a Python file? It’s a collection of Python functions and statements that are either directly executed or imported somewhere else. This collection of code can be something as simple as a text file with a .py suffix, opened with a text editor. However, it’s more common (and practical) to work with files using an IDE such as PyCharm or Visual Code Studio. (To get comfortable with the basics of PyCharm, I recommend this short tutorial here. It shows you how to start a project, create files, and run those files. For a short introduction to Visual Code Studio, click here.)
Developing Code
At the beginning of my first job, executing scripts in PyCharm wasn’t even that big of a mystery to me. Rather, I was unsure how to develop code, using not a notebook but a simple script.
Coding is an iterative process. You write a few lines of code, see if it works, write more code, and so on. In Jupyter Notebooks, this process is extremely easy, since it lets you execute every individual line to see if it works. When working with a script, things are a little bit different, since you can’t really execute only one line. You more or less have to run your whole script from top to bottom.
When switching from Jupyter Notebooks to Python scripts, what worked pretty well for me was the following approach: I wrote a snippet of code (one to three lines or so), and then ran the script to see if it works. Print statements throughout the code are usually a great way to check if your code is really doing what you want it to do. Like so:
In this example, I am writing a function that pads an image with the value zero, so that the image gets the desired shape. When I run the code and and no errors are thrown, I continue to write some more lines and check again if my code runs without any errors:
If it does and the function I wanted to write is finished, I remove the print statements and the line that calls the function — and voilà, it is done:
Combining Notebooks and Scripts
To be honest, I still use Jupyter Notebooks regularly — they are just too handy for having my visualizations and data analysis all in one place. And I often use them to develop code before transferring the code to a Python file. As I have mentioned above, notebooks are great for running individual lines of code to see if they work, which is incredibly helpful, especially when you are not yet an experienced programmer.
So what I often do is to develop code in a notebook — line by line, with print statements to see if the code is doing what I want it to do — until I have a block of code that I can turn into a function. And then I put this function into a Python file. If I want, I can import it from there into my notebook again to use it there.
I hope you feel a little bit less clueless by now and ready to start experimenting with Python files on your own. Obviously, I did not cover every piece and bit that is relevant to work with Python files. So if you’re wondering how to run Python files not from inside an IDE but from the command line, I can recommend this article here. If you want to learn more about importing module, you can read about it here. Have fun!
How to code when all you know are Jupyter Notebooks was originally published in Towards Data Science on Medium, where people are continuing the conversation by highlighting and responding to this story.
from Towards Data Science - Medium https://ift.tt/3cmsEqF
via RiYo Analytics
ليست هناك تعليقات