python如何建立全零数组


Posted in Python onJuly 19, 2020

语句格式:

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

到此这篇关于python如何建立全零数组的文章就介绍到这了,更多相关python建立全零数组的方法内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
python 获取本机ip地址的两个方法
Feb 25 Python
pycharm 使用心得(六)进行简单的数据库管理
Jun 06 Python
对于Python编程中一些重用与缩减的建议
Apr 14 Python
python监控文件或目录变化
Jun 07 Python
Python 基础教程之str和repr的详解
Aug 20 Python
基于数据归一化以及Python实现方式
Jul 11 Python
python2 与 python3 实现共存的方法
Jul 12 Python
对Pycharm创建py文件时自定义头部模板的方法详解
Feb 12 Python
pycharm快捷键汇总
Feb 14 Python
Python-opencv实现红绿两色识别操作
Jun 04 Python
基于Python和C++实现删除链表的节点
Jul 06 Python
pytorch交叉熵损失函数的weight参数的使用
May 24 Python
解决python中0x80072ee2错误的方法
Jul 19 #Python
python给视频添加背景音乐并改变音量的具体方法
Jul 19 #Python
python中加背景音乐如何操作
Jul 19 #Python
python实现最短路径的实例方法
Jul 19 #Python
python等待10秒执行下一命令的方法
Jul 19 #Python
python怎么删除缓存文件
Jul 19 #Python
python实现从ftp上下载文件的实例方法
Jul 19 #Python
You might like
php绝对路径与相对路径之间关系的的分析
2010/03/03 PHP
ThinkPHP使用UTFWry地址库进行IP定位实例
2014/04/01 PHP
PHP中让curl支持sock5的代码实例
2015/01/21 PHP
PHP将session信息存储到数据库的类实例
2015/03/04 PHP
Yii实现Command任务处理的方法详解
2016/07/14 PHP
PHP中error_reporting函数用法详细介绍
2017/06/11 PHP
PHP使用PDO访问oracle数据库的步骤详解
2017/09/29 PHP
PHP pthreads v3使用中的一些坑和注意点分析
2020/02/21 PHP
JavaScript实现多维数组的方法
2013/11/20 Javascript
js动态改变select选择变更option的index值示例
2014/07/10 Javascript
jQuery的deferred对象详解
2014/11/12 Javascript
javascript与css3动画结合使用小结
2015/03/11 Javascript
JQuery查找DOM节点的方法
2015/06/11 Javascript
Bootstrap 填充Json数据的实例代码
2017/01/11 Javascript
Bootstrap的popover(弹出框)2秒后定时消失的实现代码
2017/02/27 Javascript
jQuery插件echarts实现的多折线图效果示例【附demo源码下载】
2017/03/04 Javascript
React Native 集成jpush-react-native的示例代码
2017/08/16 Javascript
使用vux实现上拉刷新功能遇到的坑
2018/02/08 Javascript
mpvue网易云短信接口实现小程序短信登录的示例代码
2020/04/03 Javascript
Flask框架学习笔记(一)安装篇(windows安装与centos安装)
2014/06/25 Python
python根据给定文件返回文件名和扩展名的方法
2015/03/27 Python
举例讲解如何在Python编程中进行迭代和遍历
2016/01/19 Python
Python数据拟合与广义线性回归算法学习
2017/12/22 Python
Python使用min、max函数查找二维数据矩阵中最小、最大值的方法
2018/05/15 Python
Python使用tkinter库实现文本显示用户输入功能示例
2018/05/30 Python
对Python3中列表乘以某一个数的示例详解
2019/07/20 Python
Python 开发工具通过 agent 代理使用的方法
2020/09/27 Python
BudgetAir印度:预订航班、酒店和汽车租赁
2019/07/07 全球购物
生物工程专业求职信
2014/09/03 职场文书
售房协议书范本2014
2014/10/23 职场文书
2014年文员工作总结
2014/11/18 职场文书
办公室文员岗位职责
2015/02/04 职场文书
暑期实践个人总结
2015/03/06 职场文书
人工作失职检讨书
2015/05/05 职场文书
退伍军人感言
2015/08/01 职场文书
使用Navicat Premium工具将oracle数据库迁移到MySQL
2021/05/27 Oracle