NumPy - 数组创建例程

新的ndarray对象可以通过任何下列数组创建例程或使用低级ndarray构造函数构造。

numpy.empty

它创建指定形状和dtype的未初始化数组。 它使用以下构造函数:

numpy.empty(shape, dtype = float, order = 'C')

构造器接受下列参数:

序号 参数及描述
1. Shape 空数组的形状,整数或整数元组
2. Dtype 所需的输出数组类型,可选
3. Order 'C'为按行的 C 风格数组,'F'为按列的 Fortran 风格数组

示例

下面的代码展示空数组的例子:

import numpy as np
x = np.empty([3,2], dtype = int)
print x

输出如下:

[[22649312    1701344351]
[1818321759 1885959276]
[16779776 156368896]]

注意:数组元素为随机值,因为它们未初始化。

numpy.zeros

返回特定大小,以 0 填充的新数组。

numpy.zeros(shape, dtype = float, order = 'C')

构造器接受下列参数:

序号 参数及描述
1. Shape 空数组的形状,整数或整数元组
2. Dtype 所需的输出数组类型,可选
3. Order 'C'为按行的 C 风格数组,'F'为按列的 Fortran 风格数组

示例 1

# 含有 5 个 0 的数组,默认类型为 float
import numpy as np
x = np.zeros(5)
print x

输出如下:

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

示例 2

import numpy as np
x = np.zeros((5,), dtype = np.int)
print x

输出如下:

[0  0  0  0  0]

示例 3

# 自定义类型
import numpy as np
x = np.zeros((2,2), dtype = [('x', 'i4'), ('y', 'i4')])
print x

输出如下:

[[(0,0)(0,0)]
[(0,0)(0,0)]]

numpy.ones

返回特定大小,以 1 填充的新数组。

numpy.ones(shape, dtype = None, order = 'C')

构造器接受下列参数:

序号 参数及描述
1. Shape 空数组的形状,整数或整数元组
2. Dtype 所需的输出数组类型,可选
3. Order 'C'为按行的 C 风格数组,'F'为按列的 Fortran 风格数组

示例 1

# 含有 5 个 1 的数组,默认类型为 float
import numpy as np
x = np.ones(5) print x

输出如下:

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

示例 2

import numpy as np
x = np.ones([2,2], dtype = int)
print x

输出如下:

[[1  1]
[1 1]]

NumPy数组创建例程的更多相关文章

  1. numpy 数组创建例程

    1 numpy.empty empty(shape[, dtype=float, order='C']) 创建指定 shape 和dtype 的未初始化数组 返回:ndarray. 说明:order ...

  2. NumPy 数组创建

    章节 Numpy 介绍 Numpy 安装 NumPy ndarray NumPy 数据类型 NumPy 数组创建 NumPy 基于已有数据创建数组 NumPy 基于数值区间创建数组 NumPy 数组切 ...

  3. Python数据分析之numpy数组全解析

    1 什么是numpy numpy是一个在Python中做科学计算的基础库,重在数值计算,也是大部分Python科学计算库的基础库,多用于大型.多维数据上执行数值计算. 在NumPy 中,最重要的对象是 ...

  4. 3.NumPy - 数组属性

    1.ndarray.shape 这一数组属性返回一个包含数组维度的元组,它也可以用于调整数组大小 # -*- coding: utf-8 -*- import numpy as np a = np.a ...

  5. NumPy 数组迭代

    章节 Numpy 介绍 Numpy 安装 NumPy ndarray NumPy 数据类型 NumPy 数组创建 NumPy 基于已有数据创建数组 NumPy 基于数值区间创建数组 NumPy 数组切 ...

  6. NumPy 数组切片

    章节 Numpy 介绍 Numpy 安装 NumPy ndarray NumPy 数据类型 NumPy 数组创建 NumPy 基于已有数据创建数组 NumPy 基于数值区间创建数组 NumPy 数组切 ...

  7. NumPy 从已有的数组创建数组

    NumPy 从已有的数组创建数组 本章节我们将学习如何从已有的数组创建数组. numpy.asarray numpy.asarray 类似 numpy.array,但 numpy.asarray 只有 ...

  8. numpy数组的创建

    创建数组 创建ndarray 创建数组最简单的方法就是使用array函数.它接收一切序列型的对象(包括其他数组),然后产生一个新的含有传入数据的Numpy数组. array函数创建数组 import ...

  9. Numpy之数组创建

    ndarray 数组除了可以使用 ndarray 构造器来创建外,也可以通过如下方式创建. 一.创建数组 numpy.empty 语法: numpy.empty(shape, dtype = floa ...

随机推荐

  1. 关于redux应用

    redux 有点类似flux.但是我觉得远比flux要复杂.因为他非常的绕.一般搭配使用是redux 和react-redux 使用. 主要的思路就是: 写action:动作类型 写reducer:动 ...

  2. delphi ----寻找控件,以字符串类型的名称控件

    vari :Integer;beginfor i := 1 to 10 do(FindComponent('Edit'+inttostr(i)) as TEdit).Text := inttostr( ...

  3. delphi中 ExecSQL 与 open

    对于不用返回结果集的要用execsql反之则用open;insert ,update,delete就要用到execsql;select就要用open 说得对,例子:with query1 do clo ...

  4. Powershell Exchange Message Per Day Sent and Reveive

    Powershell Exchange Message Per Day Sent and Reveive # Initialize some variables used for counting a ...

  5. 解决URL地址中的中文乱码问题的办法

    解决URL地址中的中文乱码问题的办法 引言: 在Restful类的服务设计中,经常会碰到需要在URL地址中使用中文作为的参数的情况,这种情况下,一般都需要正确的设置和编码中文字符信息.乱码问题就此产生 ...

  6. 3*0.1 == 0.3 将会返回什么?true 还是 false?

    false,因为有些浮点数不能完全精确的表示出来 public static void main(String[] args) { System.out.println(3 * 0.1); Syste ...

  7. linkText()的用法

    1.linkText()常用于定位链接,以谷歌的gmail为例: WebElement gmailLink = driver.findElement(By.linkText("Gmail&q ...

  8. Python迭代对象、迭代器、生成器

    在了解Python的数据结构时,容器(container).可迭代对象(iterable).迭代器(iterator).生成器(generator).列表/集合/字典推导式(list,set,dict ...

  9. Linux mount/unmount命令(6/16)

    linux是一个优秀的开放源码的操作系统,可以运行在大到巨型小到掌上型各类计算机系统上,随着 linux系统的日渐成熟和稳定以及它开放源代码特有的优越性,linux在全世界得到了越来越广泛的应用.现在 ...

  10. 防止CSRF的攻击—Origin和Referer

    防止CSRF的攻击—Origin和Referer 为了防止CSRF的攻击,我们建议修改浏览器在发送POST请求的时候加上一个Origin字段,这个Origin字段主要是用来标识出最初请求是从哪里发起的 ...