Python Data Science Handbook Notes

Ace your homework & exams now with Quizwiz!

What do the %history and %rerun commands do?

The %history command returns a batch of the previous inputs. You can select the length of the list by using -n start - end base on the input numbers. %rerun returns a short portion of the command history.

What are the different ways to get help within the ipython shell?

To get help within the ipython shell you can use the help() function, call the docstring of the calling object or function. The single question mark prints out the docstring, file, type, and signature. The double question mark prints the source code, file, type, and signature of the object or function. Note: It's important to use docstrings to record any notes for future use when calling help on function with the notes.

What is wildcard matching?

Wildcard matching is using the * before a word and finish with a ? to find all the matching methods with said word. This is very useful when using new packages or getting reacquainted with old ones.

What do Python lists and numpy arrays differ from one another?

At the implementation level, the array essentially contains a single pointer to one con‐ tiguous block of data. The Python list, on the other hand, contains a pointer to a block of pointers, each of which in turn points to a full Python object like the Python integer we saw earlier. Again, the advantage of the list is flexibility: because each list element is a full structure containing both data and type information, the list can be filled with data of any desired type. Fixed-type NumPy-style arrays lack this flexibil‐ ity, but are much more efficient for storing and manipulating data.

What's the conventional way that most people import NumPy?

By convention, you'll find that most people in the SciPy/PyData world will import NumPy using np as an alias.

What does the %prun magic command do?

Calls the pyhon built-in prifiler on function or line of code.

How do you pass an Ipython variable to the shell?

Communication in the other direction—passing Python variables into the shell—is possible through the {varname} syntax

What does the %time do and when is it useful?

Sometimes repeating an operation, like in the case of %%timeit with fast operations, is not the best option. This is specially true when sorting presorted lists since they naturally faster to sort. The %time magic command gives a more accurate result but takes longer to execute. This command is recommended with fast operations like presorted lists. Using the %%time command also allows timing of multiline scripts just like %%timeit.

What does the %debug magic command do?

The %debug magic command calls the IPDB(Interactive Python Debugging Prompt) on the last encountered exception. The IPDB prompt lets you explore the current state of the stack, explore the available variables, and even run Python commands! The up and down keywords allow you to navigate through the function while quit gets you out of IPDB mode. To run the IPDB automatically when when an exception is raised use %pdb.

What does the %lprun magic command do?

The %lprun magic command prints a line by line profile report any function. **The information at the top gives us the key to reading the results: the time is reported in microseconds and we can see where the program is spending the most time. At this point, we may be able to use this information to modify aspects of the script and make it perform better for our desired use case.**

what does the %timeit command in ipython do?

The %timeit command will automatically determine the execution time of the single-line Python statement that follows it. For multiline statements, adding a second % sign will turn this into a cell magic that can handle multiple lines of input. Note that because this operation is so fast, %timeit automatically does a large number of repetitions. For slower commands, %timeit will automatically adjust and perform fewer repetitions

What are the does the %xmode magic function do and what arguments does it take?

The %xmode magic function (short for exception mode) takes a single argument, the mode, and there are three possibilities: Plain, Context, and Verbose. Plain is more compact and gives less information. Coxtext gives a little more information The Verbose mode adds some extra information, including the arguments to any functions that are called

What some important properties of the In and Out ipython variables?

The In variable returns a list while the Out variable returns a dictionary. You can choose which In/Out to return by calling it with the command history number associated with it. You can even even make the two variables interact with each other and do operations on them.

What does the automagic function in ipython do?

The automagic function allows for shell commands to be naturally used in the ipython shell to avoid switching bettween the two.

What are magic commands and how do you call them?

The magic function system provides a series of functions which allow you to control the behavior of IPython itself, plus a lot of system-type features. There are two kinds of magics, line-oriented which are prefixed with the % character and cell-oriented which are prefixed with a double %%. For a quick and simple list of all available magic functions, type this use %lsmagic

What two useful magic functions does the memory profiler extension contain and what do they do?

The memory profiler extension contains two useful magic functions: the %memit magic (which offers a memory-measuring equivalent of %timeit) and the %mprun function (which offers a memory-measuring equivalent of %lprun).

What are the underscore shortcuts in python and ipython? What's the shortcut for Out[x]?

The standard Python shell contains just one simple shortcut for accessing previous output; the variable _ (i.e., a single underscore) is kept updated with the previous out‐ put but IPython takes this a bit further—you can use a double underscore to access the second-to-last output, and a triple underscore to access the third-to-last output. The shortcut for Out[x] which becomes more convinient thant the underscores is _x.

what is the tab used for in the common line?

The tab can be used to get a list of the methods associated to an object and or just to auto complete the method. You must type a period after the object followed by the tab key to initiate this functionality. if type some of the method it will weed out methods that are similar in spelling. Note: that you must type the underscore __ after period for private methods "dunder methods".

What are the In and Out prompts in ipython and why are they important?

These are variables that help the user know where he is but most importantly these are fixed variables that contain the users history commands. The In variable keeps are record of user input while the Out variable keeps a record of all the users returned output during active session. Typing In on the command returns an input history while typing Out returns and output history.

How to initiate and exit Jupyter notebook?

To initiate Jupyter notebook type "jupyter notebook" on the command line. This will open jupyter notebook on your default browser. You can quit Jupyter notebook from the browser by clicking the "quit" butto next to "logout" or by pressing Ctrl+C on the command line and choosing y when the y/n option is given.

How do you suppress and output in ipython and why would you need to use this feature?

To suppress the output of a statement you use a semicolon ; at the end of the statement. This is commonly use with plotting commands or the command produces a result that you'd prefer not to store in your output history.

How do you use shell commands in ipython?

You can use shell commands in ipython by using the exclamation mark ! before the shell command. These results can even be assigned to a variable and returned as what looks and acts a lot like a Python list, but has additional functionality, such as the grep and fields methods and the s, n, and p properties that allow you to search, filter, and display the results in convenient ways.

How do you initiate and exit ipython in the terminal? or how do you exit documentation?

You initiate the ipython shell by typing "ipython" in the terminal. You know you are in the shell when you see the green in[line-number]. To exit the shell use the exit() command. You know you are out of the shell when the green in disappears and you are back into your base terminal. You exit documentation by pressing the q key.

How do you run a program in interactive(IPDB) mode from the beginning?

if you have a script that you'd like to run from the beginning in interactive mode, you can run it with the command %run -d, and use the next command to step through the lines of code interactively.


Related study sets

Foundations of Mental Health/Psychiatric Nursing

View Set

3) ТП З педагогічної етики

View Set

Chapter 21: PrepU - Nursing Assessment: Digestive, Gastrointestinal, and Metabolic Function

View Set