python中如何基于numpy创建矩阵
2023-03-20 15:26:19 时间
python的功能强大依赖于各种库发挥的作用,例如numpy库就提供了矩阵运算的功能,如果我们想要进行矩阵运算,首先要导入numpy的包,创建矩阵。本文介绍python中基于numpy创建矩阵的三种方法:1、手动创建;2、利用numpy数组创建;3、使用numpy.matix()函数创建矩阵。
一、导入numpy
>>>from numpy import *;#导入numpy的库函数 >>>import numpy as np; #这个方式使用numpy的函数时,需要以np.开头。
二、python基于numpy创建矩阵方法
1、手动创建
a=np.mat('1 2 3;4 5 6;7 8 9') # 中间打逗号也可以 b=np.mat('1,2,3;4,5,6;7,8,9')
2、利用numpy数组创建
c=np.mat(np.arange(9)) #一维的矩阵 c=np.mat(np.arange(9).reshape(3,3))
3、使用numpy.matix()函数创建矩阵
import numpy as np # create 2x2 matrix a = np.matrix([[1, 2], [3, 4]]) # using array of array print('2x2 matrix is: ', a) # using shape attribute to get the tuple describing matrix shape print('The dimension of the matrix is :', a.shape)
以上就是python中基于numpy创建矩阵的三种方法,希望能帮助到你进行创建矩阵哦~
相关文章
- Python中的函数与方法 以及Bound Method和Unbound Method
- 一文贯通python文件读取
- Python 中的异步编程:Asyncio
- 7个你现在就该学习Python的理由
- 提高Python运行效率的六个窍门
- Python数据科学:神经网络
- 一篇文章看懂大数据分析就业前景及职能定位
- R和Python中的文本挖掘:8个入门小贴士
- 告诉你为什么Python有点慢,但我却无所谓?
- 专注学习DevOps编程语言Top 5推荐
- Python发送邮件脚本
- Python多进程并行编程实践: mpi4py 的使用
- Python语言在未来的发展前景
- Python vs Ruby: 谁是最好的 web 开发语言?
- Python对Ruby:谁在Web开发领域更胜一筹?
- Python一行代码完成并行任务
- Python开发者2017应该关注的七个类库
- python爬虫入门基本知识
- 在终端中优雅地编写Python
- Python机器学习实战:信用卡欺诈检测