矩阵(Matrix)和数组(Array)的区别主要有以下两点:

  • 矩阵只能为2维的,而数组可以是任意维度的。
  • 矩阵和数组在数学运算上会有不同的结构。

代码展示

1.矩阵的创建

  • 采用mat函数创建矩阵
class numpy.mat(data, dtype=None)

(注释:Unlike matrix, asmatrix does not make a copy if the input is already a matrix or an ndarray. Equivalent to matrix(data, copy=False).这句话的含义也就是说,当传入的参数为一个矩阵或者ndarray的数组时候,返回值是和传入参数相同的引用,也就是当传入参数发生改变时候,返回值也会相应的改变。相当于numpy.matrix(data, copy=False))


import numpy as np
e = np.array([[1, 2], [3, 4]])  # 传入的参数为ndarray时
# e= np.matrix([[1, 2], [3, 4]])  # 传入的参数为矩阵时
print(e)
print('e的类型:', type(e))
print('---'*5)
e1 = np.mat(e)
print(e1)
print('e1的类型:', type(e1))
print('---'*5)
print('改变e中的值,分别打印e和e1')
# 注意矩阵和ndarray修改元素的区别
e[0][0] = 0  # 传入的参数为ndarray时
# e[0, 0] = 0  # 传入的参数为矩阵时
print(e)
print(e1)
print('---'*5)
[[1 2]
[3 4]]
e的类型: <class 'numpy.matrix'>
---------------
[[1 2]
[3 4]]
e1的类型: <class 'numpy.matrix'>
---------------
改变e中的值,分别打印e和e1
[[0 2]
[3 4]]
[[0 2]
[3 4]]
---------------
  • 采用matrix函数创建矩阵
class numpy.matrix(data, dtype=None, copy=True)

(注释:Returns a matrix from an array-like object, or from a string of data. A matrix is a specialized 2-D array that retains its 2-D nature through operations. It has certain special operators, such as * (matrix multiplication) and ** (matrix power).)

import numpy as np

e = np.array([[1, 2], [3, 4]])
# e = '1 2;3 4' # 通过字符串创建矩阵
e1 = np.matrix(e) # 传入的参数为矩阵时
print(e1)
print('e1的类型:', type(e1))
print('---'*5)
print('改变e中的值,分别打印e和e1')
e[0][0] = 0
print(e)
print(e1)
print('---'*5)
[[1 2]
[3 4]]
e1的类型: <class 'numpy.matrix'>
---------------
改变e中的值,分别打印e和e1
[[0 2]
[3 4]]
[[1 2]
[3 4]]
---------------

2.数组的创建

  • 通过传入列表创建
  • 通过range()和reshape()创建
  • linspace()和reshape()创建
  • 通过内置的一些函数创建
import numpy as np

e = [[1, 2], [3, 4]]
e1 = np.array(e)
print(e)
n = np.arange(0, 30, 2) # 从0开始到30(不包括30),步长为2
n = n.reshape(3, 5)
print(n)
o = np.linspace(0, 4, 9)
o.resize(3, 3)
print(o)
a = np.ones((3, 2))
print(a)
b = np.zeros((2, 3))
print(b)
c = np.eye(3) # 3维单位矩阵
print(c)
y = np.array([4, 5, 6])
d = np.diag(y) # 以y为主对角线创建矩阵
print(d)
e = np.random.randint(0, 10, (4, 3))
print(e)
---------------
[[1, 2], [3, 4]]
[[ 0 2 4 6 8]
[10 12 14 16 18]
[20 22 24 26 28]]

[[0. 0.5 1. ]
[1.5 2. 2.5]
[3. 3.5 4. ]]

[[1. 1.]
[1. 1.]
[1. 1.]]

[[0. 0. 0.]
[0. 0. 0.]]

[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]

[[4 0 0]
[0 5 0]
[0 0 6]]

[[3 0 2]
[1 5 1]
[9 4 7]
[5 8 9]]

3.矩阵和数组的数学运算

  • 矩阵的乘法和加法

矩阵的乘法和加法和线性代数的矩阵加法和乘法一致,运算符号也一样用*,**表示平方,例如e**2 =e*e。

  • 数组的加法和乘法

数组的乘法和加法为相应位置的数据乘法和加法。

