NumPy简介及教程(一)

NumPy 简介及教程

NumPy 是一个 Python 包。 它代表 “Numeric Python”。 它是一个由多维数组对象和用于处理数组的例程集合组成的库。

NumPy - Ndarray 对象

NumPy 中定义的最重要的对象是称为 ndarray 的 N 维数组类型。 它描述相同类型的元素集合。 可以使用基于零的索引访问集合中的项目。
ndarray中的每个元素在内存中使用相同大小的块。 ndarray中的每个元素是数据类型对象的对象(称为 dtype)。

从ndarray对象提取的任何元素(通过切片)由一个数组标量类型的 Python 对象表示。 下图显示了ndarray,数据类型对象(dtype)和数组标量类型之间的关系。
ndarray

NumPy - 数据类型

序号数据类型及描述
1 bool_存储为一个字节的布尔值(真或假)
2 int_默认整数,相当于 C 的long,通常为int32或int64
3 intc相当于 C 的int,通常为int32或int64
4 intp用于索引的整数,相当于 C 的size_t,通常为int32或int64
5 int8字节(-128 ~ 127)
6 int16 16 位整数(-32768 ~ 32767)
7 int32 32 位整数(-2147483648 ~ 2147483647)
8int64 64 位整数(-9223372036854775808 ~ 9223372036854775807)
9 uint8 8 位无符号整数(0 ~ 255)
10 uint16 16 位无符号整数(0 ~ 65535)
11 uint32 32 位无符号整数(0 ~ 4294967295)
12uint64 64 位无符号整数(0 ~ 18446744073709551615)
13 float_float64的简写
14 float16半精度浮点:符号位,5 位指数,10 位尾数
15 float32单精度浮点:符号位,8 位指数,23 位尾数
16 float64双精度浮点:符号位,11 位指数,52 位尾数
17 complex_complex128的简写
18 complex64复数,由两个 32 位浮点表示(实部和虚部)
18 complex128复数,由两个 64 位浮点表示(实部和虚部)

NumPy - 数据属性

ndarray.shape

import numpy as np
nd1 = np.array([1,2,3,4],[5,6,7,8])
print(nd1)
-> (2,4)

修改shape

nd2 = np.array([[1,2,3],[4,5,6]])
nd2.shape=(3,2)
print(nd2)
->[[1 2]
 [3 4]
 [5 6]]
nd3 = np.arange(0, 150, step=5, dtype=np.float32)
display(nd3.shape, nd3.dtype)
print(nd3)
->(30,)
->dtype('float32')
->[  0.   5.  10.  15.  20.  25.  30.  35.  40.  45.  50.  55.  60.  65.
  70.  75.  80.  85.  90.  95. 100. 105. 110. 115. 120. 125. 130. 135.
 140. 145.]
# num生成多少个数, 全闭区间
nd4 = np.linspace(0,100, num=20)
print(nd4)
# 不使用dtpye属性改变默认的数据类型,使用numpyd.astype(np.float32)
nd4.astype(np.float32)
->[  0.           5.26315789  10.52631579  15.78947368  21.05263158
  26.31578947  31.57894737  36.84210526  42.10526316  47.36842105
  52.63157895  57.89473684  63.15789474  68.42105263  73.68421053
  78.94736842  84.21052632  89.47368421  94.73684211 100.        ]
->array([  0.      ,   5.263158,  10.526316,  15.789474,  21.052631,
        26.31579 ,  31.578947,  36.842106,  42.105263,  47.36842 ,
        52.63158 ,  57.894737,  63.157894,  68.42105 ,  73.68421 ,
        78.947365,  84.210526,  89.47369 ,  94.73684 , 100.      ],
      dtype=float32)
import numpy as np
import matplotlib.pyplot as plt
nd5 = np.zeros(shape=(46,76,3))
nd5.dtype
# jpg rgb (0-255)
# png rgb (0-1)
plt.imshow(nd5)
-><matplotlib.image.AxesImage at 0x878feb8>

这里写图片描述

nd6 = np.full(shape=(2,3,3),fill_value=0.2)
print(nd6)
->[[[0.2 0.2 0.2]
  [0.2 0.2 0.2]
  [0.2 0.2 0.2]]

->[[0.2 0.2 0.2]
  [0.2 0.2 0.2]
  [0.2 0.2 0.2]]]
nd7  = np.eye(5,5)
print(nd7)
->[[1. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0.]
 [0. 0. 1. 0. 0.]
 [0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 1.]]
# ndmin设置最小的维度
nd8 = np.array([1,2,2,3,4], ndmin=2, dtype=float)
print (nd8)
->[[1. 2. 2. 3. 4.]]
# reshape调整数组大小
nd9 = np.arange(24)
b = nd9.reshape(2,4,3)
print(b)
# itemsize这一数组属性返回数组中每个元素的字节单位长度。
print(b.itemsize)
print(b.dtype)
->[[[ 0  1  2]
  [ 3  4  5]
  [ 6  7  8]
  [ 9 10 11]]

 ->[[12 13 14]
  [15 16 17]
  [18 19 20]
  [21 22 23]]]
->4
->int32
# ones生成都是1的矩阵
nd10 = np.ones(shape=(3,3,3))
print(nd10)
->[[[1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]]

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

 [[1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]]]
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值