Python numpy 转置、逆、去掉一列、按列取出、矩阵拼接、矩阵排序、矩阵相等、np.where,一维转二维

转置

例如:

  
  
  1. from numpy import *
  2. import numpy as np
  3. >>> c = [[1,2,5],[4,5,8]]
  4. >>> print c
  5. [[1, 2, 5], [4, 5, 8]]

先mat,然后转置T

  
  
  1. >>> print mat(c).T
  2. [[1 4]
  3. [2 5]
  4. [5 8]]

或者:先转为array,然后T(最好不用这个)

  
  
  1. >>> d = np.array(c)
  2. >>> print d
  3. [[1 2 5] [4 5 8]]
  4. >>> print d.T
  5. [[1 4]
  6. [2 5]
  7. [5 8]]


逆:

用mat以后,然后用I

  
  
  1. >>> a = [[1,3,4],[2,5,9],[4,9,8]]
  2. >>> print mat(a).I
  3. [[-3.72727273  1.09090909  0.63636364]
  4. [ 1.81818182 -0.72727273 -0.09090909]
  5. [-0.18181818  0.27272727 -0.09090909]]

但是这种方法不行:先转为array,然后再I

  
  
  1. >>> print np.array(a).I
  2. Traceback (most recent call last):
  3.  File "<stdin>", line 1, in <module>
  4. AttributeError: 'numpy.ndarray' object has no attribute 'I'


numpy的列操作:

numpy类型去掉一列(例子中为倒数第一列):

   
   
  1. cut_data = np.delete(mydata, -1, axis=1)

numpy按类标取出:

   
   
  1. dataone = list(d for d in raw_data[:] if d[mark_line] == 0)
  2. datatwo = list(d for d in raw_data[:] if d[mark_line] == 1)
  3. datathree = list(d for d in raw_data[:] if d[mark_line] == 2)

矩阵拼接:

list先转化为list形式,然后用mat转为矩阵,再用 c = np.hstack((a,b))   d = np.vstack((a,b))

  
  
  1. >>> a = [[1,2,3],[4,5,6]]
  2. >>> b = [[11,22,33],[44,55,66]]
  3. >>> a_a = mat(a)
  4. >>> b_b = mat(b)
  5. >>> print a_a
  6. [[1 2 3]
  7. [4 5 6]]
  8. >>> print b_b
  9. [[11 22 33]
  10. [44 55 66]]
  11. >>> c = np.hstack((a,b))
  12. >>> d = np.vstack((a,b))
  13. >>> print c
  14. [[ 1  2  3 11 22 33]
  15. [ 4  5  6 44 55 66]]
  16. >>> print d
  17. [[ 1  2  3]
  18. [ 4  5  6]
  19. [11 22 33]
  20. [44 55 66]]


矩阵排序:

list也可以这样做,只是返回值仍然是一个排好序的list

  
  
  1. a = [[4,1,5],[1,2,5]]
  2. >>> c = sorted(a,key = operator.itemgetter(1),reverse = True)
  3. >>> print c
  4. [[1, 2, 5], [4, 1, 5]]

  
  
  1. import operator
  2. a = [[4,1,5],[1,2,5]]
  3. >>> b = np.array(a)
  4. >>> print b
  5. [[4 1 5]
  6. [1 2 5]]
  7. ## 注意必须在b前面也加上c变量用于记录位置,否则的话b是不变的
  8. >>> c = sorted(b,key = operator.itemgetter(1),reverse = True)  #按照第二列进行排序,并按高到低排序
  9. [array([1, 2, 5]), array([4, 1, 5])]
  10. >>> sorted(b,key = operator.itemgetter(0),reverse = True)
  11. [array([4, 1, 5]), array([1, 2, 5])]
  12. >>> sorted(b,key = operator.itemgetter(0),reverse = True)
  13. [array([4, 1, 5]), array([1, 2, 5])]
  14. >>> c = sorted(a ,key = operator.itemgetter(1),reverse = True)        # list 也可以排序,但是里面的类型不同
  15. >>> print c
  16. [[1, 2, 5], [4, 1, 5]]


寻找位置:

  
  
  1. >>> a = [[1,2,3],[4,5,6],[7,8,9]]
  2. >>> b = np.array(a)
  3. >>> print b
  4. [[1 2 3]
  5. [4 5 6]
  6. [7 8 9]]
  7. >>> np.where(b = 5)
  8. Traceback (most recent call last):
  9.  File "<stdin>", line 1, in <module>
  10. TypeError: where() takes no keyword arguments
  11. >>> np.where(5)
  12. (array([0]),)
  13. >>> np.where(b == 5)
  14. (array([1]), array([1]))
  15. >>> np.where(b == 6)
  16. (array([1]), array([2]))


判断两个矩阵是不是相等:

注意不能直接用 == 号

  
  
  1. >>> a = [[1,2,3],[4,5,6],[7,8,9]]
  2. >>> b = [[1,2,3],[4,5,6],[7,8,9]]
  3. >>> c = np.array(a)
  4. >>> d = np.array(b)
  5. >>> print c
  6. [[1 2 3]
  7. [4 5 6]
  8. [7 8 9]]
  9. >>> print d
  10. [[1 2 3]
  11. [4 5 6]
  12. [7 8 9]]
  13. >>> if c == d:
  14. ...     print 'yes'
  15. ...
  16. Traceback (most recent call last):
  17.  File "<stdin>", line 1, in <module>
  18. ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
  19. >>> if (c == d).all():
  20. ...     print 'yes'
  21. ...
  22. yes

若还是报错的话,则使用np.close(a, b):

  
  
  1. >>> a = [array([ 4.90312812,  0.31002876, -3.41898514]), array([ 16.02316243,   1.51557803,  82.28424555])]
  2. >>> b = [array([ 1.57286264,  2.1289384 , -1.57890685]), array([ 10.22050379,   6.02365441,  48.91208021])]
  3. >>> a == b
  4. Traceback (most recent call last):
  5.  File "<input>", line 1, in <module>
  6. ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
  7. >>> (a == b).all()
  8. Traceback (most recent call last):
  9.  File "<input>", line 1, in <module>
  10. ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
  11. >>> np.allclose(a,b)
  12. False



矩阵的copy问题:

当用copy()的时候相当于另外开辟了一个空间存储这个变量与copy过来的值,否则的话仍然在以前变量的基础上修改!

  
  
  1. >>> a = [[1,2,3],[4,5,6],[7,8,9]]
  2. >>> b = np.array(a)
  3. >>> print b
  4. [[1 2 3]
  5. [4 5 6]
  6. [7 8 9]]
  7. >>> c = b.copy()
  8. >>> c[1,1] = 100
  9. >>> print c
  10. [[  1   2   3]
  11. [  4 100   6]
  12. [  7   8   9]]
  13. >>> print b
  14. [[1 2 3]
  15. [4 5 6]
  16. [7 8 9]]
  17. >>> d = b
  18. >>> d[1,1] = 99
  19. >>> print d
  20. [[ 1  2  3]
  21. [ 4 99  6]
  22. [ 7  8  9]]
  23. >>> print b
  24. [[ 1  2  3]
  25. [ 4 99  6]
  26. [ 7  8  9]]


从numpy中取出数据,可以传入list

  
  
  1. In[17]: a = [1,4,5,6,9,6,7]
  2. In[18]: b = [0,1,5]
  3. In[20]: a = np.array(a)
  4. In[21]: a[b]
  5. Out[21]:
  6. array([1, 4, 6])


numpy一维转二维

例如:对于二维数组而言,(3,1)与(3,)是不同的

  
  
  1. >>> a = [[1],[2],[3]]
  2. >>> a = np.array(a)
  3. >>> a
  4. array([[1],
  5.       [2],
  6.       [3]])
  7. >>> np.shape(a)
  8. (3, 1)
  9. >>> b = a[:,0]
  10. >>> b
  11. array([1, 2, 3])
  12. >>> np.shape(a=b)
  13. (3,)


矩阵包


附录:

  
  
  1. >>> print a
  2. [[1, 3, 4], [2, 5, 9], [4, 9, 8]]
  3. >>> print type(a)
  4. <type 'list'>
  5. >>> print np.array(a)
  6. [[1 3 4]
  7. [2 5 9]
  8. [4 9 8]]
  9. >>> print mat(a)
  10. [[1 3 4]
  11. [2 5 9]
  12. [4 9 8]]
  13. >>> print type(a[0])
  14. <type 'list'>
  15. >>> print type(np.array(a)[0])
  16. <type 'numpy.ndarray'>
  17. >>> print type(mat(a)[0])
  18. <class 'numpy.matrixlib.defmatrix.matrix'>
  19. >>> print np.array(a)[0,0]
  20. 1
  21. >>> print mat(a)[0,0]

猜你喜欢

转载自blog.csdn.net/shutuozhui7077/article/details/78413135