EXERCISE: Colors example
Another Example
Another Example
Danger! Indicates a dangerous or potentially negative action.

Question Example:


Show Answer

Answer!!!

In [50]:
#show every cell time - maybe use %%time instead
# not longer work, see https://github.com/ipython/ipython/issues/8634
# %install_ext https://raw.github.com/cpcloud/ipython-autotime/master/autotime.py 

# %load_ext autotime


The autotime extension is already loaded. To reload it, use:
  %reload_ext autotime
time: 2 ms

Profiling:

чтобы использовать все кроме %prun - надо настроить как сказано в http://pynash.org/2013/03/06/timing-and-profiling/


In [1]:
import time

In [5]:
def foo(): time.sleep(1)

In [6]:
%prun foo()


 

In [7]:
def foo(n):
    phrase = 'repeat me'
    pmul = phrase * n
    pjoi = ''.join([phrase for x in xrange(n)])
    pinc = ''
    for x in xrange(n):
        pinc += phrase
    del pmul, pjoi, pinc

In [10]:
%mprun -f foo(100000)


ERROR:root:Line magic function `%mprun` not found.

In [11]:
%memit -r 3 [x for x in xrange(1000000)]


ERROR:root:Line magic function `%memit` not found.

In [ ]:


In [99]:
%matplotlib notebook
import pandas as pd
import matplotlib.pyplot as plt
from ipywidgets import *
from IPython.display import display
import ping
# from IPython.html import widgets
plt.style.use('ggplot')

NUMBER_OF_PINGS = 4

# displaying the text widget
text = widgets.Text(description="Domain to ping", width=200)
display(text)

# preparing the plot 
data = pd.DataFrame()
x = range(1,NUMBER_OF_PINGS+1)
plots = dict()
fig, ax = plt.subplots()
plt.xlabel('iterations')
plt.ylabel('ms')
plt.xticks(x)
plt.show()

# preparing a container to put in created checkbox per domain
checkboxes = []
cb_container = HBox()
display(cb_container)

# add button that updates the graph based on the checkboxes
button = Button(description="Update the graph")

# function to deal with the added domain name
def handle_submit(sender):
    # a part of the magic inside python : pinging
#     print text.value
#     res = !ping -n {NUMBER_OF_PINGS} {text.value}
#     print res[0:4]
#     hits = res.grep('32').fields(-2).s.replace("time=","").split()
    hits = np.random.uniform(low=10.0, high=1000.0, size=4)
    if len(hits) == 0:
        print "Domain gave error on pinging"
    else:
         # rebuild plot based on ping result
        data[text.value] = hits
        data[text.value] = data[text.value].astype(float)
        plots[text.value], = ax.plot(x, data[text.value], label=text.value)
        plt.legend()
        plt.draw()
        # add a new checkbox for the new domain
        checkboxes.append(widgets.Checkbox(description = text.value, value=True, width=90))
        cb_container.children=[i for i in checkboxes]
        if len(checkboxes) == 1:
            display(button)

# function to deal with the checkbox update button       
def on_button_clicked(b):
    for c in cb_container.children:
        if not c.value:
            plots[c.description].set_visible(False)
        else:
            plots[c.description].set_visible(True)
    plt.legend()
    plt.draw()

button.on_click(on_button_clicked)
text.on_submit(handle_submit)
plt.show()



In [4]:
# as for boruta, works in console but not in notebook
import xgboost


---------------------------------------------------------------------------
WindowsError                              Traceback (most recent call last)
<ipython-input-4-afdaff4619ce> in <module>()
----> 1 import xgboost

C:\Anaconda\lib\site-packages\xgboost-0.6-py2.7.egg\xgboost\__init__.py in <module>()
      9 import os
     10 
---> 11 from .core import DMatrix, Booster
     12 from .training import train, cv
     13 from . import rabit                   # noqa

C:\Anaconda\lib\site-packages\xgboost-0.6-py2.7.egg\xgboost\core.py in <module>()
    110 
    111 # load the XGBoost library globally
--> 112 _LIB = _load_lib()
    113 
    114 

C:\Anaconda\lib\site-packages\xgboost-0.6-py2.7.egg\xgboost\core.py in _load_lib()
    104     if len(lib_path) == 0:
    105         return None
