IPython

Python


In [ ]:
a = 1 + 2

In [ ]:
a

In [ ]:
a + 1

In [ ]:
_

In [ ]:
? sum

In [ ]:
! ps -xa | grep python

In [ ]:
import time
%time time.sleep(1)

Markdown

link

  • 1
  • 2
    • a
    • b
  • 3

multiline
text

NumPy


In [ ]:
import numpy as np

In [ ]:
np.array([[1,2,3], [7,1,2]])

In [ ]:
data = np.array([1,2,3,4,5])
data

In [ ]:
data[1:-2]

In [ ]:
data + 1

In [ ]:
data * 2

In [ ]:
data * data

In [ ]:
np.sum(data * data)

In [ ]:
data.dot(data)

In [ ]:
data > 2

In [ ]:
data[data > 2]

In [ ]:
replaces = data[:]
replaces[data > 2] = -1
replaces

In [ ]:
matrix = np.random.rand(5, 4)

In [ ]:
matrix

In [ ]:
matrix[1]

In [ ]:
matrix[:, 1]

In [ ]:
matrix[(1, 2), :]

In [ ]:
import random
vec_len = 1000
v1 = [random.random() for _ in range(vec_len)]
v2 = [random.random() for _ in range(vec_len)]

In [ ]:
%timeit [a * b for a, b in zip(v1, v2)]

In [ ]:
np_v1, np_v2 = np.array(v1), np.array(v2)
%timeit np_v1 * np_v2

In [ ]:
np.arange(1, 5, 0.7)

In [ ]:
np.linspace(1, 5, 13)

In [ ]:
first = np.array([[1, 2], [3, 4]])
second = np.array([[10, 20], [30, 40]])
np.hstack((first, second))

In [ ]:
np.vstack((first, second))

Pandas


In [ ]:
import pandas as pd

In [ ]:
df = pd.read_csv('iris.csv')

In [ ]:
df.head(3)

In [ ]:
df[['Sepal.length', 'Petal.length']].head(5)

In [ ]:
df[df['Sepal.length'] > 7.5]

In [ ]:
df.describe()

In [ ]:
%pylab inline

In [ ]:
df['Sepal.length'].plot(kind='hist');

Matplotlib


In [ ]:
import matplotlib.pyplot as plt

In [ ]:
x = np.arange(1, 10, 0.2)
y = np.sin(x)
plt.plot(x, y);

In [ ]:
plt.scatter(x, y);

In [ ]:
plt.plot(x, y, marker='o');