Python numpy 转置、逆、去掉一列、按列取出、矩阵拼接、矩阵排序、矩阵相等、np.where,一维转二维
转置
例如:
from numpy import *import numpy as np>>> c = [[1,2,5],[4,5,8]]>>> print c[[1, 2, 5], [4, 5, 8]]
先mat,然后转置T
>>> print mat(c).T[[1 4][2 5][5 8]]
或者:先转为array,然后T(最好不用这个)
>>> d = np.array(c)>>> print d[[1 2 5] [4 5 8]]>>> print d.T[[1 4][2 5][5 8]]
逆:
用mat以后,然后用I
>>> a = [[1,3,4],[2,5,9],[4,9,8]]>>> print mat(a).I[[-3.72727273 1.09090909 0.63636364][ 1.81818182 -0.72727273 -0.09090909][-0.18181818 0.27272727 -0.09090909]]
但是这种方法不行:先转为array,然后再I
>>> print np.array(a).ITraceback (most recent call last):File "<stdin>", line 1, in <module>AttributeError: "numpy.ndarray" object has no attribute "I"
numpy的列操作:
numpy类型去掉一列(例子中为倒数第一列):
cut_data = np.delete(mydata, -1, axis=1)
numpy按类标取出:
dataone = list(d for d in raw_data[:] if d[mark_line] == 0)datatwo = list(d for d in raw_data[:] if d[mark_line] == 1)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))
>>> a = [[1,2,3],[4,5,6]]>>> b = [[11,22,33],[44,55,66]]>>> a_a = mat(a)>>> b_b = mat(b)>>> print a_a[[1 2 3][4 5 6]]>>> print b_b[[11 22 33][44 55 66]]>>> c = np.hstack((a,b))>>> d = np.vstack((a,b))>>> print c[[ 1 2 3 11 22 33][ 4 5 6 44 55 66]]>>> print d[[ 1 2 3][ 4 5 6][11 22 33][44 55 66]]
矩阵排序:
list也可以这样做,只是返回值仍然是一个排好序的list
a = [[4,1,5],[1,2,5]]>>> c = sorted(a,key = operator.itemgetter(1),reverse = True)>>> print c[[1, 2, 5], [4, 1, 5]]
import operatora = [[4,1,5],[1,2,5]]>>> b = np.array(a)>>> print b[[4 1 5][1 2 5]]## 注意必须在b前面也加上c变量用于记录位置,否则的话b是不变的>>> c = sorted(b,key = operator.itemgetter(1),reverse = True) #按照第二列进行排序,并按高到低排序[array([1, 2, 5]), array([4, 1, 5])]>>> sorted(b,key = operator.itemgetter(0),reverse = True)[array([4, 1, 5]), array([1, 2, 5])]>>> sorted(b,key = operator.itemgetter(0),reverse = True)[array([4, 1, 5]), array([1, 2, 5])]>>> c = sorted(a ,key = operator.itemgetter(1),reverse = True) # list 也可以排序,但是里面的类型不同>>> print c[[1, 2, 5], [4, 1, 5]]
寻找位置:
>>> a = [[1,2,3],[4,5,6],[7,8,9]]>>> b = np.array(a)>>> print b[[1 2 3][4 5 6][7 8 9]]>>> np.where(b = 5)Traceback (most recent call last):File "<stdin>", line 1, in <module>TypeError: where() takes no keyword arguments>>> np.where(5)(array([0]),)>>> np.where(b == 5)(array([1]), array([1]))>>> np.where(b == 6)(array([1]), array([2]))
判断两个矩阵是不是相等:
注意不能直接用 == 号
>>> a = [[1,2,3],[4,5,6],[7,8,9]]>>> b = [[1,2,3],[4,5,6],[7,8,9]]>>> c = np.array(a)>>> d = np.array(b)>>> print c[[1 2 3][4 5 6][7 8 9]]>>> print d[[1 2 3][4 5 6][7 8 9]]>>> if c == d:... print "yes"...Traceback (most recent call last):File "<stdin>", line 1, in <module>ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()>>> if (c == d).all():... print "yes"...yes
若还是报错的话,则使用np.close(a, b):
>>> a = [array([ 4.90312812, 0.31002876, -3.41898514]), array([ 16.02316243, 1.51557803, 82.28424555])]>>> b = [array([ 1.57286264, 2.1289384 , -1.57890685]), array([ 10.22050379, 6.02365441, 48.91208021])]>>> a == bTraceback (most recent call last):File "<input>", line 1, in <module>ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()>>> (a == b).all()Traceback (most recent call last):File "<input>", line 1, in <module>ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()>>> np.allclose(a,b)False
矩阵的copy问题:
当用copy()的时候相当于另外开辟了一个空间存储这个变量与copy过来的值,否则的话仍然在以前变量的基础上修改!
>>> a = [[1,2,3],[4,5,6],[7,8,9]]>>> b = np.array(a)>>> print b[[1 2 3][4 5 6][7 8 9]]>>> c = b.copy()>>> c[1,1] = 100>>> print c[[ 1 2 3][ 4 100 6][ 7 8 9]]>>> print b[[1 2 3][4 5 6][7 8 9]]>>> d = b>>> d[1,1] = 99>>> print d[[ 1 2 3][ 4 99 6][ 7 8 9]]>>> print b[[ 1 2 3][ 4 99 6][ 7 8 9]]
从numpy中取出数据,可以传入list
In[17]: a = [1,4,5,6,9,6,7]In[18]: b = [0,1,5]In[20]: a = np.array(a)In[21]: a[b]Out[21]:array([1, 4, 6])
numpy一维转二维
例如:对于二维数组而言,(3,1)与(3,)是不同的
>>> a = [[1],[2],[3]]>>> a = np.array(a)
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
