numpy

作者: 蛱蝶飞来过墙去 | 来源:发表于2019-06-25 14:14 被阅读0次

结构化数据类型使用

import numpy as np
dt = np.dtype([('age', np.int8)])
# 展示结构化数据类型的使用,类型字段和对应的实际类型将被创建
a = np.array([(10,), (20,), (30,)], dtype=dt)
print(a)
# [(10,) (20,) (30,)]
print(a["age"])
# [10 20 30]
# 类型字段名可以用于存取实际的 age 列

student = np.dtype([('name', 'S20'), ('age', 'i1'), ('marks', 'f4')])
print(student)
# [('name', 'S20'), ('age', 'i1'), ('marks', '<f4')]
a = np.array([('abc', 21, 50), ('xyz', 18, 75)], dtype=student)
print(a)
#[(b'abc', 21, 50.) (b'xyz', 18, 75.)]

数组属性

a = np.arange(24)
print (a.ndim)             # a 现只有一个维度
#1
# 现在调整其大小
b = a.reshape(2,4,3)  # b 现在拥有三个维度
print (b.ndim)
#3
a = np.array([[1,2,3],[4,5,6]])
print (a.shape)
#(2, 3)

调整数组大小

a = np.array([[1,2,3],[4,5,6]])
a.shape =  (3,2)
print (a)
# [[1 2]
#  [3 4]
#  [5 6]]

a = np.array([[1,2,3],[4,5,6]])
b = a.reshape(3,2)
print (b)
# [[1, 2]
#  [3, 4]
#  [5, 6]]

ndarray.itemsize

# ndarray.itemsize 以字节的形式返回数组中每一个元素的大小。
# 例如,一个元素类型为 float64 的数组 itemsiz 属性值为
# 8(float64 占用 64 个 bits,每个字节长度为 8,所以 64/8,占用 8 个字节),
# 又如,一个元素类型为 complex32 的数组 item 属性为 4(32/8)。
# 数组的 dtype 为 int8(一个字节)
x = np.array([1, 2, 3, 4, 5], dtype=np.int8)
print (x.itemsize)
#1
# 数组的 dtype 现在为 float64(八个字节)
y = np.array([1, 2, 3, 4, 5], dtype=np.float64)
print (y.itemsize)
#8

创建数组

x = np.empty([3, 2], dtype=int)
# 生成随机数
# 三行两列,第一维度三个元素,每一元素里又有两个元素
print(x)
# [[0 0]
#  [0 0]
#  [0 0]]
# 默认为浮点数
x = np.zeros(5)
print(x)
# [0. 0. 0. 0. 0.]
# 设置类型为整数
y = np.zeros((5,), dtype=np.int)
print(y)
# [0 0 0 0 0]
# 自定义类型
z = np.zeros((2, 2), dtype=[('x', 'i4'), ('y', 'i4')])
print(z)
# [[(0, 0) (0, 0)]
#  [(0, 0) (0, 0)]]
print(z.dtype)
#[('x', '<i4'), ('y', '<i4')]

默认为浮点数

x = np.ones(5)
print(x)

自定义类型

x = np.ones([2, 2], dtype=int)
print(x)
# [1. 1. 1. 1. 1.]
# [[1 1]
#  [1 1]]

将列表转化为ndarray

x =  [1,2,3]
a = np.asarray(x)
print (a)
#[1  2  3]

将元组转换为 ndarray:

x =  (1,2,3)
a = np.asarray(x)
print (a)
#[1  2  3]

设置dtype参数

x =  [1,2,3]
a = np.asarray(x, dtype =  float)
print (a)
#[ 1.  2.  3.]

以流的形式读入转化成 ndarray 对象

# numpy.frombuffer
# numpy.frombuffer 用于实现动态数组。
# numpy.frombuffer 接受 buffer 输入参数,以流的形式读入转化成 ndarray 对象。

# buffer    可以是任意对象,会以流的形式读入。
# dtype 返回数组的数据类型,可选
# count 读取的数据数量,默认为-1,读取所有数据。
# offset    读取的起始位置,默认为0。

s =  b'Hello World'
a = np.frombuffer(s, dtype =  'S1')
print (a)
#[b'H' b'e' b'l' b'l' b'o' b' ' b'W' b'o' b'r' b'l' b'd']

NumPy 从数值范围创建数组

# numpy 包中的使用 arange 函数创建数值范围并返回 ndarray 对象,函数格式如下:
# numpy.arange(start, stop, step, dtype)
#根据 start 与 stop 指定的范围以及 step 设定的步长,生成一个 ndarray。

#生成 0 到 5 的数组:
x = np.arange(5)
print (x)
#[0  1  2  3  4]

