<jupyter_start><jupyter_text>Python Course Homework 1: Jupyter Notebook and Python builtin type methods  Jupyter NotebookThis notebook file runs on Jupyter Notebook, once called IPython notebook.  It is an incredible tool that is geared towards an interactive data exploration workflow and increasing transparancy in how data analysis was performed.  It does this by sharing text, code, and figures all in the same place.  The format has become very popular, and notebooks be found online for all kinds of analyses!Please download and open this notebook in the jupyter notebook application on your computer to interact with it and work through the exercises!  Markdown Cells are for Text, Code Cells are for Code.Keyboard shortcuts for Jupyter Notebook can be found under the Help menu.  They will speed up your work immensely! Markdown CellsTo add text to a notebook, click a "cell" (one of the boxes you can type into) and select **Markdown** from the dropdown list on the toolbar near the top of the page.  You can tell it worked if there is no "In []" text at the left of the cell.  Then, type your sentences, format it using the Markdown text format (details can be found at https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet) and run the cell by:  - clicking the "Run cell" triangle button on the toolbar,   - clicking "Run Cell" in the Cell dropdown menu, or   - pressing Ctrl-Enter. To see how this cell was formatted, simply double-click it or select it and press the Enter key, and you will see what was typed into the cell.For great examples of how effective notebooks can be at communication: see the notebooks at the following link: http://nb.bianp.net/sort/views/  Code CellsCode cells run code!  By default, they should be running Python just fine.  To change the kernel that is running, go to the Kernel menu, highlight "Change Kernel", and select the language you want started.  You'll also see options in that menu for restarting it and running everything in the notebook.Following is an example of a code cell: Go ahead and run it!  You'll should see the output below.  Notice that the print() function wasn't necessary here--the notebook will display one line (the last line) that is called without assignment to a variable.<jupyter_code># Code cells run Python.
# This code will fetch an image from the internet, from a site that gives a random image.
# The image will be an Image object!
from IPython.display import Image
rand_pic = Image(url='http://lorempixel.com/400/200/', width=310)
rand_pic<jupyter_output><empty_output><jupyter_text>IPython MagicksIPython has a lot of extension tricks for performing useful tasks or modifying its behavior!  You can recognize them as functions prepended by a % sign.  If it is used to modify an entire cell or notebook, then two % signs will be used.  Here are a couple of my favorites: %timeitTells you how long a line took to run.  For example:<jupyter_code>%timeit sum([1,2,3,4,5])<jupyter_output>The slowest run took 10.89 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 175 ns per loop<jupyter_text>You can also use **%%timeit** to time a whole cell!  %qtconsoleLaunches an IPython console that is tied to the notebook session.  Super useful for trying things out, because all variables are shared between them!  By habit, I put this line at the top of all my analysis notebooks, making it an essential part of my workflow. %matplotlib inline %matplotlib notebookMakes all Matplotlib figures appear inside the notebook.  If the **inline** option is used, it will appear as an image below (useful, especially when also making plots in the qtconsole).  If the **notebook** option is used, a fully interactive plot will appear!<jupyter_code>%qtconsolem
whos<jupyter_output>Variable       Type     Data/Info
---------------------------------
Image          type     <class 'IPython.core.display.Image'>
abstract       str      We analyze the locomotor <...>on into \ndistinct modes.
bigfilm        str      The Lion King
film           str      the lion king
new_subject    str      ASF193
new_subjects   list     n=3
rand_pic       Image    <IPython.core.display.Image object>
strdata        str      12 40 89 100 41.2 41.2 0.45 1.1
subject        str      NOP234
subject1       str      NOP234
subject2       str      GHS673
subjects       list     n=6
title          str      Performance Comparison between Subjects {} and {}<jupyter_text>Python Built-In Type MethodsAs we discussed in the lecture, all Python objects are **Objects**, which means they have **Properties/Attributes** (data) and **Methods** associated with them.  These are accessible via the **Dot Notation**. For example, here we use a string **Class**'s *title()* method:<jupyter_code>film = 'the lion king'
bigfilm = film.title()
bigfilm<jupyter_output><empty_output><jupyter_text>It could also have been called like this:<jupyter_code>'the lion king'.title()<jupyter_output><empty_output><jupyter_text>Exercises**Directions**: Answer the following questions by adding lines of code to perform the task requested on the given data in each data cell.  In many exercises, this will only require one line of code, but some may require multiple steps.**Tip**: Learning a programming language is just as much about learning the names and locations of the functions as it is about learning how the language works.  This is akin to how learning the vocabulary of a spoken language is just as important as learning its grammar. **Tip**: All of these exercises can be completed with the methods of the built-in types: str, int, float, list, tuple, set, dict.  **Go to https://docs.python.org/3/library/stdtypes.html for the documentation!**  Exercise 2: The Variable SubjectYou want to label your figure title with the subject code, and the subject code changes depending on which subject is being shown!  How could you stick a subject's name in your title string?<jupyter_code>subject = 'NOP234'
title = "Mean Values of SUBJECT NAME's data over Time"
title.replace('SUBJECT NAME', subject)<jupyter_output><empty_output><jupyter_text>If you had formatted your title string in the following way, though, another string method would be more useful.  Which one would you use for this title:<jupyter_code>subject1 = 'NOP234'
subject2 = 'GHS673'
title = "Performance Comparison between Subjects {subj1} and {subj2}"

