In [1]:
%load_ext watermark
%watermark
cmd + shift + P
ctrl + shift + P
In [2]:
print("Hello world!")
In [5]:
!ls
In [6]:
%lsmagic
Out[6]:
In [7]:
%%html
<h1>Hello from html</h1>
<p>Here is some text</p>
In [ ]:
%env
In [8]:
!cat hello-world.py
In [9]:
%run hello-world.py
In [10]:
my_var = "my variable"
%store my_var
del my_var
In [12]:
%store -r my_var
my_var
Out[12]:
In [13]:
var_one = 1
var_two = 2
var_three = 3
%who
In [14]:
var_two = 20
In [15]:
sum_one_two = var_one + var_two
In [16]:
sum_one_two
Out[16]:
In [ ]:
def sum_one_two(var_one, var_two):
return var_one + var_two
sum_one_two(1, 20)
In [17]:
%%writefile fe.py
def feature_engineering(data):
data['year'] = data['datetime'].dt.year
data['diff_year'] = data['year'] - 2010
data['month'] = data['datetime'].dt.month
data['day'] = data['datetime'].dt.day
data['hour'] = data['datetime'].dt.hour
data['minute'] = data['datetime'].dt.minute
data['dayofweek'] = data['datetime'].dt.dayofweek
data['weekofyear'] = data['datetime'].dt.weekofyear
data['weekend'] = data.dayofweek.map(lambda x: int(x in [5,6]) )
data['time_of_day'] = data['hour'].map(cat_hour)
data['dayofyear'] = data['datetime'].dt.dayofyear
data['day_'] = data[ ['year', 'dayofyear'] ].apply(lambda x: x['dayofyear'] + int(str(x['year'])[-1]) * 365 , axis=1)
data['rush_hour'] = data['datetime'].apply(lambda i: min([np.fabs(9-i.hour), np.fabs(20-i.hour)]))
data.loc[:,('rush_hour')] = data['datetime'].apply(lambda i: np.fabs(14-i.hour))
data.loc[data['workingday'] != 0].loc[:,('rush_hour')] = 0
data['holiday'] = data[['month', 'day', 'holiday', 'year']].apply(lambda x: (x['holiday'], 1)[x['year'] == 2012 and x['month'] == 10 and (x['day'] in [30])], axis = 1)
data['holiday'] = data[['month', 'day', 'holiday']].apply(lambda x: (x['holiday'], 1)[x['month'] == 12 and (x['day'] in [24, 26, 31])], axis = 1)
data['workingday'] = data[['month', 'day', 'workingday']].apply(lambda x: (x['workingday'], 0)[x['month'] == 12 and x['day'] in [24, 31]], axis = 1)
data['peak'] = data[['hour', 'workingday']].apply(lambda x: (0, 1)[(x['workingday'] == 1 and ( x['hour'] == 8 or 17 <= x['hour'] <= 18 or 12 <= x['hour'] <= 12)) or (x['workingday'] == 0 and 10 <= x['hour'] <= 19)], axis = 1)
data['sticky'] = data[['humidity', 'workingday']].apply(lambda x: (0, 1)[x['workingday'] == 1 and x['humidity'] >= 60], axis = 1)
return data
print("done")
In [18]:
%pycat fe.py
In [19]:
%run fe.py
In [20]:
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
In [21]:
def f(x): return np.pi*np.sin(x) + np.pi*np.cos(np.pi * x) + np.sin(x + np.pi)
x = np.linspace(0, 10, 100)# generate 100 points between 0 and 10
y = f(x)
plt.plot(x, y)
Out[21]:
In [ ]:
x = [
'one',
'two',
'three'
]
In [22]:
from sklearn.externals.joblib import Memory
memory = Memory(cachedir='/tmp', verbose=1)
@memory.cache
def f(x):
print('Running f(%s)' % x)
return x
In [23]:
f(1)
Out[23]:
In [24]:
f(1)
Out[24]:
In [25]:
f(2)
Out[25]:
In [26]:
from tqdm import tqdm
import time
for i in tqdm(range(100)):
time.sleep(0.1)
To install it, please run:
!pip install jupyternotify
and restart kernel after.
In [5]:
import jupyternotify
ip = get_ipython()
ip.register_magics(jupyternotify.JupyterNotifyMagics)
%notify -m "sleep for 5 secs"
# import time
# time.sleep(5)
In [ ]: