Python for Data Analysis by Wes McKinney (O’Reilly).
《利用Python进行数据分析》
1. Creating ndarrays 生成ndarray
数组生成函数 | Description |
---|---|
array | Convert input data (list, tuple, array, or other sequence type) to an ndarray either by inferring a dtype or explicitly specifying a dtype; copies the input data by default |
asarray | Convert input to ndarray, but do not copy if the input is already an ndarray |
arange | Like the built-in range but returns an ndarray instead of a list |
ones | Produce an array of all 1s with the given shape and dtype; ones_like takes another array and produces a ones array of the same shape and dtype |
ones_like | |
zeros | Like ones and ones_like but producing arrays of 0s instead |
zeros_like | |
empty | Create new arrays by allocating new memory, but do not populate with any values like ones and zeros |
empty_like | |
full | Produce an array of the given shape and dtype with all values set to the indicated “fill value” full_like takes another array and produces a filled array of the same shape and dtype |
full_like | |
eye, identity | Create a square N × N identity matrix (1s on the diagonal and 0s elsewhere) |
1 | data1 = [6, 7.5, 8, 0, 1] #列表 |
1 | np.zeros(10) |
1 | np.arange(15) |
2. Transposing Arrays and Swapping Axes 数组转置和换轴
Arrays have the transpose method and also the special T attribute:
1 | In [126]: arr = np.arange(15).reshape((3, 5)) |
3. 通用函数:Universal Functions: Fast Element-Wise Array Functions
一元通用函数 | Description |
---|---|
abs, fabs | Compute the absolute value element-wise for integer, floating-point, or complex values |
sqrt | Compute the square root of each element (equivalent to arr ** 0.5) |
square | Compute the square of each element (equivalent to arr ** 2) |
exp | Compute the exponent ex of each element |
log, log10, log2, log1p | Natural logarithm (base e), log base 10, log base 2, and log(1 + x), respectively |
sign | Compute the sign of each element: 1 (positive), 0 (zero), or –1 (negative) |
ceil | Compute the ceiling of each element (i.e., the smallest integer greater than or equal to that number) |
floor | Compute the floor of each element (i.e., the largest integer less than or equal to each element) |
rint | Round elements to the nearest integer, preserving the dtype |
modf | Return fractional and integral parts of array as a separate array |
isnan | Return boolean array indicating whether each value is NaN (Not a Number) |
isfinite, isinf | Return boolean array indicating whether each element is finite (non-inf, non-NaN) or infinite, respectively |
cos, cosh, sin, sinh, tan, tanh | Regular and hyperbolic trigonometric functions |
arccos, arccosh, arcsin, arcsinh, arctan, arctanh | Inverse trigonometric functions |
二元通用函数 | Description |
---|---|
add | Add corresponding elements in arrays |
subtract | Subtract elements in second array from first array |
multiply | Multiply array elements |
divide, floor_divide | Divide or floor divide (truncating the remainder) |
power | Raise elements in first array to powers indicated in second array |
maximum, fmax | Element-wise maximum; fmax ignores NaN |
minimum, fmin | Element-wise minimum; fmin ignores NaN |
mod | Element-wise modulus (remainder of division) |
copysign | Copy sign of values in second argument to values in first argument |
1 | In [137]: arr = np.arange(10) |
4.条件逻辑作为数组操作 Conditional Logic as Array Operations
1 | #np.where(cond, xarr, yarr), xarr和yarr可以是标量也可以是数组 |
5. 数学和统计方法 Mathematical and Statistical Methods
方法 | Description |
---|---|
sum | Sum of all the elements in the array or along an axis; zero-length arrays have sum 0 |
mean | Arithmetic mean; zero-length arrays have NaN mean |
std, var | Standard deviation and variance, respectively, with optional degrees of freedom adjustment (default denominator n) |
min, max | Minimum and maximum) |
argmin, argmax | Indices of minimum and maximum elements, respectively |
cumsum | Cumulative sum of elements starting from 0 |
cumprod | Cumulative product of elements starting from 1 |
1 | In [177]: arr = np.random.randn(5, 4) |
6. 布尔值数组方法 Methods for Boolean Arrays
method any tests whether one or more values in an array is True, while all checks if every value is True:
1 | In [192]: bools = np.array([False, False, True, False]) |
7. 排序 Sorting
1 | In [195]: arr = np.random.randn(6) |
1 | In [199]: arr = np.random.randn(5, 3) |
8. 唯一值和其他集合逻辑 Unique and Other Set Logic
1 | In [206]: names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe']) |
数组集合操作 | Description |
---|---|
unique(x) | Compute the sorted, unique elements in x |
intersect1d(x,y) | Compute the sorted, common elements in x and y |
union1d(x, y) | Compute the sorted union of elements |
in1d(x, y) | Compute a boolean array indicating whether each element ofxis contained iny |
setdiff1d(x, y) | Set difference, elements inxthat are not iny |
setxor1d(x, y) | Set symmetric differences; elements that are in either of the arrays, but not both |
线性代数 Linear Algebra
numpy.linalg模块
常用numpy.linalg函数 | Description |
---|---|
diag | Return the diagonal (or off-diagonal) elements of a square matrix as a 1D array, or convert a 1D array into a square matrix with zeros on the off-diagonal |
dot | Matrix multiplication |
trace | Compute the sum of the diagonal elements |
det | Compute the matrix determinant |
eig | Compute the eigenvalues and eigenvectors of a square matrix |
inv | Compute the inverse of a square matrix |
pinv | Compute the Moore-Penrose pseudo-inverse of a matrix |
qr | Compute the QR decomposition |
svd | Compute the singular value decomposition (SVD) |
solve | Solve the linear system Ax = b for x, where A is a square matrix |
lstsq | Compute the least-squares solution to Ax = b |