赞
踩
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
|
使用numpy.set_printoptions可以设置numpy变量的打印格式
在ipython环境下,使用help(numpy.set_printoptions)查询使用帮助和示例
加法和减法操作要求操作双方的维数信息一致,均为M*N为数组方可正确执行操作。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
多维数组操作过程中的类型转换
When operating with arrays of different types, the type of the resulting array corresponds to the more general or precise one (a behavior known as upcasting)
即操作不同类型的多维数组时,结果自动转换为精度更高类型的数组,即upcasting
1 2 3 4 |
|
1 2 3 4 5 6 7 8 9 10 |
|
多维数组的一元操作,如求和、求最小值、最大值等
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
universal functions
1 2 3 4 5 |
|
其他的ufunc函数包括:
all, any, apply_along_axis, argmax, argmin, argsort, average, bincount, ceil, clip, conj, corrcoef, cov, cross, cumprod, cumsum, diff, dot, floor,inner, lexsort, max, maximum, mean, median, min, minimum, nonzero, outer, prod, re, round, sort, std, sum, trace, transpose, var,vdot, vectorize, where
1 2 3 4 5 6 |
|
1 2 3 4 5 6 7 8 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
详查以下函数:
ndarray.shape, reshape, resize, ravel
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
|
详细使用请查询以下函数:
hstack, vstack, column_stack, concatenate, c_, r_
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
实现类似功能的函数包括:
hsplit,vsplit,array_split
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
|
numpy基本函数和方法一览
Array Creation
arange, array, copy, empty, empty_like, eye, fromfile, fromfunction, identity, linspace, logspace, mgrid, ogrid, ones, ones_like, r, zeros,zeros_like
Conversions
ndarray.astype, atleast_1d, atleast_2d, atleast_3d, mat
Manipulations
array_split, column_stack, concatenate, diagonal, dsplit, dstack, hsplit, hstack, ndarray.item, newaxis, ravel, repeat, reshape, resize,squeeze, swapaxes, take, transpose, vsplit, vstack
Questionsall, any, nonzero, where
Ordering
argmax, argmin, argsort, max, min, ptp, searchsorted, sort
Operations
choose, compress, cumprod, cumsum, inner, ndarray.fill, imag, prod, put, putmask, real, sum
Basic Statistics
Basic Linear Algebra
cross, dot, outer, linalg.svd, vdot
完整的函数和方法一览表链接:
https://docs.scipy.org/doc/numpy-dev/reference/routines.html#routines
- a = np.array([1,2,3])
- a1 = np.mat(a)
- 输出:
- matrix([[1, 2, 3]])
- type(a1)
- 输出:
- numpy.matrixlib.defmatrix.matrix
- a1.shape
- 输出:
- (1L, 3L)
- a.shape
- 输出:
- (3L,)
-
-
- b=np.matrix([1,2,3])
- 输出:
- matrix([[1, 2, 3]])
-
-
-
- from numpy import *
- data1 = mat(zeros((3,3)))
- data2 = mat(ones((2,4)))
- data3 = mat(random.rand(2,2))
- data4 = mat(random.randint(2,8,size=(2,5)))
- data5 = mat(eye(2,2,dtype=int))
- a1 = mat([1,2])
- a2 = mat([[1],[2]])
- a3 = a1 * a2
- print(a3)
- 输出:
- matrix([[5]])
-
- print(a1*2)
- 输出:
- matrix([[2, 4]])
-
- a1 = mat(eye(2,2)*0.5)
- print(a1.I)
- 输出:
- matrix([[ 2., 0.],
- [ 0., 2.]])
-
-
- a1 = mat([[1,2],[2,3],[4,2]])
- a1.sum(axis=0)
- 输出:
- matrix([[7, 7]])
- a1.sum(axis=1)
- 输出:
- matrix([[3],
- [5],
- [6]])
- a1.max() # 求矩阵元素最大值
- 输出:
- 4
- a1.min() # 求矩阵元素最小值
- 输出:
- 1
-
- np.max(a1,0) # 求矩阵每列元素最大值
- 输出:
- matrix([[4, 3]])
- np.max(a1,1) # 求矩阵每行元素最大值
- 输出:
- matrix([[2],
- [3],
- [4]])
-
-
- a = mat(ones((2,2)))
- b = mat(eye((2)))
- c = hstack((a,b))
- 输出:
- matrix([[ 1., 1., 1., 0.],
- [ 1., 1., 0., 1.]])
- d = vstack((a,b))
- 输出:
- matrix([[ 1., 1.],
- [ 1., 1.],
- [ 1., 0.],
- [ 0., 1.]])
- aa = [[1,2],[3,4],[5,6]]
- bb = array(aa)
- cc = mat(bb)
-
- cc.getA() # 矩阵转换为数组
- cc.tolist() # 矩阵转换为列表
- bb.tolist() # 数组转换为列表
-
-
- # 当列表为一维时,情况有点特殊
- aa = [1,2,3,4]
- bb = array(aa)
- 输出:
- array([1, 2, 3, 4])
- cc = mat(bb)
- 输出:
- matrix([[1, 2, 3, 4]])
-
- cc.tolist()
- 输出:
- [[1, 2, 3, 4]]
-
- bb.tolist()
- 输出:
- [1, 2, 3, 4]
-
- cc.tolist()[0]
- 输出:
- [1, 2, 3, 4]
参考博客:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。