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
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)
In [11]:
%memit -r 3 [x for x in xrange(1000000)]
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()
http://machinelearningmastery.com/feature-importance-and-feature-selection-with-xgboost-in-python/
In [4]:
# as for boruta, works in console but not in notebook
import xgboost
In [1]:
from xgboost import XGBClassifier
In [6]:
%load_ext rmagic
In [9]:
%%R
x <- runif(10)
In [10]:
%R X=c(1,4,5,7); sd(X); mean(X)
In [5]:
%load_ext rpy2.ipython
In [7]:
import rpy2
from rpy2.robjects import r
print dir(r)
%Rpush and %Rpull
In [68]:
%load_ext sql
In [73]:
%sql sqlite://
Out[73]:
In [75]:
%%sql
CREATE TABLE writer (fn, ln, year);
INSERT INTO writer VALUES ('Will', 'Sheak', 1616);
INSERT INTO writer VALUES ('Baddy', 'Butch', 1916);
Out[75]:
In [76]:
%%sql
SELECT * FROM writer
WHERE year = 1616
Out[76]: