Back to Data Science Ipython Notebooks

NumPy

numpy/numpy.ipynb

latest1.9 KB
Original Source

NumPy

Credits: Forked from Parallel Machine Learning with scikit-learn and IPython by Olivier Grisel

  • NumPy Arrays, dtype, and shape
  • Common Array Operations
  • Reshape and Update In-Place
  • Combine Arrays
  • Create Sample Data
python
import numpy as np

NumPy Arrays, dtypes, and shapes

python
a = np.array([1, 2, 3])
print(a)
print(a.shape)
print(a.dtype)
python
b = np.array([[0, 2, 4], [1, 3, 5]])
print(b)
print(b.shape)
print(b.dtype)
python
np.zeros(5)
python
np.ones(shape=(3, 4), dtype=np.int32)

Common Array Operations

python
c = b * 0.5
print(c)
print(c.shape)
print(c.dtype)
python
d = a + c
print(d)
python
d[0]
python
d[0, 0]
python
d[:, 0]
python
d.sum()
python
d.mean()
python
d.sum(axis=0)
python
d.mean(axis=1)

Reshape and Update In-Place

python
e = np.arange(12)
print(e)
python
# f is a view of contents of e
f = e.reshape(3, 4)
print(f)
python
# Set values of e from index 5 onwards to 0
e[5:] = 0
print(e)
python
# f is also updated
f
python
# OWNDATA shows f does not own its data
f.flags

Combine Arrays

python
a
python
b
python
d
python
np.concatenate([a, a, a])
python
# Use broadcasting when needed to do this automatically
np.vstack([a, b, d])
python
# In machine learning, useful to enrich or 
# add new/concatenate features with hstack
np.hstack([b, d])

Create Sample Data

python
%matplotlib inline

import pylab as plt
import seaborn

seaborn.set()
python
# Create evenly spaced numbers over the specified interval
x = np.linspace(0, 2, 10)
plt.plot(x, 'o-');
plt.show()
python
# Create sample data, add some noise
x = np.random.uniform(1, 100, 1000)
y = np.log(x) + np.random.normal(0, .3, 1000)

plt.scatter(x, y)
plt.show()