Numerical Computation : NumPy


In [1]:
import numpy as np
from __future__ import print_function

In [2]:
x = np.arange(10)
print (x)


[0 1 2 3 4 5 6 7 8 9]

In [3]:
print(x * 2 + 1)


[ 1  3  5  7  9 11 13 15 17 19]

In [4]:
x = np.random.rand(10000000)

In [5]:
%%timeit

y = 2*x + 1


10 loops, best of 3: 34.9 ms per loop

Labeled Data : Pandas


In [6]:
import pandas as pd

In [7]:
df = pd.DataFrame({'id' : ['A', 'B', 'A', 'b'], 'val' : [1, 2, 3, 4]})
print(df)


  id  val
0  A    1
1  B    2
2  A    3
3  b    4

In [8]:
grouped = df.groupby('id').sum()
print(grouped)


    val
id     
A     4
B     2
b     4

Visualization : Matplotlib


In [9]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

In [10]:
x = np.linspace(0 ,10 ,1000)
plt.plot(x, np.sin(x))


Out[10]:
[<matplotlib.lines.Line2D at 0x1231a5278>]

Numberical Algorithms : SciPy


In [11]:
import numpy as np
import matplotlib.pyplot as plt
from scipy import special, optimize

x = np.linspace(0, 10, 1000)
opt = optimize.minimize(special.j1, x0=3)
plt.plot(x, special.j1(x))
plt.plot(opt.x, special.j1(opt.x), marker='o', color='red')


Out[11]:
[<matplotlib.lines.Line2D at 0x1232492e8>]

Machine Learning with scikit-learn


In [14]:
x = 10*np.random.rand(100)
y = np.sin(x) + 0.1*np.random.rand(100)
plt.plot(x, y, '.k')


Out[14]:
[<matplotlib.lines.Line2D at 0x12474ec18>]

In [16]:
from sklearn.ensemble import RandomForestRegressor
model = RandomForestRegressor()

model.fit(x[:, np.newaxis], y)
xfit = np.linspace(-1, 11, 1000)
yfit = model.predict(xfit[:, np.newaxis])

plt.plot(x, y, '.k')
plt.plot(xfit, yfit)


Out[16]:
[<matplotlib.lines.Line2D at 0x12575eb70>]

In [ ]: