numpy库
来源:互联网 发布:用在淘宝上的比价神器' 编辑:程序博客网 时间:2024/06/08 04:12
1、创建随机矩阵
>>> from numpy import *>>> random.rand(4,4)array([[ 0.1801566 , 0.02580119, 0.02685281, 0.52768083], [ 0.45411008, 0.7831068 , 0.21375316, 0.5834276 ], [ 0.14515285, 0.56107743, 0.70939684, 0.38059215], [ 0.38190561, 0.90318702, 0.36228335, 0.41596983]])>>> random.rand(1,5)array([[ 0.44651467, 0.62156783, 0.26844721, 0.0429569 , 0.94402983]])>>> mat(random.rand(2,2))-----》转换为矩阵matrix([[ 0.70740332, 0.74586087], [ 0.63854925, 0.08342785]])>>> randMat = mat(random.rand(4,5))>>> randMatmatrix([[ 0.06601757, 0.01608014, 0.42190585, 0.01011915, 0.31645594], [ 0.65403083, 0.59347221, 0.19135569, 0.46777104, 0.66128351], [ 0.35348795, 0.82899244, 0.56450194, 0.90785287, 0.79258663], [ 0.71413199, 0.62240897, 0.17560023, 0.51640713, 0.51107191]])>>> randMat.I-----》求矩阵的逆matrix([[ 0.99798378, -1.76149543, -1.19227532, 3.46560494], [-0.85138719, -0.19618883, 0.49031565, 0.21072417], [ 2.50529117, -4.01838827, 0.18836154, 3.3826735 ], [-0.89262193, -1.69025782, 1.0541609 , 0.95941787], [-0.31650047, 5.78889385, -0.06102358, -5.27421759]])>>>>>> IrandMat = randMat.I>>> myEye = IrandMat * randMat----》矩阵*矩阵的逆=单位矩阵>>> myEyematrix([[ 0.96725654, 0.13928549, 0.01950231, -0.1066253 , -0.02283814], [ 0.13928549, 0.40750154, -0.08295973, 0.45356717, 0.09714986], [ 0.01950231, -0.08295973, 0.98838424, 0.06350702, 0.01360261], [-0.1066253 , 0.45356717, 0.06350702, 0.65278699, -0.07436979], [-0.02283814, 0.09714986, 0.01360261, -0.07436979, 0.98407068]])>>> myEye - eye(4)Traceback (most recent call last): File "<stdin>", line 1, in <module>ValueError: operands could not be broadcast together with shapes (5,5) (4,4)>>> eye(4)array([[ 1., 0., 0., 0.], [ 0., 1., 0., 0.], [ 0., 0., 1., 0.], [ 0., 0., 0., 1.]])>>> myEye - eye(4)Traceback (most recent call last): File "<stdin>", line 1, in <module>ValueError: operands could not be broadcast together with shapes (5,5) (4,4)>>> myEye - eye(5)-----》减去5*5的单位矩阵,计算误差matrix([[-0.03274346, 0.13928549, 0.01950231, -0.1066253 , -0.02283814], [ 0.13928549, -0.59249846, -0.08295973, 0.45356717, 0.09714986], [ 0.01950231, -0.08295973, -0.01161576, 0.06350702, 0.01360261], [-0.1066253 , 0.45356717, 0.06350702, -0.34721301, -0.07436979], [-0.02283814, 0.09714986, 0.01360261, -0.07436979, -0.01592932]])
2、shape()—>针对矩阵
from numpy import *import operatorprint eye(4);print eye(4).shape;print eye(4).shape[0];#行数print eye(4).shape[1];#列数a = [[1,2,4,5],[2,4,5,5]];b = mat(a);print b.shape;print b.shape[0];print b.shape[1];[[ 1. 0. 0. 0.] [ 0. 1. 0. 0.] [ 0. 0. 1. 0.] [ 0. 0. 0. 1.]](4, 4)44(2, 4)24
3、矩阵的重复
c = [1,2];print tile(c,3);#列重复3次print tile(c,(2,3));#行重复2次 列重复3次
4、sum()
b = [[1,2,4,5],[2,4,5,5]];#b = mat(a);result = np.sum(b,axis=1);#一个矩阵的每一行向量相加,result2 = np.sum(b,axis=0);#矩阵的所有行向量相加,print result;print result2;
阅读全文
0 0
- numpy库
- Numpy库
- numpy库
- numpy库
- numpy库
- python中的numpy库的numpy.asarray
- fodera安装numpy库
- numpy 库记录
- python -numpy 库
- python库之numpy
- python安装numpy库
- python numpy库
- py numpy库
- numpy库用法总结
- [Python] Numpy库
- Python 中的numpy 库
- Python之NumPy库
- numpy库 简介
- 比特币学习之钱包
- 解决SDL/SDL.h: No such file or directory的方法
- hadoop之hadoop配置
- Keepalived+Nginx+Redis+Tomcat实现高可用web负载均衡
- Javaweb开发(前端和后端学习资料)
- numpy库
- 关于指针传递和指针的引用传递
- 数据结构:手动编写Java栈
- Solr之高亮显示-yellowcong
- 介绍一种音频信号分类方法
- python时间格式输出 以及递增 日期加一天
- Spring 事务
- Oracle 存储过程样例
- FMDB保存数组和字典