numpy创建矩阵常用方法
numpy创建矩阵常用方法
arange+reshape
in:
n = np.arange(0, 30, 2)# start at 0 count up by 2, stop before 30
n = n.reshape(3, 5) # reshape array to be 3x5
- 1
- 2
out:
linspace+resize
in:
o = np.linspace(0, 4, 9)
o.resize(3, 3)
- 1
- 2
out:
notice:reshape与resize区别
ones zeros eye diag random.randint等创建矩阵
in:
np.ones((3, 2))
np.zeros((2, 3))
np.eye(3)#3维单位矩阵
y = np.array([4, 5, 6])
np.diag(y)#以y为主对角线创建矩阵
np.random.randint(0, 10, (4,3))
- 1
- 2
- 3
- 4
- 5
- 6
out:
ones:
[[ 1. 1.]
[ 1. 1.]
[ 1. 1.]]
zeros:
[[ 0. 0. 0.]
[ 0. 0. 0.]]
eye:
[[ 1. 0. 0.]
[ 0. 1. 0.]
[ 0. 0. 1.]]
diag:
[[4 0 0]
[0 5 0]
[0 0 6]]
randint
[[1 3 5]
[4 4 3]
[9 3 0]
[7 0 0]]
矩阵拼接
in:
p = np.ones([2, 3], int)
np.hstack([p, 2*p])#水平拼接
np.vstack([p, 2*p])#竖直拼接
- 1
- 2
- 3
out:
hstack:
[[1 1 1 2 2 2]
[1 1 1 2 2 2]]
vstack:
[[1 1 1]
[1 1 1]
[2 2 2]
[2 2 2]]
备注:
1 矩阵常用操作:
>>> b1 = array([[1,2,3],[4,5,6],[7,8,9]])
>>> b1[1:2]
array([[4, 5, 6]])
numpy创建矩阵常用方法的更多相关文章
- 利用python的numpy创建矩阵并对其赋值
创建一个3X3的矩阵并对其赋值: x = numpy.array([[1,2,3],[4,5,6],[7,8,9]]) print x print x.shape 运行结果: [[ ] [ ] [ ] ...
- numpy创建array【老鱼学numpy】
在上一篇文章中,我们已经看到了如何通过numpy创建numpy中的数组,这里再重复一下: import numpy as np # 数组 a = [[1, 2, 3], [4, 5, 6]] prin ...
- Python numpy中矩阵的用法总结
关于Python Numpy库基础知识请参考博文:https://www.cnblogs.com/wj-1314/p/9722794.html Python矩阵的基本用法 mat()函数将目标数据的类 ...
- Numpy 定义矩阵的方法
import numpy as np #https://www.cnblogs.com/xzcfightingup/p/7598293.html a = np.zeros((2,3),dtype=in ...
- Numpy 创建数组2
Numpy数组除了可以使用底层 ndarray 构造器来创建外,也可以同伙一下集中方式来创建. numpty.empty numpy.empty方法用来创建一个指定形状(shaoe).数据类型(dty ...
- python创建矩阵
创建二维数组的办法 直接创建(不推荐) 列表生产式法(可以去列表生成式 - 廖雪峰的官方网站学习) 使用模块numpy创建 举个栗子: 创建一个3*3矩阵,并计算主对角线元素之和. import nu ...
- Numpy中矩阵和数组的区别
矩阵(Matrix)和数组(Array)的区别主要有以下两点: 矩阵只能为2维的,而数组可以是任意维度的. 矩阵和数组在数学运算上会有不同的结构. 代码展示 1.矩阵的创建 采用mat函数创建矩阵 c ...
- python numpy和矩阵
2.numpy数据选取 lst=[[1, 2, 3], [4, 5, 6]] np.array(lst)[:-1] Out[32]: array([[1, 2, 3]]) np.array(lst)[ ...
- Python数据分析--Numpy常用函数介绍(6)--Numpy中矩阵和通用函数
在NumPy中,矩阵是 ndarray 的子类,与数学概念中的矩阵一样,NumPy中的矩阵也是二维的,可以使用 mat . matrix 以及 bmat 函数来创建矩阵. 一.创建矩阵 mat 函数创 ...
随机推荐
- Python语言学习之Python入门到进阶
人们常说Python语言简单,编写简单程序时好像也确实如此.但实际上Python绝不简单,它也是一种很复杂的语言,其功能特征非常丰富,能支持多种编程风格,在几乎所有方面都能深度定制.要想用好Pytho ...
- PHP中写定时任务
1.写一个bat E:\Progra~1\wamp\php\php.exe -f "E:\service.php" 2.写接口文件 ignore_user_abort(); //即 ...
- 刷shipid 简便方法
将表中的数据手动更改: select * from cmpps025 where pino = ''; insert into cmpps025 select ncmp, pino, pono, i ...
- 联想功能 Jquery autocomplete.js输入框联想补全功能
转载地址:https://www.cnblogs.com/jinzhiming/p/6768402.html
- 一个小工具,利用php把指定目录文件递归上传到阿里云OSS
cp2oss(_GALLERY_DIR); function cp2oss($directory) { $mydir = dir($directory); while($file = $mydir-& ...
- IEC2017级_1-2班2次博客作业成绩说明
一.博客作业内容 2018上IEC计算机高级语言(C)作业 第2次作业 二.评分规则说明 1.程序调试题,要描述出调试所遇到问题及修改内容,并表述清楚程序功能.流程图不规范的会减1-2分: 2.知识点 ...
- mysql自定义时间段分组
说明:一下是自定义7天为一个时间段的分组统计 select datediff(ms_time, '开始时间') div 7 as time , count(*) count,sum(money) mo ...
- Django模板标签
一.模板标签 1.模板标签是在模板中运用python语言的实现,如for循环,if语句 2.模板标签的运用 2.1在teacher模板下创建students_list模板, 在teacher视图中国创 ...
- freeType移植总结①——使用keil编译freeType2库
在各个技术博客搜索相关资料后,终于将freeType的源码用keil工程编译通过,这里记录一下步骤和遇到的问题. 因为网上的资料都是旧版本freeType的工程,这里博主使用的是freeType2.9 ...
- es6面向对象
<script> class user{ constructor(name,age){ this.name=name; this.age=age; } showName(){ alert( ...