Numpy

100 Numpy exercises

https://github.com/rougier/numpy-100/blob/master/100%20Numpy%20exercises.md

identity matrix

https://en.wikipedia.org/wiki/Identity_matrix

In linear algebra, the identity matrix, or sometimes ambiguously called a unit matrix, of size n is the n × n square matrix with ones on the main diagonal and zeros elsewhere.
# Create a 3x3 identity matrix
import numpy as np
np.eye(3)

Normalize a 5x5 random matrix

Z = np.random.random((5,5))
Zmax, Zmin = Z.max(), Z.min()
Z = (Z - Zmin)/(Zmax - Zmin)

Create a custom dtype that describes a color as four unisgned bytes (RGBA)

color = np.dtype([("r", np.ubyte, 1),
                  ("g", np.ubyte, 1),
                  ("b", np.ubyte, 1),
                  ("a", np.ubyte, 1)])

How to round away from zero a float array ?

QUESTION: 不知道这个题目是什么意思

# Author: Charles R Harris

Z = np.random.uniform(-10,+10,10)
print (np.copysign(np.ceil(np.abs(Z)), Z))

How to sum a small array faster than np.sum?

QUESTION: 为什么 reduce 比直接用 sum 快

# Author: Evgeni Burovski

Z = np.arange(10)
np.add.reduce(Z)

Consider two random array A anb B, check if they are equal

QUESTION: 没懂这两个的区别

A = np.random.randint(0,2,5)
B = np.random.randint(0,2,5)
# Assuming identical shape of the arrays and a tolerance for the comparison of values
equal = np.allclose(A,B)
print(equal)

# Checking both the shape and the element values, no tolerance (values have to be exactly equal)
equal = np.array_equal(A,B)
print(equal)

Create a structured array with x and y coordinates covering the [0,1]x[0,1] area

QUESTION: 不是很懂这个题目的意思

Z = np.zeros((5,5), [('x',float),('y',float)])
Z['x'], Z['y'] = np.meshgrid(np.linspace(0,1,5),
                             np.linspace(0,1,5))
print(Z)

Given two arrays, X and Y, construct the Cauchy matrix C (Cij =1/(xi - yj))

QUESTION: Cauchy matrix 是什么

# Author: Evgeni Burovski

X = np.arange(8)
Y = X + 0.5
C = 1.0 / np.subtract.outer(X, Y)
print(np.linalg.det(C))

Consider a random vector with shape (100,2) representing coordinates, find point by point distances

QUESTION: 不懂题目的意思 QUESTION: X - X.T 是怎么计算的

Z = np.random.random((10,2))
X,Y = np.atleast_2d(Z[:,0]), np.atleast_2d(Z[:,1])
D = np.sqrt( (X-X.T)**2 + (Y-Y.T)**2)
print(D)

# Much faster with scipy
import scipy
# Thanks Gavin Heverly-Coulson (#issue 1)
import scipy.spatial

Z = np.random.random((10,2))
D = scipy.spatial.distance.cdist(Z,Z)
print(D)

Generate a generic 2D Gaussian-like array

QUESTION: 不懂什么叫 Gaussian-like array

X, Y = np.meshgrid(np.linspace(-1,1,10), np.linspace(-1,1,10))
D = np.sqrt(X*X+Y*Y)
sigma, mu = 1.0, 0.0
G = np.exp(-( (D-mu)**2 / ( 2.0 * sigma**2 ) ) )
print(G)

Subtract the mean of each row of a matrix

感觉统计 SLA 会用到

# Author: Warren Weckesser

X = np.random.rand(5, 10)

# Recent versions of numpy
Y = X - X.mean(axis=1, keepdims=True)

# Older versions of numpy
Y = X - X.mean(axis=1).reshape(-1, 1)

Considering two arrays with shape (1,3) and (3,1), how to compute their sum using an iterator?

QUESTION: 没懂这个计算方法

A = np.arange(3).reshape(3,1)
B = np.arange(3).reshape(1,3)
it = np.nditer([A,B,None])
for x,y,z in it: z[...] = x + y
print(it.operands[2])

Considering a one-dimensional vector D, how to compute means of subsets of D using a vector S of same size describing subset indices?

同时算一堆指标的平均值,棒棒哒。

# Author: Jaime Fernández del Río

D = np.random.uniform(0,1,100)
S = np.random.randint(0,10,100)
D_sums = np.bincount(S, weights=D)
D_counts = np.bincount(S)
D_means = D_sums / D_counts
print(D_means)

How to get the diagonal of a dot product?

QUESTION: 不懂这里面的数学逻辑

# Author: Mathieu Blondel

A = np.random.uniform(0,1,(5,5))
B = np.random.uniform(0,1,(5,5))

# Slow version
np.diag(np.dot(A, B))

# Fast version
np.sum(A * B.T, axis=1)

# Faster version
np.einsum("ij,ji->i", A, B)

Consider the vector [1, 2, 3, 4, 5], how to build a new vector with 3 consecutive zeros interleaved between each value?

QUESTION: Z0[::nz+1] = z 是什么操作?

# Author: Warren Weckesser

Z = np.array([1,2,3,4,5])
nz = 3
Z0 = np.zeros(len(Z) + (len(Z)-1)*(nz))
Z0[::nz+1] = Z
print(Z0)

Consider an array of dimension (5,5,3), how to mulitply it by an array with dimensions (5,5)?

QUESTION: 没懂 A * B[:,:,None] 做的是什么运算

A = np.ones((5,5,3))
B = 2*np.ones((5,5))
print(A * B[:,:,None])

How to swap two rows of an array?

A[(0, 1)] 是一个点, A[[0,1]] 是两行 LOL

# Author: Eelco Hoogendoorn

A = np.arange(25).reshape(5,5)
A[[0,1]] = A[[1,0]]
print(A)

Inf & NaN

http://astrostatistics.psu.edu/datasets/R/html/base/html/is.finite.html

Inf and -Inf are positive and negative “infinity” whereas NaN means “Not a Number”.

Reference

numpy.bincount(x, weights=None, minlength=None)

Count number of occurrences of each value in array of non-negative ints.

A possible use of bincount is to perform sums over variable-size chunks of an array, using the weights keyword.

>>> w = np.array([0.3, 0.5, 0.2, 0.7, 1., -0.6]) # weights
>>> x = np.array([0, 1, 1, 2, 2, 2])
    >>> np.bincount(x,  weights=w)
    array([ 0.3,  0.7,  1.1])

Theano - NumPy refresher

http://deeplearning.net/software/theano/tutorial/numpy.html

To remember this, keep in mind that we read left-to-right, top-to-bottom, so each thing that is contiguous is a row. That is, there are 3 rows and 2 columns.

Numpy does broadcasting of arrays of different shapes during arithmetic operations. What this means in general is that the smaller array (or scalar) is broadcasted across the larger array so that they have compatible shapes. The example below shows an instance of broadcastaing:

>>> a = numpy.asarray([1.0, 2.0, 3.0])
>>> b = 2.0
>>> a * b
array([ 2.,  4.,  6.])

Reference

numpy.ndarray.shape
Tuple of array dimensions.
numpy.tile(A, reps)
Construct an array by repeating A the number of times given by reps.
numpy.ndarray.sum(axis=None, dtype=None, out=None, keepdims=False)
Return the sum of the array elements over the given axis.