Python numpy 转置、逆、去掉一列、按列取出、矩阵拼接、矩阵排序、矩阵相等、np.where,一维转二维
2017-11-01 13:34
741 查看
转置
例如:
先mat,然后转置T
或者:先转为array,然后T(最好不用这个)
逆:
用mat以后,然后用I
但是这种方法不行:先转为array,然后再I
numpy的列操作:
numpy类型去掉一列(例子中为倒数第一列):
numpy按类标取出:
矩阵拼接:
list先转化为list形式,然后用mat转为矩阵,再用 c
=
np.hstack((a,b))
d = np.vstack((a,b))
矩阵排序:
list也可以这样做,只是返回值仍然是一个排好序的list
寻找位置:
判断两个矩阵是不是相等:
注意不能直接用 == 号
若还是报错的话,则使用np.close(a, b):
矩阵的copy问题:
当用copy()的时候相当于另外开辟了一个空间存储这个变量与copy过来的值,否则的话仍然在以前变量的基础上修改!
从numpy中取出数据,可以传入list
numpy一维转二维
例如:对于二维数组而言,(3,1)与(3,)是不同的
矩阵包
附录:
例如:
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).I
Traceback (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 operator
a = [[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 == b
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()
>>> (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)
>>> a
array([[1],
[2],
[3]])
>>> np.shape(a)
(3, 1)
>>> b = a[:,0]
>>> b
array([1, 2, 3])
>>> np.shape(a=b)
(3,)
矩阵包
附录:
>>> print a
[[1, 3, 4], [2, 5, 9], [4, 9, 8]]
>>> print type(a)
<type 'list'>
>>> print np.array(a)
[[1 3 4]
[2 5 9]
[4 9 8]]
>>> print mat(a)
[[1 3 4]
[2 5 9]
[4 9 8]]
>>> print type(a[0])
<type 'list'>
>>> print type(np.array(a)[0])
<type 'numpy.ndarray'>
>>> print type(mat(a)[0])
<class 'numpy.matrixlib.defmatrix.matrix'>
>>> print np.array(a)[0,0]
1
>>> print mat(a)[0,0]
相关文章推荐
- python numpy 矩阵转置
- [Python]使用Numpy对矩阵进行转置
- python学习笔记--二维矩阵的转置
- Numpy 将二维图像矩阵转换为一维向量的方法
- 【python】【numpy】【给多维矩阵添加一列】
- Python numpy 提取矩阵的某一行或某一列的实例
- numpy线性代数基础 - Python和MATLAB矩阵处理的不同
- python numpy 使用笔记 矩阵操作
- 【python】numpy数组中按照特征值对特征向量进行排序
- python基础练习(三)—— numpy的矩阵基本操作
- python 对矩阵进行复制操作 np.repeat 与 np.tile区别
- python 有关矩阵行列的存取 np.array
- 用numpy把一个矩阵的一行或一列删除,再把剩下的拼在一起
- Python-按字典dict的键排序,并取出相应的键值放于list中
- python 矩阵转置transpose
- python二维列表一维列表的互相转换实例
- Python实遍历二维数组题目一:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
- Python之numpy教程(五):筛选、排序、集合函数、读取存入数据
- numpy: np.where
- python pandas 如何找到NaN、缺失值或者某些元素的索引名称以及位置,np.where的使用