numpy

结构化数据类型使用

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的主要对象是同种元素的多维数组。这是一个所有的元素都是一种类型、通过一个正整数元组索引的元素表格(...
    oyan99阅读 5,151评论 0 18
  • 介绍 NumPy 是一个 Python 包。 它代表 “Numeric Python”。 它是一个由多维数组对象和...
    喔蕾喔蕾喔蕾蕾蕾阅读 1,802评论 0 5
  • Numpy的组成与功能 Numpy(Numeric Python)可以被理解为一个用python实现的科学计算包,...
    不做大哥好多年阅读 4,325评论 0 10
  • 换工作之后,PYTHON就放下啦,基本没接触了,也都忘记的差不多了,现在重新捡起来 1.1 NumPy Array...
    wqh8384阅读 581评论 0 1
  • 前言 numpy是支持 Python语言的数值计算扩充库,其拥有强大的高维度数组处理与矩阵运算能力。除此之外,nu...
    TensorFlow开发者阅读 3,248评论 0 35