
import numpy as np
np.random.seed(0)
def compute_reciprocals(values):
output = np.empty(len(values))
for i in range(len(values)):
output[i] = 1.0 / values[i]
return output
values = np.random.randint(1, 10, size=5)
compute_reciprocals(values)%timeit magic (discussed in Profiling and Timing Code):
big_array = np.random.randint(1, 100, size=1000000)
%timeit compute_reciprocals(big_array)
print(compute_reciprocals(values))
print(1.0 / values)
%timeit (1.0 / big_array)
np.arange(5) / np.arange(1, 6)
x = np.arange(9).reshape((3, 3))
2 ** x
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** operator for exponentiation, and a % operator for modulus:
print("-x = ", -x)
print("x ** 2 = ", x ** 2)
print("x % 2 = ", x % 2)
-(0.5*x + 1) ** 2+ operator is a wrapper for the add function:
np.add(x, 2)| Operator | Equivalent ufunc | Description |
|---|---|---|
+ | np.add | Addition (e.g., 1 + 1 = 2) |
- | np.subtract | Subtraction (e.g., 3 - 2 = 1) |
- | np.negative | Unary negation (e.g., -2) |
* | np.multiply | Multiplication (e.g., 2 * 3 = 6) |
/ | np.divide | Division (e.g., 3 / 2 = 1.5) |
// | np.floor_divide | Floor division (e.g., 3 // 2 = 1) |
** | np.power | Exponentiation (e.g., 2 ** 3 = 8) |
% | np.mod | Modulus/remainder (e.g., 9 % 4 = 1) |

x = np.array([-2, -1, 0, 1, 2])
abs(x)np.absolute, which is also available under the alias np.abs:
np.absolute(x)
np.abs(x)
x = np.array([3 - 4j, 4 - 3j, 2 + 0j, 0 + 1j])
np.abs(x)
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))
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))np.log gives the natural logarithm; if you prefer to compute the base-2 logarithm or the base-10 logarithm, these are available as well:
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))x is very small, these functions give more precise values than if the raw np.log or np.exp were to be used.scipy.special.
If you want to compute some obscure mathematical function on your data, chances are it is implemented in scipy.special.
There are far too many functions to list them all, but the following snippet shows a couple that might come up in a statistics context:
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))scipy.special.
Because the documentation of these packages is available online, a web search along the lines of "gamma function python" will generally find the relevant information.out argument of the function:
x = np.arange(5)
y = np.empty(5)
np.multiply(x, 10, out=y)
print(y)
y = np.zeros(10)
np.power(2, x, out=y[::2])
print(y)y[::2] = 2 ** x, this would have resulted in the creation of a temporary array to hold the results of 2 ** x, followed by a second operation copying those values into the y array.
This doesn't make much of a difference for such a small computation, but for very large arrays the memory savings from careful use of the out argument can be significant.reduce method of any ufunc.
A reduce repeatedly applies a given operation to the elements of an array until only a single result remains.reduce on the add ufunc returns the sum of all elements in the array:
x = np.arange(1, 6)
np.add.reduce(x)reduce on the multiply ufunc results in the product of all array elements:
np.multiply.reduce(x)accumulate:
np.add.accumulate(x)
np.multiply.accumulate(x)np.sum, np.prod, np.cumsum, np.cumprod), which we'll explore in Aggregations: Min, Max, and Everything In Between.outer method.
This allows you, in one line, to do things like create a multiplication table:
x = np.arange(1, 6)
np.multiply.outer(x, x)