Skip to content
Snippets Groups Projects
Commit 26fb760d authored by bys24's avatar bys24
Browse files

Upload New File

parent c6ea1a84
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
# INFO 103: Introduction to data science <br> Demo \#1: Introduction to Jupyter <br> Author: JRW
#### Download Jupyter notebooks!
The first thing you will have to do is download Jupyter, which will get you Python too (if you don't already have it). The easiest way to do this (regardless of operating system) is through the Anaconda distribution, which is a Python version specially geared for data science! Go and download a version if you haven't already!
* https://www.anaconda.com/products/individual
%% Cell type:markdown id: tags:
## About Project Jupyter
Project Jupyter is an open source project was born out of the IPython Project in 2014 as it evolved to support interactive data science and scientific computing across all programming languages. Jupyter will always be 100% open source software, free for all to use and released under the liberal terms of the modified BSD license
<br><br>
(The above was taken from the [Project Jupyter about page](http://jupyter.org/about.html).)
%% Cell type:markdown id: tags:
#### In case you are unfamiliar with Jupyter
Jupyter runs a persistent python environment in the background. This means you can do all the usual Python stuff. Note: Jupyter also works with (has kernels for) the Julia and Ruby programming languages ([among others](https://github.com/jupyter/jupyter/wiki/Jupyter-kernels)). However, we'll only use Python. Regardless, commands are entered in code cells:
%% Cell type:code id: tags:
``` python
1+2+3
```
%% Output
6
%% Cell type:code id: tags:
``` python
import numpy as np
import re
import json
import datetime
## make some variables
mystring = "this is some text"
mylist = ["these", "are", "some", "words"]
mydict = {
"jake makes": "examples",
"which are": "boring"
}
## print and traverse a string
print(mystring)
print("")
for character in mystring:
print(character)
print("")
## print and traverse a list
print(mylist)
print("")
for myelement in mylist:
print(myelement)
print("")
## print and traverse a dcit
print(mydict)
print("")
for key in mydict:
print(key,mydict[key])
```
%% Output
this is some text
t
h
i
s
i
s
s
o
m
e
t
e
x
t
['these', 'are', 'some', 'words']
these
are
some
words
{'jake makes': 'examples', 'which are': 'boring'}
jake makes examples
which are boring
%% Cell type:code id: tags:
``` python
mydict
```
%% Output
{'jake makes': 'examples', 'which are': 'boring'}
%% Cell type:code id: tags:
``` python
mydict['jake makes']
```
%% Output
'examples'
%% Cell type:markdown id: tags:
#### Jupyter is good for code and project exposition
While comments in code help users and programmers to understand what's going on, Jupyter goes one step further by allowing you to break code down into separate, bite-sized pieces. As I am doing here, you can also intersperse markdown cells, which can be formatted to be much more readable. Also, you can do crazy stuff with $\LaTeX$ and equations:
$$\mu = \frac{\sum_{n=1}^N{x(x)}}{N}$$
and embed hyperlinks, for example, to the [Jupyter/IPython docs](http://ipython.readthedocs.io/en/stable/index.html). You can also make lists easily:
* thing 1
* thing 2
* etc
%% Cell type:markdown id: tags:
# This is my title
## this is a smaller title
%% Cell type:markdown id: tags:
## This is smaller title
%% Cell type:markdown id: tags:
$\mu = \frac{\sum_{n=1}^n (x(n)}{N}$
%% Cell type:markdown id: tags:
#### Jupyter is good for code and project exploration
Even though I wouldn't recommend writing your final, production-level code in Jupyter, it can definitely be helpful for a project when you are in the exploration and early-development stages. For all of its benefits, it's not hard to confuse yourself while developing in Jupyter, since the code execution is asynchronous. For example, you may create a variable, execute the cell, delete the cell, and continue to use the variable you've entered into memory. If you then shut down your notebook and try to run it all again a variable will be missing!
%% Cell type:markdown id: tags:
#### Jupyter has lots of add-on "magic" features
Jupyter's magic features are usually related to system control and navigation. These are generally run with a preceeded "%" character. For example, the "%whos" magic command neatly tells you everything that's in your namespace (workspace):
%% Cell type:code id: tags:
``` python
%whos
```
%% Output
Variable Type Data/Info
-------------------------------
character str t
datetime module <module 'datetime' from '<...>b/python3.6/datetime.py'>
json module <module 'json' from '/Use<...>hon3.6/json/__init__.py'>
key str which are
mydict dict n=2
myelement str words
mylist list n=4
mystring str this is some text
np module <module 'numpy' from '/Us<...>kages/numpy/__init__.py'>
re module <module 're' from '/Users<...>nda/lib/python3.6/re.py'>
%% Cell type:markdown id: tags:
There's also "%lsmagic", which lists all magic commands:
%% Cell type:code id: tags:
``` python
%lsmagic
```
%% Output
Available line magics:
%alias %alias_magic %autocall %automagic %autosave %bookmark %cat %cd %clear %colors %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 %popd %pprint %precision %profile %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
Available cell magics:
%%! %%HTML %%SVG %%bash %%capture %%debug %%file %%html %%javascript %%js %%latex %%perl %%prun %%pypy %%python %%python2 %%python3 %%ruby %%script %%sh %%svg %%sx %%system %%time %%timeit %%writefile
Automagic is ON, % prefix IS NOT needed for line magics.
%% Cell type:markdown id: tags:
#### What do "%%" magic commands do?
The double-percentage magic commands apply to a whole cell, for example the "%%writefile", which if entered as "%%writefile PATH/NAME.EXTENSION" at the top of a cell will write the cell's content to a file in the specified location. Also really cool are the language language-based magic commands that let you switch between programming languages. Note that these will not create persistent environments (kernels) for those languages. For example, we can use "%%html" to render a nice [example website](https://www.example.com) in Jupyter markdown:
%% Cell type:code id: tags:
``` python
%%html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<title></title>
<meta name="Generator" content="Cocoa HTML Writer">
<meta name="CocoaVersion" content="1504.82">
<style type="text/css">
body {background-color: #f0f0f2}
p.p2 {margin: 0.0px 0.0px 12.0px 0.0px; line-height: 14.0px; font: 12.0px 'Helvetica Neue'; color: #000000; -webkit-text-stroke: #000000}
p.p3 {margin: 0.0px 0.0px 12.0px 0.0px; line-height: 14.0px; font: 12.0px 'Helvetica Neue'; color: #38488f; -webkit-text-stroke: #38488f}
span.s1 {font-kerning: none; background-color: #ffffff}
span.s2 {font: 12.0px 'Helvetica Neue'; font-kerning: none; color: #38488f; -webkit-text-stroke: 0px #38488f}
</style>
</head>
<body>
<h1 style="margin: 0.0px 0.0px 16.1px 0.0px; line-height: 29.0px; font: 24.0px 'Helvetica Neue'; color: #000000; -webkit-text-stroke: #000000"><span class="s1"><b>Example Domain</b></span></h1>
<p class="p2"><span class="s1">This domain is established to be used for illustrative examples in documents. You may use this domain in examples without prior coordination or asking for permission.</span></p>
<p class="p3"><span class="s1"><a href="http://www.iana.org/domains/example">More information...<span class="s2"></span></a></span></p>
</body>
</html>
```
%% Output
%% Cell type:markdown id: tags:
#### Not everyone has Jupyter even though you may want to share!
This is ok, because you can use the Jupyter/IPython notebook viewer facility:
* https://nbviewer.jupyter.org
All you have to do is get your notebook into a public location on the internet and then place its url in the specified location (in the website above). This will generate a link that people can visit, where the notebook can be viewed as html through ANY browser, regardless of whether or not they have Jupyter. In fact, supposing you have not downloaded Jupyter yet this is how you are viewing this introduction&mdash;here's the link for [this very notebook](https://nbviewer.jupyter.org/url/www.pages.drexel.edu/%7Ejw3477/documents/GettingStartedWithJupyter.ipynb)!
%% Cell type:markdown id: tags:
#### What to do next?
Well, next week's will play with different kinds of data, but for now feel free to check out the offical gallery of interesting Jupyter notebooks:
* https://github.com/jupyter/jupyter/wiki/A-gallery-of-interesting-Jupyter-Notebooks
For example, there is the xkcd-style plotifier:
* http://nbviewer.jupyter.org/url/jakevdp.github.com/downloads/notebooks/XKCD_plots.ipynb
Also, keep the Jupyter/IPython docs handy for reference:
* http://ipython.readthedocs.io/en/stable/index.html
%% Cell type:code id: tags:
``` python
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment