Python numpy 转置、逆、去掉一列、按列取出、矩阵拼接、矩阵排序、矩阵相等、np.where,一维转二维
来源:互联网 发布:垃圾发电厂危害 知乎 编辑:程序博客网 时间:2024/06/05 00:23
转置
例如:
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]
阅读全文
0 0
- Python numpy 转置、逆、去掉一列、按列取出、矩阵拼接、矩阵排序、矩阵相等、np.where,一维转二维
- 给numpy矩阵添加一列
- 给numpy矩阵添加一列
- 给numpy矩阵添加一列
- Python获取二维矩阵每列最大值
- python numpy 矩阵操作
- python numpy 矩阵用法
- 给numpy矩阵添加一行或一列
- Python numpy生成矩阵、串联矩阵
- Python numpy生成矩阵、串联矩阵
- Python基础-numpy中矩阵增加行或列
- Python numpy 提取矩阵的某一行或某一列
- python numpy包---矩阵运算
- Python:合并两个numpy矩阵
- python numpy 矩阵转置
- Python中的numpy矩阵运算
- Python numpy矩阵处理技巧
- Matlab从三维矩阵中取出一列并且reshape
- 这篇文章让你了解智能制造及其十大关键技术
- github使用SSH Clone工程避免多次输入用户密码
- 这哥们讲了比较多的高通方面开发遇到的问题
- 同步服务端
- Flex修改语言编译错误问题
- Python numpy 转置、逆、去掉一列、按列取出、矩阵拼接、矩阵排序、矩阵相等、np.where,一维转二维
- AI | 杭州国芯推语音AI芯片 让你的智能设备随处可用
- 无人机操控成澳高考项目 尼康关相机工厂 手机的锅
- 自动驾驶圈的4位学院派“长老”
- python-正则表达式
- [DeeplearningAI笔记]ML strategy_1_3可避免误差与改善模型方法
- 8 种 NoSQL 数据库系统对比
- unity——PictureAnimation
- 剑指offer---替换空格