title.format(subj2=subject1, subj1=subject2)<jupyter_output><empty_output><jupyter_text>Exercise 3: The Wrong TypeYour colleague sent you the data you needed, but in the text of an email!  Seriously, who sends data like that, it's ridiculous!  Well, now you need to make a list of numbers out of it.  How do you do that without typing them by hand?<jupyter_code>strdata = '12 40 89 100 41.2 41.2 0.45 1.1'
strdata.split(' ')<jupyter_output><empty_output><jupyter_text>Exercise 4: One more time!You have a list of subjects, and you want to add another one!  How do you do it?<jupyter_code>subjects = ['NOP234', 'GHS673', 'JGL212']
new_subject = 'ASF193'

subjects + [new_subject]<jupyter_output><empty_output><jupyter_text>Exercise 5: Lots at once!Now, a bunch of new subjcts appeared!  How do you add them to the main list?<jupyter_code>subjects = ['NOP234', 'GHS673', 'JGL212']
new_subjects = ['ASF193', 'THW994', 'JJZ231']

subjects.extend(new_subjects)
subjects<jupyter_output><empty_output><jupyter_text>Exercise 6: Nice and NeatPlease put those subjects in alphabetical order.  It looks better that way, doesn't it?<jupyter_code>subjects = ['NOP234', 'GHS673', 'jGL212', 'ASF193', 'THW994', 'JJZ231']
subjects.sort()
subjects<jupyter_output><empty_output><jupyter_text>Exercise 7: The Bad SubjectOh, no, 'JGL202' was a terrible subject, he intentionally ruined your study.  Well, there's no way you're keeping him.  How do you remove him from the list?<jupyter_code>subjects = ['NOP234', 'GHS673', 'JGL212', 'ASF193', 'THW994', 'JJZ231']
subjects.remove('JGL212')
subjects<jupyter_output><empty_output><jupyter_text>Exercise 8: The Limited AbstractThe conference says it only takes abstracts that have a maximum word count of 100 words.  Did our abstract make the cut?  **hint: the len() function is useful here**<jupyter_code>abstract = """We analyze the locomotor behavior of the rat during exploration, 
and show that digitally collected data (time series of positions) 
provide a sufftcient basis for establishing that the rat uses several distinct modes of motion (first, second, 
third, and sometimes fourth gear). The distinction between these modes is obtained by first segmenting the 
time series into sequences of data points occurring between arrests (as ascertained within the resolution of 
the data acquisition system). The statistical distribution of the maximal amount of motion occurring within 
each of these episodes is then analyzed and shown to be multi modal. This enables us to decompose motion into 
distinct modes."""

len(set(abstract.split(' ')))<jupyter_output><empty_output><jupyter_text>Oh, wait, now that I look closer, it actually says that it's a maximum of 100 **unique** words--duplicated words don't count.  How many unique words do we have in our abstract?  (What a strange conference...)**Hint**: There is a useful class for this...  Exercise 9: The Balloon SellerA balloon seller (oops, I mean the balloon Scientist!) is giving out free balloons to all the, umm, researchers in the neighborhood.  He has three balloons, and wants to give them to Jenny, Manny, and Benny.  How can he assign them to them one at a time?<jupyter_code>balloons = ['red', 'blue', 'green']
jenny = balloons.pop()
manny = balloons.pop()
benny = balloons.pop()
jenny<jupyter_output><empty_output><jupyter_text>Of course, he could have given them all out in a single step.  How?<jupyter_code>balloons = ['red', 'blue', 'green']<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07496463932107496, 'fraction_numerical': 0.01919579713073348, 'mean_word_length': 4.637243735763098, 'pattern_counts': {'":': 0, '<': 62, '<?xml version=': 0, '>': 62, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '508061', 'license': 'BSD-2-Clause', 'n_tokens_mistral': 2915, 'n_tokens_neox': 2721, 'n_words': 1346, 'path': 'notebooks/block1/homework/Homework 1 Jupyter Notebook and Builtin Type Methods.ipynb', 'repo_name': 'janfb/msne-datascience-2018', 'size': '9898'}