博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
numpy常用方法详解(array,matrix差别分析)
阅读量:4097 次
发布时间:2019-05-25

本文共 1295 字,大约阅读时间需要 4 分钟。

numpy/referencehttps://docs.scipy.org/doc/numpy-1.10.0/reference/generated/

数据array相乘,矩阵matrix相乘运算的差别

MATLAB中矩阵相乘用星号"*",元素相乘用点星 ".*"

对于array数据,对应元素相乘也就是每个元素的平方,直接用星号*

a=np.array([[1,2,3],           [4,5,6]])print(a*a)print(a**2) # 对应元素相乘,结果相等
结果都是:
[[ 1  4  9] [16 25 36]]

对于array数据,矩阵的乘法用函数 np.dot()

a=np.array([[1,2,3],           [4,5,6]])print( np.dot(a,a.T))  print( a.dot(a.T)) #另一种写法

结果都是:

[[14 32] [32 77]]

对于matrix数据,对应元素相乘,即元素平方,用函数 np.multiply(a,a)

a=np.mat([[1,2,3],          [4,5,6]])np.multiply(a,a)
结果:
matrix([[ 1,  4,  9],        [16, 25, 36]])

对于matrix数据,矩阵乘法, 用星号* ,或者函数, np.dot()

a=np.mat([[1,2,3],          [4,5,6]])print (np.dot(a,a.T))   #矩阵乘法print (a*a.T)
结果:matrix([[14, 32],        [32, 77]])

其它一些矩阵运算:

数据array的指数运算,使用**,

a=np.array([[-1,2,3],            [2,-4,6]])print(a**3)
结果:
array([[ -1,   8,  27],       [  8, -64, 216]], dtype=int32)

合并数组

import numpy as np#np.hstack(a,b)#!注意 只接受一个参数,list或者tuple#np.vstack(a,b)a1 = np.array([[1,2],             [4,5]])a2 = np.array([3,6])a2.shape=1,2print(np.vstack((a1,a2)))#垂直放,列要求相等a2.shape=2,1print(np.hstack([a1,a2]))#水平放,行要求相等

一维的array不区分行列,所以转置操作无效。可以通过shape属性,变为二维再使用转置

用数组的shape属性调整维度,升维降维,等价于reshape (r,c)

特征向量按特征值排序

value,eigvector = np.linalg.eig(Cx) indices = np.argsort(value)   indices[::-1]eigvector[:,indices]

你可能感兴趣的文章
Linux网络编程---I/O复用模型之poll
查看>>
Java NIO详解
查看>>
单列模式-编写类ConfigManager读取属性文件
查看>>
java中float和double的区别
查看>>
Statement与PreparedStatement区别
查看>>
Tomcat配置数据源步骤以及使用JNDI
查看>>
before start of result set 是什么错误
查看>>
(正则表达式)表单验证
查看>>
在JS中 onclick="save();return false;"return false是
查看>>
JSTL 常用标签总结
查看>>
内容里面带标签,在HTML显示问题,JSTL
查看>>
VS编译器运行后闪退,处理方法
查看>>
用div+css做下拉菜单,当鼠标移向2级菜单时,为什么1级菜单的a:hover背景色就不管用了?
查看>>
idea 有时提示找不到类或者符号
查看>>
JS遍历的多种方式
查看>>
ng-class的几种用法
查看>>
node入门demo-Ajax让前端angularjs/jquery与后台node.js交互,技术支持:mysql+html+angularjs/jquery
查看>>
神经网络--单层感知器
查看>>
注册表修改DOS的编码页为utf-8
查看>>
matplotlib.pyplot.plot()参数详解
查看>>