In [1]:
import numpy as np
In [2]:
np.array([1, 2, 3, 4])
Out[2]:
In [3]:
np.array([1, 2, 3, 4], dtype='float32')
Out[3]:
In [4]:
np.array([range(i, i + 3) for i in [2, 4, 6]])
Out[4]:
In [5]:
np.zeros(10, dtype=int)
Out[5]:
In [6]:
np.ones((3, 5), dtype=float)
Out[6]:
In [7]:
np.full((3, 5), 3.14)
Out[7]:
In [8]:
np.arange(0, 20, 2)
Out[8]:
In [9]:
np.linspace(0, 1, 5)
Out[9]:
In [10]:
np.random.random((3, 3))
Out[10]:
In [11]:
np.random.normal(0, 1, (3, 3)) # mean=0, std=1
Out[11]:
In [12]:
np.random.randint(0, 10, (3, 3)) # [0,10)
Out[12]:
In [13]:
np.eye(3)
Out[13]:
In [14]:
np.empty(3) # no value init, values will be whatever happens to already exist at that memory location
Out[14]:
In [15]:
np.random.seed(0) # seed for reproducibility
x1 = np.random.randint(10, size=6) # One-dimensional array
x2 = np.random.randint(10, size=(3, 4)) # Two-dimensional array
x3 = np.random.randint(10, size=(3, 4, 5)) # Three-dimensional array
print("x3 ndim: ", x3.ndim)
print("x3 shape:", x3.shape)
print("x3 size: ", x3.size)
print("dtype:", x3.dtype)
print("itemsize:", x3.itemsize, "bytes")
print("nbytes:", x3.nbytes, "bytes")
In [16]:
x1
Out[16]:
In [17]:
x1[0], x1[3], x1[-1], x1[-2]
Out[17]:
In [18]:
x2
Out[18]:
In [19]:
x2[0, 0], x2[2, 0], x2[2, -1]
Out[19]:
In [20]:
x2[0, 0] = 12
x2
Out[20]:
In [21]:
x = np.arange(10)
x
Out[21]:
In [22]:
x[:5]
Out[22]:
In [23]:
x[5:]
Out[23]:
In [24]:
x[2:5]
Out[24]:
In [25]:
x[::2]
Out[25]:
In [26]:
x[1::2]
Out[26]:
In [27]:
x[::-1]
Out[27]:
In [28]:
x[5::-2]
Out[28]:
In [29]:
x2
Out[29]:
In [30]:
x2[:2, :3]
Out[30]:
In [31]:
x2[:2, ::2]
Out[31]:
In [32]:
x2[::-1, ::-1]
Out[32]:
In [33]:
x2[:, 0]
Out[33]:
In [34]:
x2[0] # == x2[0, :]
Out[34]:
In [35]:
x2_sub = x2[:2, :2] # view
x2_sub
Out[35]:
In [36]:
x2_sub[0, 0] = 99
x2_sub
Out[36]:
In [37]:
x2
Out[37]:
In [38]:
x2_sub_copy = x2[:2, :2].copy() # copy
x2_sub_copy[0, 0] = 42
print x2_sub_copy
print x2
In [39]:
np.arange(1, 10).reshape((3, 3))
Out[39]:
In [40]:
x = np.array([1, 2, 3])
print x.shape
print x[:, np.newaxis].shape
print x[np.newaxis, :].shape
print x.reshape(-1, 1).shape
print x.reshape(1, -1).shape
In [41]:
x = np.array([1, 2, 3])
y = np.array([3, 2, 1])
print np.concatenate([x, y])
z = [9, 9, 9]
print np.concatenate([x, y, z])
In [42]:
grid = np.array([[1, 2, 3],
[4, 5, 6]])
print np.concatenate([grid, grid])
print np.concatenate([grid, grid], axis=1)
In [43]:
x = np.array([1, 2, 3])
grid = np.array([[9, 8, 7],
[6, 5, 4]])
# vertically stack the arrays
np.vstack([x, grid])
Out[43]:
In [44]:
y = np.array([[99],
[99]])
# horizontally stack the arrays
np.hstack([grid, y])
Out[44]:
In [45]:
x=[1,2,3,99,99,3,2,1]
x1, x2, x3 = np.split(x, [3, 5])
print(x1, x2, x3)
In [46]:
a, b = np.vsplit(np.arange(16).reshape((4, 4)), [2])
print a
print b
In [47]:
a, b = np.hsplit(np.arange(16).reshape((4, 4)), [2])
print a
print b
In [48]:
def compute_reciprocals(values):
output = np.empty(len(values))
for i in range(len(values)):
output[i] = 1.0 / values[i]
return output
big_array = np.random.randint(1, 100, size=1000000)
%timeit compute_reciprocals(big_array)
In [49]:
%timeit (1.0 / big_array)
Operator Equivalent ufunc
In [50]:
x = np.arange(4)
print("x =", x)
print("x + 5 =", x + 5)
print("x - 5 =", x - 5)
print("x * 2 =", x * 2)
print("x / 2 =", x / 2)
print("x // 2 =", x // 2) # floor division
print("-x = ", -x)
print("x ** 2 = ", x ** 2)
print("x % 2 = ", x % 2)
print('-(0.5*x + 1) ** 2 = ', -(0.5*x + 1) ** 2)
In [51]:
np.absolute is np.abs
Out[51]:
In [52]:
theta = np.linspace(0, np.pi, 3)
print("theta = ", theta)
print("sin(theta) = ", np.sin(theta))
print("cos(theta) = ", np.cos(theta))
print("tan(theta) = ", np.tan(theta))
x = [-1, 0, 1]
print("x = ", x)
print("arcsin(x) = ", np.arcsin(x))
print("arccos(x) = ", np.arccos(x))
print("arctan(x) = ", np.arctan(x))
In [53]:
x = [1, 2, 3]
print("x =", x)
print("e^x =",np.exp(x))
print("2^x =",np.exp2(x))
print("3^x =",np.power(3,x))
x = [1, 2, 4, 10]
print("x =", x)
print("ln(x) =", np.log(x))
print("log2(x) =", np.log2(x))
print("log10(x) =", np.log10(x))
x = [0, 0.001, 0.01, 0.1]
print("exp(x) - 1 =", np.expm1(x))
print("log(1 + x) =", np.log1p(x))
In [54]:
from scipy import special
# Gamma functions (generalized factorials) and related functions
x=[1,5,10]
print("gamma(x) =", special.gamma(x))
print("ln|gamma(x)| =", special.gammaln(x))
print("beta(x, 2) =", special.beta(x, 2))
# Error function (integral of Gaussian) # its complement, and its inverse
x = np.array([0, 0.3, 0.7, 1.0])
print("erf(x) =", special.erf(x))
print("erfc(x) =", special.erfc(x))
print("erfinv(x) =", special.erfinv(x))
In [55]:
# For all ufuncs, you can do this using the out argument of the function:
x = np.arange(5)
y = np.empty(5)
np.multiply(x, 10, out=y)
print(y)
In [56]:
# for very large arrays the memory savings from careful use of the out argument can be significant
y = np.zeros(10)
np.power(2, x, out=y[::2])
print(y)
In [57]:
big_array = np.random.rand(1000000)
%timeit sum(big_array)
%timeit np.sum(big_array)
In [58]:
%timeit min(big_array)
%timeit np.min(big_array)
In [59]:
a = np.array([0, 1, 2])
b = np.array([5, 5, 5])
print a+b
print a+5
In [60]:
a = np.arange(3)
b = np.arange(3)[:, np.newaxis]
print(a.shape, b.shape)
print(a+b)
In [61]:
a = np.arange(3).reshape((3, 1))
b = np.arange(3)
print(a+b)
In [62]:
M = np.ones((3, 2))
a = np.arange(3)
M+a
In [63]:
M + a[:, np.newaxis]
Out[63]:
In [64]:
X = np.random.random((10, 3))
Xmean = X.mean(0)
print Xmean
X_centered = X - Xmean
print X_centered.mean(0)
In [65]:
%matplotlib inline
import matplotlib.pyplot as plt
x = np.linspace(0, 5, 50)
y = np.linspace(0, 5, 50)[:, np.newaxis]
z = np.sin(x) ** 10 + np.cos(10 + y * x) * np.cos(x)
plt.imshow(z, origin='lower', extent=[0, 5, 0, 5], cmap='viridis')
plt.colorbar()
Out[65]:
In [66]:
x = np.array([1, 2, 3, 4, 5])
print x < 3 # less than
print x > 3 # greater than
print x <= 3 # less than or equal
print x >= 3 # greater than or equal
print x != 3 # not equal
print x == 3 # equal
In [67]:
rng = np.random.RandomState(0)
x = rng.randint(10, size=(3, 4))
print x
print x < 6
In [68]:
print np.sum(x < 6)
# how many values less than 6 in each row
print np.sum(x < 6, axis=1)
In [69]:
print np.any(x > 8)
print np.all(x < 10)
print np.all(x < 8, axis=1)
In [70]:
# and and or perform a single Boolean evaluation on an entire object,
# while & and | perform multiple Boolean evaluations on the content (the indi‐ vidual bits or bytes) of an object.
# For Boolean NumPy arrays, the latter is nearly always the desired operation.
print np.sum((x > 4) & (x < 7))
print np.sum(~((x <= 5) | (x >= 1)))
In [71]:
x[x < 5]
Out[71]:
In [72]:
rand = np.random.RandomState(42)
x = rand.randint(100, size=10)
print(x)
In [73]:
ind = [3, 7, 4]
print x[ind]
ind = np.array([[3, 7],
[4, 5]])
print x[ind]
In [74]:
X = np.arange(12).reshape((3, 4))
row = np.array([0, 1, 2])
col = np.array([2, 1, 3])
X[row, col]
Out[74]:
In [75]:
X[row[:, np.newaxis], col]
Out[75]:
In [76]:
row[:, np.newaxis] * col
Out[76]:
In [77]:
print X[2, [2, 0, 1]]
print X[1:, [2, 0, 1]]
mask = np.array([1, 0, 1, 0], dtype=bool)
print X[row[:, np.newaxis], mask]
In [78]:
mean = [0, 0]
cov = [[1, 2],
[2, 5]]
X = rand.multivariate_normal(mean, cov, 100)
X.shape
Out[78]:
In [79]:
plt.scatter(X[:, 0], X[:, 1])
Out[79]:
In [81]:
indices = np.random.choice(X.shape[0], 20, replace=False)
selection = X[indices]
plt.scatter(X[:, 0], X[:, 1], alpha=0.3)
plt.scatter(selection[:, 0], selection[:, 1], facecolor='none', s=200, edgecolors='r')
Out[81]:
In [82]:
x = np.zeros(10)
x[[0, 0]] = [4, 6]
print(x) # careful
i=[2,3,3,4,4,4]
x[i] += 1
print(x) # careful
In [83]:
x = np.zeros(10)
np.add.at(x, i, 1)
print(x)
In [84]:
np.random.seed(42)
x = np.random.randn(100)
# compute a histogram by hand
bins = np.linspace(-5, 5, 20)
counts = np.zeros_like(bins)
# find the appropriate bin for each x
i = np.searchsorted(bins, x)
# add 1 to each of these bins
np.add.at(counts, i, 1)
plt.plot(bins, counts, linestyle='steps')
Out[84]:
In [85]:
plt.hist(x, bins, histtype='step')
Out[85]:
In [86]:
x = np.array([2, 1, 4, 3, 5])
print np.sort(x) # return a copy
x.sort() # in-place
print x
In [87]:
x = np.array([2, 1, 4, 3, 5])
i = np.argsort(x)
print i
print x[i]
In [88]:
rand = np.random.RandomState(42)
X = rand.randint(0, 10, (4, 6))
print(X)
# sort each column of X
print(np.sort(X, axis=0))
# sort each row of X
print(np.sort(X, axis=1))
Out[88]:
In [89]:
# Sometimes we’re not interested in sorting the entire array,
# but simply want to find the K smallest values in the array.
# NumPy provides this in the np.partition function.
x = np.array([7, 2, 3, 1, 6, 5, 4])
print np.partition(x, 3)
print np.partition(X, 2, axis=1)
In [90]:
X = rand.rand(10, 2)
dist_sq = np.sum((X[:,np.newaxis,:] - X[np.newaxis,:,:]) ** 2, axis=-1) # pari-wise distance
nearest = np.argsort(dist_sq, axis=1)
K = 2
nearest_partition = np.argpartition(dist_sq, K + 1, axis=1) ##
plt.scatter(X[:, 0], X[:, 1], s=100)
for i in range(X.shape[0]):
for j in nearest_partition[i, :K+1]:
# plot a line from X[i] to X[j]
# use some zip magic to make it happen:
plt.plot(*zip(X[j], X[i]), color='black') #!!
In [3]:
# Use a compound data type for structured arrays
data = np.zeros(4, dtype={'names':('name', 'age', 'weight'),
'formats':('U10', 'i4', 'f8')})
print data, data.dtype
In [4]:
name = ['Alice', 'Bob', 'Cathy', 'Doug']
age = [25, 45, 37, 19]
weight = [55.0, 85.5, 68.0, 61.5]
data['name'] = name
data['age'] = age
data['weight'] = weight
print data
In [5]:
print data['name']
print data[0]
print data[-1]['name']
# Get names where age is under 30
print data[data['age'] < 30]['name']
In [6]:
print np.dtype({'names':('name', 'age', 'weight'), 'formats':('U10', 'i4', 'f8')})
print np.dtype({'names':('name', 'age', 'weight'), 'formats':((np.str_, 10), int, np.float32)})
print np.dtype([('name', 'S10'), ('age', 'i4'), ('weight', 'f8')])
print np.dtype('S10,i4,f8')
In [9]:
tp = np.dtype([('id', 'i8'), ('mat', 'f8', (3, 3))])
X = np.zeros(1, dtype=tp)
print(X[0])
print(X['mat'][0])
In [ ]: