<jupyter_start><jupyter_text>Complessità nei sistemi socialiLaurea Magistrale in Fisica Dei Sistemi Complessi A.A. 2018/19<NAME> & <NAME>Notebook 1 - Python and Jupyter Notebook This is a very short intro to Python. We will use Python and Jupyter notebooks for all our analyses.  Python is easy :))<jupyter_code>from IPython.display import Image
Image(url='http://imgs.xkcd.com/comics/python.png')<jupyter_output><empty_output><jupyter_text>**Many more resources are available on the Web** Here, I list two good references:1. [Scientific Python lectures by <NAME>](http://github.com/jrjohansson/scientific-python-lectures)2. [A very good intro tutorial by <NAME>](http://introtopython.org/)In the following, the content of this notebook will mainly follow the lectures by <NAME>.   Install The easiest way to install Python and the Jupyter notebook is through the [Anaconda distribution](https://www.continuum.io/downloads) for every OS.The official documentation of the Jupyter project and how to install it can be found [here](http://jupyter.org/install)More info [here](http://nbviewer.jupyter.org/github/jrjohansson/scientific-python-lectures/blob/master/Lecture-0-Scientific-Computing-with-Python.ipynb).  Basic concepts Run a code cell using Shift-Enter or pressing the "Play" button in the toolbar above:<jupyter_code>ls ./<jupyter_output>README.md                            nb01_Python_Jupyter_notebook.ipynb
data.csv                             nb02_data_import_and_networks.ipynb<jupyter_text>Code is run in a separate process called the IPython Kernel. The Kernel can be interrupted or restarted.   Modules Most of the functionality in Python is provided by modules. The Python Standard Library is a large collection of modules that provides cross-platform implementations of common facilities such as access to the operating system, file I/O, string management, network communication, and much more.<jupyter_code>import math
x = math.cos(2 * math.pi)
print(x)<jupyter_output>1.0<jupyter_text>We can also import only some symbols in the namespace<jupyter_code>from math import cos, pi

x = cos(2 * pi)

print(x)<jupyter_output>1.0<jupyter_text>Or assign a module to a different symbol<jupyter_code>import math as mth

x = mth.cos(2 * pi)

print(x)
help(math)<jupyter_output>Help on module math:

NAME
    math

MODULE REFERENCE
    https://docs.python.org/3.6/library/math
    
    The following documentation is automatically generated from the Python
    source files.  It may be incomplete, incorrect or include features that
    are considered implementation detail and may vary between Python
    implementations.  When in doubt, consult the module reference at the
    location listed above.

DESCRIPTION
    This module is always available.  It provides access to the
    mathematical functions defined by the C standard.

FUNCTIONS
    acos(...)
        acos(x)
        
        Return the arc cosine (measured in radians) of x.
    
    acosh(...)
        acosh(x)
        
        Return the inverse hyperbolic cosine of x.
    
    asin(...)
        asin(x)
        
        Return the arc sine (measured in radians) of x.
    
    asinh(...)
        asinh(x)
        
        Return the inverse hyperbolic sine of x.
    
    atan(...)
        atan(x)
        
 [...]<jupyter_text>Tab completion:<jupyter_code>import numpy
numpy.random.sample<jupyter_output><empty_output><jupyter_text>The NumPy module provides structures and functions for scientific computing (http://www.numpy.org/) Adding ? opens the docstring in the pager below:<jupyter_code>numpy.random?<jupyter_output><empty_output><jupyter_text>Exceptions are formatted nicely:<jupyter_code>x = 1
y = 4
z = y/(1-x)<jupyter_output><empty_output><jupyter_text>Variables and types The assignment operator in Python is `=`. Python is a dynamically typed language, so we do not need to specify the type of a variable when we create one.Assigning a value to a new variable creates the variable:<jupyter_code>a = 1
b = 1.2
c = 'my string'
type(a)
type(b)
type(c)<jupyter_output><empty_output><jupyter_text>The %load magic lets you load code from URLs or local files:<jupyter_code>%load?<jupyter_output><empty_output><jupyter_text>Compound types: strings, list and dictionaries **Strings** are the variable type that is used for storing text.<jupyter_code>c
c[0]
c[:5]
c[-1]<jupyter_output><empty_output><jupyter_text>Python has a very rich set of functions for text processing. See for example http://docs.python.org/2/library/string.html for more information. **Lists** are very similar to strings, except that each element can be of any type.<jupyter_code>l = [0,1,2,3,4]

print(type(l))
print(l)
l[-1]
len(l)
start = 10
stop = 30
step = 2

l=range(start, stop, step)
for i in range(start, stop, step):
    print(i)<jupyter_output>10
12
14
16
18
20
22
24
26
28<jupyter_text>Tuples **Tuples** are like lists, except that they cannot be modified once created, that is they are *immutable*.<jupyter_code>point = (10, 20)

print(point, type(point))
point[0] = 20<jupyter_output><empty_output><jupyter_text>Dictionaries **Dictionaries** are also like lists, except that each element is a key-value pair. The syntax for dictionaries is `{key1 : value1, ...}`:<jupyter_code>params = {"parameter1" : 1.0,
          "parameter2" : 2.0,
          "parameter3" : 3.0}

print(type(params))
print(params)
params.keys()
params['parameter1']
params['parameter1']=4.0
params['parameter4']=6.0
params<jupyter_output><empty_output><jupyter_text>Markdown Text can be added to Jupyter Notebooks using Markdown cells. Markdown is a popular markup language that is a superset of HTML. Its specification can be found here:http://daringfireball.net/projects/markdown/ You can make text *italic* or **bold**.You can build nested itemized or enumerated lists:* One    - Sublist        - This  - Sublist        - That        - The other thing* Two  - Sublist* Three  - SublistNow another list:1. Here we go    1. Sublist    2. Sublist2. There we go3. Now thisYou can add horizontal rules:---Here is a blockquote:> Beautiful is better than ugly.> Explicit is better than implicit.> Simple is better than complex.> Complex is better than complicated.> Flat is better than nested.> Sparse is better than dense.> Readability counts.> Special cases aren't special enough to break the rules.> Although practicality beats purity.> Errors should never pass silently.> Unless explicitly silenced.> In the face of ambiguity, refuse the temptation to guess.> There should be one-- and preferably only one --obvious way to do it.> Although that way may not be obvious at first unless you're Dutch.> Now is better than never.> Although never is often better than *right* now.> If the implementation is hard to explain, it's a bad idea.> If the implementation is easy to explain, it may be a good idea.> Namespaces are one honking great idea -- let's do more of those!And shorthand for links:[IPython's website](http://ipython.org)If you want, you can add headings using Markdown's syntax: Heading 1 Heading 2 Heading 2.1 Heading 2.2 You can embed code meant for illustration instead of execution in Python:    def f(x):        """a docstring"""        return x**2or other languages:    if (i=0; i<n; i++) {      printf("hello %d\n", i);      x += 4;    } Because Markdown is a superset of HTML you can even add things like HTML tables:Header 1Header 2row 1, cell 1row 1, cell 2row 2, cell 1row 2, cell 2  Rich Display System To work with images (JPEG, PNG) use the Image class.<jupyter_code>from IPython.display import Image
Image(url='http://python.org/images/python-logo.gif')<jupyter_output><empty_output><jupyter_text>More exotic objects can also be displayed, as long as their representation supports the IPython display protocol. For example, videos hosted externally on YouTube are easy to load (and writing a similar wrapper for other hosted content is trivial):<jupyter_code>from IPython.display import YouTubeVideo
YouTubeVideo('26wgEsg9Mcc')<jupyter_output><empty_output><jupyter_text>Python objects can declare HTML representations that will be displayed in the Notebook. If you have some HTML you want to display, simply use the HTML class. You can even embed an entire page from another site in an iframe; for example this is today's Wikipedia page for mobile users:<jupyter_code>from IPython.display import HTML
HTML('<iframe src=http://en.m.wikipedia.org/wiki/Main_Page></iframe>')<jupyter_output><empty_output><jupyter_text>LaTeX IPython Notebook supports the display of mathematical expressions typeset in LaTeX<jupyter_code>from IPython.display import Math
Math(r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx')
from IPython.display import Latex
Latex(r"""\begin{eqnarray}
\nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} & = \frac{4\pi}{c}\vec{\mathbf{j}} \\
\nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \\
\nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\
\nabla \cdot \vec{\mathbf{B}} & = 0 
\end{eqnarray}""")<jupyter_output><empty_output><jupyter_text>Pandas *pandas* is an open source, BSD-licensed library providing high-performance, easy-to-use data structures and data analysis tools for the Python programming language.https://pandas.pydata.org/<jupyter_code>import pandas as pd
%%file data.csv
Date,Open,High,Low,Close,Volume,Adj Close
2012-06-01,569.16,590.00,548.50,584.00,14077000,581.50
2012-05-01,584.90,596.76,522.18,577.73,18827900,575.26
2012-04-02,601.83,644.00,555.00,583.98,28759100,581.48
2012-03-01,548.17,621.45,516.22,599.55,26486000,596.99
2012-02-01,458.41,547.61,453.98,542.44,22001000,540.12
2012-01-03,409.40,458.24,409.00,456.48,12949100,454.53
df = pd.read_csv('data.csv')
df
df.Volume.max()
df.Low.min()
df['Diff']=df['High']-df['Low']
df<jupyter_output><empty_output><jupyter_text>Matplotlib and plotting Matplotlib is an excellent 2D and 3D graphics library for generating scientific figures. Some of the many advantages of this library include:* Easy to get started* Support for $\LaTeX$ formatted labels and texts* Great control of every element in a figure, including figure size and DPI. * High-quality output in many formats, including PNG, PDF, SVG, EPS, and PGF.* GUI for interactively exploring figures *and* support for headless generation of figure files (useful for batch jobs).All aspects of the figure can be controlled *programmatically*. This is important for reproducibility and convenient when one needs to regenerate the figure with updated data or change its appearance. More information at the Matplotlib web page: http://matplotlib.org/<jupyter_code>%pylab inline
df.Diff.plot()
import numpy as np
x = np.linspace(0, 5, 10)
y = x ** 2
x
plt.figure()
plt.plot(x, y, 'r')
plt.xlabel('x')
plt.ylabel('y')
plt.title('title')
plt.show()<jupyter_output><empty_output><jupyter_text>Great documentation and basic tutorial available from the lectures of [<NAME>](http://nbviewer.jupyter.org/github/jrjohansson/scientific-python-lectures/blob/master/Lecture-4-Matplotlib.ipynb)  Seaborn  Seaborn is a Python visualization library based on matplotlib. It provides a high-level interface for drawing attractive statistical graphics.https://seaborn.pydata.org/<jupyter_code>import seaborn as sns
df.High.hist()
sns.kdeplot(df.High)
sns.kdeplot(df.High, bw=10)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10235046483073144, 'fraction_numerical': 0.03323978249429924, 'mean_word_length': 4.527387300048473, 'pattern_counts': {'":': 0, '<': 89, '<?xml version=': 0, '>': 107, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, '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': '21', 'dir': 'jupyter-structured-clean-dedup', 'id': '48344', 'license': 'MIT', 'n_tokens_mistral': 3855, 'n_tokens_neox': 3560, 'n_words': 1246, 'path': '1-intro/nb01_Python_Jupyter_notebook.ipynb', 'repo_name': 'simonepoetto/Complessit-', 'size': '11457'}