python如何建立全零数组
作者:晓曦&sea 时间:2023-08-02 09:50:45
语句格式:
numpy.zeros(shape, dtype=float, order='C')
参数说明:
shape:整型或元素为整型的序列,表示生成的新数组的shape,如(2,3)或 2。
dtype:生成数组的数据格式,如numpy.int8。默认为numpy.float64。
order:{'C', 'F'}可选,是否将多维数据存储为C-或Fortran-contiguous(按行或按列)顺序。
返回值:ndarray,一个指定了shape, dtype, order的零数组。
示例见下:
第四个例子看起来很方便。
Numpy文档原文:
numpy.zeros
numpy.zeros(shape, dtype=float, order='C')
Return a new array of given shape and type, filled with zeros.
Parameters:
shape : int or sequence of ints
Shape of the new array, e.g., (2, 3) or 2.
dtype : data-type, optional
The desired data-type for the array, e.g., numpy.int8. Default is numpy.float64.
order : {‘C', ‘F'}, optional
Whether to store multidimensional data in C- or Fortran-contiguous (row- or column-wise) order in memory.
Returns:
out : ndarray
Array of zeros with the given shape, dtype, and order.
#指定长度的一维数组
>>> np.zeros(5)
array([ 0., 0., 0., 0., 0.])
#指定数据类型,指定长度的一维数组
>>> np.zeros((5,), dtype=int)
array([0, 0, 0, 0, 0])
#二维数组
>>> np.zeros((2, 1))
array([[ 0.],
[ 0.]])
>>> s = (2,2)
>>> np.zeros(s)
array([[ 0., 0.],
[ 0., 0.]])
#指定dtype
>>> np.zeros((2,), dtype=[('x', 'i4'), ('y', 'i4')]) # custom dtype
array([(0, 0), (0, 0)],
dtype=[('x', '<i4'), ('y', '<i4')])
内容扩展:
python创建数组的方法
直接定义法:
1.直接定义
matrix=[0,1,2,3]
2.间接定义
matrix=[0 for i in range(4)]
print(matrix)
Numpy方法:
Numpy内置了从头开始创建数组的函数:
zeros(shape)将创建一个用指定形状用0填充的数组。默认的dtype是float64。
下面是几种常用的创建方法:
#coding=utf-8
import numpy as np
a = np.array([1,2,3,4,5])
print a
b = np.zeros((2,3))
print b
c = np.arange(10)
print c
d = np.arange(2,10,dtype=np.float)
print d
e = np.linspace(1.0,4.0,6)
print e
f = np.indices((3,3))
print f
来源:https://www.py.cn/jishu/jichu/19592.html
标签:python,全零数组
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
原生js实现tab选项卡切换
2024-04-19 10:43:14
![](https://img.aspxhome.com/file/2023/9/135809_0s.jpg)
thinkphp四种url访问方式详解
2024-05-05 09:16:50
Python xlwings插入Excel图片的实现方法
2023-11-23 05:53:18
![](https://img.aspxhome.com/file/2023/8/115738_0s.jpg)
JS实现仿百度文库评分功能
2024-05-11 09:34:05
![](https://img.aspxhome.com/file/2023/3/126163_0s.gif)
CentOS7.2虚拟机上安装MySQL 5.6.32的教程
2024-01-23 07:30:59
![](https://img.aspxhome.com/file/2023/3/121553_0s.png)
用 Python 脚本实现电脑唤醒后自动拍照并截屏发邮件通知
2023-08-30 14:15:44
![](https://img.aspxhome.com/file/2023/5/62555_0s.png)
快速认识CSS中的overflow属性
2009-05-29 16:36:00
![](https://img.aspxhome.com/file/UploadPic/up/2009052916595412.gif)
vue阻止页面回退的实现方法(浏览器适用)
2024-06-07 15:24:10
MySQL之存储过程按月创建表的方法步骤
2024-01-25 02:40:40
Python 保存矩阵为Excel的实现方法
2022-07-23 07:05:00
numpy中np.dstack()、np.hstack()、np.vstack()用法
2021-08-27 11:47:42
详解SQL游标的用法
2024-01-18 02:41:30
![](https://img.aspxhome.com/file/2023/2/105462_0s.png)
解决IOS端微信H5页面软键盘弹起后页面下方留白的问题
2024-04-27 15:47:30
简化版的vue-router实现思路详解
2024-05-10 14:17:28
MySQL 回表,覆盖索引,索引下推
2024-01-21 12:56:59
![](https://img.aspxhome.com/file/2023/9/118189_0s.png)
详细介绍Python中的偏函数
2022-12-28 08:40:53
python 如何对logging日志封装
2023-07-06 11:27:36
![](https://img.aspxhome.com/file/2023/3/107993_0s.png)
Python 实现过滤掉列表中唯一值
2021-09-26 23:13:55
![](https://img.aspxhome.com/file/2023/8/117978_0s.jpg)
mysql启动时出现ERROR 2003 (HY000)问题的解决方法
2024-01-22 02:53:13
![](https://img.aspxhome.com/file/2023/7/110327_0s.png)
Django 批量插入数据的实现方法
2023-01-11 10:40:58