--> 106     lib = ctypes.cdll.LoadLibrary(lib_path[0])
    107     lib.XGBGetLastError.restype = ctypes.c_char_p
    108     return lib

C:\Anaconda\lib\ctypes\__init__.pyc in LoadLibrary(self, name)
    438 
    439     def LoadLibrary(self, name):
--> 440         return self._dlltype(name)
    441 
    442 cdll = LibraryLoader(CDLL)

C:\Anaconda\lib\ctypes\__init__.pyc in __init__(self, name, mode, handle, use_errno, use_last_error)
    360 
    361         if handle is None:
--> 362             self._handle = _dlopen(self._name, mode)
    363         else:
    364             self._handle = handle

WindowsError: [Error 126] 

In [1]:
from xgboost import XGBClassifier


---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-9b31cfdb821c> in <module>()
----> 1 from xgboost import XGBClassifier

ImportError: No module named xgboost

In [6]:
%load_ext rmagic


C:\Anaconda\lib\site-packages\IPython\extensions\rmagic.py:11: UserWarning: The rmagic extension in IPython has moved to `rpy2.ipython`, please see `rpy2` documentation.
  warnings.warn("The rmagic extension in IPython has moved to "

In [9]:
%%R
x <- runif(10)


ERROR:root:Cell magic `%%R` not found.

In [10]:
%R X=c(1,4,5,7); sd(X); mean(X)


ERROR:root:Line magic function `%R` not found.

In [5]:
%load_ext rpy2.ipython


---------------------------------------------------------------------------
WindowsError                              Traceback (most recent call last)
<ipython-input-5-691c6d73b073> in <module>()
----> 1 get_ipython().magic(u'load_ext rpy2.ipython')

C:\Anaconda\lib\site-packages\IPython\core\interactiveshell.pyc in magic(self, arg_s)
   2144         magic_name, _, magic_arg_s = arg_s.partition(' ')
   2145         magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2146         return self.run_line_magic(magic_name, magic_arg_s)
   2147 
   2148     #-------------------------------------------------------------------------

C:\Anaconda\lib\site-packages\IPython\core\interactiveshell.pyc in run_line_magic(self, magic_name, line)
   2065                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2066             with self.builtin_trap:
-> 2067                 result = fn(*args,**kwargs)
   2068             return result
   2069 

<decorator-gen-63> in load_ext(self, module_str)

C:\Anaconda\lib\site-packages\IPython\core\magic.pyc in <lambda>(f, *a, **k)
    186     # but it's overkill for just that one bit of state.
    187     def magic_deco(arg):
--> 188         call = lambda f, *a, **k: f(*a, **k)
    189 
    190         if callable(arg):

C:\Anaconda\lib\site-packages\IPython\core\magics\extension.pyc in load_ext(self, module_str)
     35         if not module_str:
     36             raise UsageError('Missing module name.')
---> 37         res = self.shell.extension_manager.load_extension(module_str)
     38 
     39         if res == 'already loaded':

C:\Anaconda\lib\site-packages\IPython\core\extensions.pyc in load_extension(self, module_str)
     81             if module_str not in sys.modules:
     82                 with prepended_to_syspath(self.ipython_extension_dir):
---> 83                     __import__(module_str)
     84             mod = sys.modules[module_str]
     85             if self._call_load_ipython_extension(mod):

C:\Anaconda\lib\site-packages\rpy2\ipython\__init__.py in <module>()
----> 1 from .rmagic import load_ipython_extension

C:\Anaconda\lib\site-packages\rpy2\ipython\rmagic.py in <module>()
     50 # numpy and rpy2 imports
     51 
---> 52 import rpy2.rinterface as ri
     53 import rpy2.robjects as ro
     54 import rpy2.robjects.packages as rpacks

C:\Anaconda\lib\site-packages\rpy2\rinterface\__init__.py in <module>()
     14     R_HOME = (os.environ["R_HOME"], )
     15 except KeyError:
---> 16     tmp = subprocess.check_output(("R", "RHOME"), universal_newlines=True)
     17     R_HOME = tmp.split(os.linesep)
     18     del(tmp)

C:\Anaconda\lib\subprocess.pyc in check_output(*popenargs, **kwargs)
    565     if 'stdout' in kwargs:
    566         raise ValueError('stdout argument not allowed, it will be overridden.')
--> 567     process = Popen(stdout=PIPE, *popenargs, **kwargs)
    568     output, unused_err = process.communicate()
    569     retcode = process.poll()

C:\Anaconda\lib\subprocess.pyc in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags)
    709                                 p2cread, p2cwrite,
    710                                 c2pread, c2pwrite,
--> 711                                 errread, errwrite)
    712         except Exception:
    713             # Preserve original exception in case os.close raises.

C:\Anaconda\lib\subprocess.pyc in _execute_child(self, args, executable, preexec_fn, close_fds, cwd, env, universal_newlines, startupinfo, creationflags, shell, to_close, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite)
    957                                          env,
    958                                          cwd,
--> 959                                          startupinfo)
    960             except pywintypes.error, e:
    961                 # Translate pywintypes.error to WindowsError, which is

WindowsError: [Error 2] 

In [7]:
import rpy2
from rpy2.robjects import r

print dir(r)


---------------------------------------------------------------------------
WindowsError                              Traceback (most recent call last)
<ipython-input-7-bded7f36a2c2> in <module>()
      1 import rpy2
----> 2 from rpy2.robjects import r
      3 
      4 print dir(r)

C:\Anaconda\lib\site-packages\rpy2\robjects\__init__.py in <module>()
     13 import itertools
     14 from datetime import datetime
---> 15 import rpy2.rinterface as rinterface
     16 import rpy2.rlike.container as rlc
     17 

C:\Anaconda\lib\site-packages\rpy2\rinterface\__init__.py in <module>()
     14     R_HOME = (os.environ["R_HOME"], )
     15 except KeyError:
---> 16     tmp = subprocess.check_output(("R", "RHOME"), universal_newlines=True)
     17     R_HOME = tmp.split(os.linesep)
     18     del(tmp)

C:\Anaconda\lib\subprocess.pyc in check_output(*popenargs, **kwargs)
    565     if 'stdout' in kwargs:
    566         raise ValueError('stdout argument not allowed, it will be overridden.')
--> 567     process = Popen(stdout=PIPE, *popenargs, **kwargs)
    568     output, unused_err = process.communicate()
    569     retcode = process.poll()

C:\Anaconda\lib\subprocess.pyc in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags)
    709                                 p2cread, p2cwrite,
    710                                 c2pread, c2pwrite,
--> 711                                 errread, errwrite)
    712         except Exception:
    713             # Preserve original exception in case os.close raises.

C:\Anaconda\lib\subprocess.pyc in _execute_child(self, args, executable, preexec_fn, close_fds, cwd, env, universal_newlines, startupinfo, creationflags, shell, to_close, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite)
    957                                          env,
    958                                          cwd,
--> 959                                          startupinfo)
    960             except pywintypes.error, e:
    961                 # Translate pywintypes.error to WindowsError, which is

WindowsError: [Error 2] 

%Rpush and %Rpull


In [68]:
%load_ext sql


C:\Anaconda\lib\site-packages\IPython\config.py:13: ShimWarning: The `IPython.config` package has been deprecated. You should import from traitlets.config instead.
  "You should import from traitlets.config instead.", ShimWarning)
C:\Anaconda\lib\site-packages\IPython\utils\traitlets.py:5: UserWarning: IPython.utils.traitlets has moved to a top-level traitlets package.
  warn("IPython.utils.traitlets has moved to a top-level traitlets package.")

In [73]:
%sql sqlite://


Out[73]:
'Connected: None@None'

In [75]:
%%sql
CREATE TABLE writer (fn, ln, year);
INSERT INTO writer VALUES ('Will', 'Sheak', 1616);
INSERT INTO writer VALUES ('Baddy', 'Butch', 1916);


Done.
1 rows affected.
1 rows affected.
Out[75]:
[]

In [76]:
%%sql
SELECT * FROM writer
WHERE year = 1616


Done.
Out[76]:
fn ln year
Will Sheak 1616