#设置dtype
x = np.arange(5, dtype =  float)
print (x)
#[0.  1.  2.  3.  4.]

构建等差数列数组

# numpy.linspace
# numpy.linspace 函数用于创建一个一维数组,数组是一个等差数列构成的,格式如下:
# np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)

# 参数说明:
#
# 参数    描述
# start 序列的起始值
# stop  序列的终止值,如果endpoint为true,该值包含于数列中
# num   要生成的等步长的样本数量,默认为50
# endpoint  该值为 ture 时,数列中中包含stop值,反之不包含,默认是True。
# retstep   如果为 True 时,生成的数组中会显示间距,反之不显示。
# dtype ndarray 的数据类型
# 以下实例用到三个参数,设置起始点为 1 ,终止点为 10,数列个数为 10。

a = np.linspace(1,10,10)
print(a)
#[ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10.]

a = np.linspace(1,1,10)
print(a)
#[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
a = np.linspace(10, 20,  5, endpoint =  False)
print(a)
#[10. 12. 14. 16. 18.]

# numpy.logspace 函数用于创建一个于等比数列。格式如下:
# np.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None)

# start 序列的起始值为:base ** start
# stop  序列的终止值为:base ** stop。如果endpoint为true,该值包含于数列中
# num   要生成的等步长的样本数量,默认为50
# endpoint  该值为 ture 时,数列中中包含stop值,反之不包含,默认是True。
# base  对数 log 的底数。
# dtype ndarray 的数据类型

a = np.logspace(1.0,  2.0, num =  10)
print (a)
#
# [ 10.           12.91549665     16.68100537      21.5443469  27.82559402
#   35.93813664   46.41588834     59.94842503      77.42636827    100.    ]

NumPy 切片和索引

a = np.arange(10)
s = slice(2, 7, 2)  # 从索引 2 开始到索引 7 停止,间隔为2
print(a[s])  # 通过切片对象访问数组

# 或者
a = np.arange(10)
b = a[2:7:2]  # 从索引 2 开始到索引 7 停止,间隔为 2
print(b)

# 输出结果为:
# [2  4  6]

# 冒号 : 的解释:如果只放置一个参数,如 [2],将返回与该索引相对应的单个元素。
# 如果为 [2:],表示从该索引开始以后的所有项都将被提取。如果使用了两个参数,
# 如 [2:7],那么则提取两个索引(不包括停止索引)之间的项。

多维数组切片

a = np.array([[1, 2, 3], [3, 4, 5], [4, 5, 6]])
print(a)
# 从某个索引处开始切割
print('从数组索引 a[1:] 处开始切割')
print(a[1:])

# 输出结果为:
#
# [[1 2 3]
#  [3 4 5]
#  [4 5 6]]
# 从数组索引 a[1:] 处开始切割
# [[3 4 5]
#  [4 5 6]]

a = np.array([[1, 2, 3], [3, 4, 5], [4, 5, 6]])
print(a[..., 1])  # 第2列元素
print(a[1, ...])  # 第2行元素
print(a[..., 1:])  # 第2列及剩下的所有元素

# 输出结果为:
# 
# [2 4 5]
# [3 4 5]
# [[2 3]
#  [4 5]
#  [5 6]]

</font>

相关文章

  • 科学计算库numpy的执行示例

    numpy1 numpy2 numpy3 numpy4

  • numpy中的常量

    Constants 正无穷 numpy.inf numpy.Inf numpy.Infinity numpy.in...

  • NumPy学习资料

    Numpy 中文资料 NumPy 中文文档 NumPy 中文用户指南 NumPy 中文参考手册

  • Numpy基础

    安装Numpy Numpy Numpy属性 ndim:纬度 shape:行数和列数 size:元素个数 Numpy...

  • Numpy和Pandas基本操作速查

    """ numpy 基本操作 """'''安装 Numpy 的方法:pip install numpy''''''...

  • numpy 基础

    numpy 基础 导入numpy 版本 np常用方法 numpy.array 的基本属性 numpy.array ...

  • Numpy入门

    1、熟悉 numpy 的基础属性 2、numpy 创建 array 3、numpy的基础运算 4、numpy索引 ...

  • 学习:biopython的安装

    安装Numpy 因为使用biopython需要numpy的支持,所以需要先安装numpy。安装numpy过程如下:...

  • Numpy

    Numpy中文文档 # 基本语法 ``` import numpy myText = numpy.genfromt...

  • numpy运算

    numpy的与运算 numpy 中 argsort() numpy 中的布尔索引

网友评论

      本文标题:numpy

      本文链接:https://www.haomeiwen.com/subject/bpmtcctx.html