numpy矩阵的常用用法
基本操作 >>> m= np.mat([1,2,3]) #创建矩阵 >>> m matrix([[1, 2, 3]]) >>> m[0] #取一行 matrix([[1, 2, 3]]) >>> m[0,1] #第一行,第2个数据 2 >>> m[0][1] #注意不能像数组那样取值了 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib64/python2.7/site-packages/numpy/matrixlib/defmatrix.py", line 305, in __getitem__ out = N.ndarray.__getitem__(self, index) IndexError: index 1 is out of bounds for axis 0 with size 1 #将Python的列表转换成NumPy的矩阵 >>> list=[1,2,3] >>> mat(list) matrix([[1, 2, 3]]) #Numpy dnarray转换成Numpy矩阵 >>> n = np.array([1,2,3]) >>> n array([1, 2, 3]) >>> np.mat(n) matrix([[1, 2, 3]]) #排序 >