Numpy中矩阵和数组的区别的更多相关文章

  1. Python numpy中矩阵的用法总结

    关于Python Numpy库基础知识请参考博文:https://www.cnblogs.com/wj-1314/p/9722794.html Python矩阵的基本用法 mat()函数将目标数据的类 ...

  2. opencv、numpy中矩阵转置,矩阵内的固定位置相应的坐标变换

    opencv.numpy中矩阵转置,矩阵内的固定位置相应的坐标变换

  3. Python数据分析--Numpy常用函数介绍(6)--Numpy中矩阵和通用函数

    在NumPy中,矩阵是 ndarray 的子类,与数学概念中的矩阵一样,NumPy中的矩阵也是二维的,可以使用 mat . matrix 以及 bmat 函数来创建矩阵. 一.创建矩阵 mat 函数创 ...

  4. C语言中指针和数组的区别

    看<C专家编程>一书,看到数组与指针并不相同一章,遂做了一段测试: 代码: #include <stdio.h> #include <stdlib.h> int m ...

  5. 上机实践 - - 一个例子了解C/C++中指针与数组的区别

    本例子来自于<剑指Offer>(P37) 解答如下: size1:20 data1是一个数组,sizeof(data1)是求数组大小. 这个数组包含5个整数,每个整数4个字节,共20字节. ...

  6. numpy中矩阵乘法,星乘(*)和点乘(.dot)的区别

    import numpy a = numpy.array([[,], [,]]) b = numpy.array([[,], [,]]) 星乘表示矩阵内各对应位置相乘,矩阵a*b下标(0,0)=矩阵a ...

  7. 【转】numpy中 meshgrid 和 mgrid 的区别和使用

    转自:https://www.cnblogs.com/shenxiaolin/p/8854197.html 一.meshgrid函数 meshgrid函数通常使用在数据的矢量化上. 它适用于生成网格型 ...

  8. Python的 numpy中 meshgrid 和 mgrid 的区别和使用

    一.meshgrid函数 meshgrid函数通常使用在数据的矢量化上. 它适用于生成网格型数据,可以接受两个一维数组生成两个二维矩阵,对应两个数组中所有的(x,y)对. 示例展示: 由上面的示例展示 ...

  9. [转]Numpy中矩阵对象(matrix)

    numpy模块中的矩阵对象为numpy.matrix,包括矩阵数据的处理,矩阵的计算,以及基本的统计功能,转置,可逆性等等,包括对复数的处理,均在matrix对象中. class numpy.matr ...

随机推荐

  1. (尚019)Vue基于脚手架编写项目

    vue_demo目录结构截图: (1)图一 (2).图二 (3).图三 (四).图四 (5).图五 (6).图六 (7).图七 不能随便改入口文件的名字,因为已经配置好了 (8).图八 (9).图九 ...

  2. PHP正则表达式提取html超链接中的href地址

    $preg='/<a .*?href="(.*?)".*?>/is'; preg_match_all($preg,$str,$array2); ;$i<count ...

  3. POJ 1436.Horizontally Visible Segments-线段树(区间更新、端点放大2倍)

    水博客,水一水. Horizontally Visible Segments Time Limit: 5000MS   Memory Limit: 65536K Total Submissions:  ...

  4. python计算1~2008中0和1的个数

    计算1~2008中所有自然数中1和0的个数总数. 通过自然数的大小划分区间,将自然数每位上的数载入列表,循环计数. list = [] onecount = 0 zerocount = 0 for i ...

  5. 【18NOIP普及组】对称二叉树(信息学奥赛一本通 1981)(洛谷 5018)

    [题目描述] 一棵有点权的有根树如果满足以下条件,则被轩轩称为对称二叉树: 1.二叉树: 2.将这棵树所有节点的左右子树交换,新树和原树对应位置的结构相同且点权相等. 下图中节点内的数字为权值,节点外 ...

  6. ranger整合kerberos

    一.生成主体 在kerberos服务器生成用于ranger的用户主体: # kadmin.local addprinc -randkey HTTP/manager1@HADOOP.COM addpri ...

  7. ES6继承小实例

    ES6继承小实例 一.总结 一句话总结: js中的类和继承可以多用es6里面的,和其它后端语言的使用方法一样 class Animal { constructor(name) { this.name ...

  8. 008 webpack的其他使用方式

    一:配置 1.配置文件 每次修改main文件,重新打包都要指定入口与出口,比较费事,可以使用配置文件的方式 在根目录下新建webpack.config.js: const path = require ...

  9. Python3 多线程(连接池)操作MySQL插入数据

    1.主要模块DBUtils : 允许在多线程应用和数据库之间连接的模块套件Threading : 提供多线程功能 2.创建连接池PooledDB 基本参数: mincached : 最少的空闲连接数, ...

  10. 软件定义网络基础---REST API的设计规范

    一:REST API的设计 REST API是基于HTTP协议进行设计的,由HTTP动词+URI组成 (一)HTTP动词 (二)资源的原型 文档(Document): 文档是资源的单一表现形式: 集合 ...